summaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authortc@google.com <tc@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-04-24 18:06:05 +0000
committertc@google.com <tc@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-04-24 18:06:05 +0000
commit80cc3f70e67f5a2efeb0f434c58d9f9b5641f21a (patch)
tree9d7b3bf4cc3caf1690e96164f0f0297ca5c635e6 /tools
parent4d9cf078fa29f9e4b530c909af386ef201e3a935 (diff)
downloadchromium_src-80cc3f70e67f5a2efeb0f434c58d9f9b5641f21a.zip
chromium_src-80cc3f70e67f5a2efeb0f434c58d9f9b5641f21a.tar.gz
chromium_src-80cc3f70e67f5a2efeb0f434c58d9f9b5641f21a.tar.bz2
First cut at adding a map between strings names and resource ids.
This creates a mapping for all the entries in the theme_resources.grd file and adds a static method for querying the mapping. BUG=10639 Review URL: http://codereview.chromium.org/92085 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@14443 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools')
-rw-r--r--tools/grit/grit/format/resource_map.py116
-rw-r--r--tools/grit/grit/node/include.py3
-rw-r--r--tools/grit/grit/node/misc.py6
-rw-r--r--tools/grit/grit/tool/build.py3
4 files changed, 127 insertions, 1 deletions
diff --git a/tools/grit/grit/format/resource_map.py b/tools/grit/grit/format/resource_map.py
new file mode 100644
index 0000000..79ce50c
--- /dev/null
+++ b/tools/grit/grit/format/resource_map.py
@@ -0,0 +1,116 @@
+#!/usr/bin/python2.4
+# Copyright (c) 2009 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+'''This file contains item formatters for resource_map_header and
+resource_map_source files. A resource map is a mapping between resource names
+(string) and the internal resource ID.'''
+
+import os
+
+from grit import util
+from grit.format import interface
+
+def GetMapName(root):
+ '''Get the name of the resource map based on the header file name. E.g.,
+ if our header filename is theme_resources.h, we name our resource map
+ kThemeResourcesMap.
+
+ |root| is the grd file root.'''
+ outputs = root.GetOutputFiles()
+ rc_header_file = None
+ for output in outputs:
+ if 'rc_header' == output.GetType():
+ rc_header_file = output.GetFilename()
+ if not rc_header_file:
+ raise Exception('unable to find resource header filename')
+ filename = os.path.splitext(os.path.split(rc_header_file)[1])[0]
+ filename = filename[0].upper() + filename[1:]
+ while filename.find('_') != -1:
+ pos = filename.find('_')
+ if pos >= len(filename):
+ break
+ filename = filename[:pos] + filename[pos + 1].upper() + filename[pos + 2:]
+ return 'k' + filename
+
+
+class HeaderTopLevel(interface.ItemFormatter):
+ '''Create the header file for the resource mapping. This file just declares
+ an array of name/value pairs.'''
+ def Format(self, item, lang='en', begin_item=True, output_dir='.'):
+ if not begin_item:
+ return ''
+ return '''\
+// Copyright (c) %(year)d The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+// This file is automatically generated by GRIT. Do not edit.
+
+#include <stddef.h>
+
+struct GritResourceMap {
+ const char* const name;
+ int value;
+};
+extern const GritResourceMap %(map_name)s[];
+extern const size_t %(map_name)sSize;
+''' % { 'year': util.GetCurrentYear(),
+ 'map_name': GetMapName(item.GetRoot()),
+ }
+
+
+class SourceTopLevel(interface.ItemFormatter):
+ '''Create the C++ source file for the resource mapping. This class handles
+ the header/footer of the file.'''
+ def Format(self, item, lang='en', begin_item=True, output_dir='.'):
+ if begin_item:
+ grit_root = item.GetRoot()
+ outputs = grit_root.GetOutputFiles()
+ rc_header_file = None
+ map_header_file = None
+ for output in outputs:
+ if 'rc_header' == output.GetType():
+ rc_header_file = output.GetFilename()
+ elif 'resource_map_header' == output.GetType():
+ map_header_file = output.GetFilename()
+ if not rc_header_file or not map_header_file:
+ raise Exception('resource_map_source output type requires '
+ 'resource_map_header and rc_header outputs')
+
+ return '''\
+// Copyright (c) %(year)d The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+// This file is automatically generated by GRIT. Do not edit.
+
+#include "%(map_header_file)s"
+
+#include "base/basictypes.h"
+#include "%(rc_header_file)s"
+
+const GritResourceMap %(map_name)s[] = {
+''' % { 'year': util.GetCurrentYear(),
+ 'map_header_file': map_header_file,
+ 'rc_header_file': rc_header_file,
+ 'map_name': GetMapName(item.GetRoot()),
+ }
+ else:
+ # Return the footer text.
+ return '''\
+};
+
+const size_t %(map_name)sSize = arraysize(%(map_name)s);
+''' % { 'map_name': GetMapName(item.GetRoot()) }
+
+
+class SourceInclude(interface.ItemFormatter):
+ '''Populate the resource mapping. For each include, we map a string to
+ the ID.'''
+ def Format(self, item, lang='en', begin_item=True, output_dir='.'):
+ if not begin_item:
+ return ''
+ short_name = item.attrs['name'].lower()
+ if short_name.startswith('idr_'):
+ short_name = short_name[4:]
+ return ' {"%s", %s},\n' % (short_name, item.attrs['name'])
diff --git a/tools/grit/grit/node/include.py b/tools/grit/grit/node/include.py
index 2175240..b304549 100644
--- a/tools/grit/grit/node/include.py
+++ b/tools/grit/grit/node/include.py
@@ -40,6 +40,9 @@ class IncludeNode(base.Node):
self.attrs['filenameonly'] == 'true',
self.attrs['relativepath'] == 'true',
self.attrs['flattenhtml'] == 'true')
+ elif t == 'resource_map_source':
+ from grit.format import resource_map
+ return resource_map.SourceInclude()
else:
return super(type(self), self).ItemFormatter(t)
diff --git a/tools/grit/grit/node/misc.py b/tools/grit/grit/node/misc.py
index d3441f2..1495615 100644
--- a/tools/grit/grit/node/misc.py
+++ b/tools/grit/grit/node/misc.py
@@ -218,6 +218,12 @@ class GritNode(base.Node):
elif t in ['rc_all', 'rc_translateable', 'rc_nontranslateable']:
from grit.format import rc # avoid circular dep
return rc.TopLevel()
+ elif t == 'resource_map_header':
+ from grit.format import resource_map
+ return resource_map.HeaderTopLevel()
+ elif t == 'resource_map_source':
+ from grit.format import resource_map
+ return resource_map.SourceTopLevel()
else:
return super(type(self), self).ItemFormatter(t)
diff --git a/tools/grit/grit/tool/build.py b/tools/grit/grit/tool/build.py
index 7e274ee..c2111ab 100644
--- a/tools/grit/grit/tool/build.py
+++ b/tools/grit/grit/tool/build.py
@@ -162,7 +162,8 @@ are exported to translation interchange files (e.g. XMB files), etc.
# Microsoft's RC compiler can only deal with single-byte or double-byte
# files (no UTF-8), so we make all RC files UTF-16 to support all
# character sets.
- if output.GetType() in ['rc_header']:
+ if output.GetType() in ('rc_header', 'resource_map_header',
+ 'resource_map_source'):
encoding = 'cp1252'
else:
encoding = 'utf_16'