2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 '''This file contains item formatters for resource_map_header and
7 resource_map_source files. A resource map is a mapping between resource names
8 (string) and the internal resource ID.'''
11 from functools import partial
16 def GetFormatter(type):
17 if type == 'resource_map_header':
19 elif type == 'resource_map_source':
20 return partial(_FormatSource, _GetItemName)
21 elif type == 'resource_file_map_source':
22 return partial(_FormatSource, _GetItemPath)
26 '''Get the name of the resource map based on the header file name. E.g.,
27 if our header filename is theme_resources.h, we name our resource map
30 |root| is the grd file root.'''
31 outputs = root.GetOutputFiles()
33 for output in outputs:
34 if 'rc_header' == output.GetType():
35 rc_header_file = output.GetFilename()
36 if not rc_header_file:
37 raise Exception('unable to find resource header filename')
38 filename = os.path.splitext(os.path.split(rc_header_file)[1])[0]
39 filename = filename[0].upper() + filename[1:]
40 while filename.find('_') != -1:
41 pos = filename.find('_')
42 if pos >= len(filename):
44 filename = filename[:pos] + filename[pos + 1].upper() + filename[pos + 2:]
48 def _FormatHeader(root, lang='en', output_dir='.'):
49 '''Create the header file for the resource mapping. This file just declares
50 an array of name/value pairs.'''
52 // This file is automatically generated by GRIT. Do not edit.
56 #ifndef GRIT_RESOURCE_MAP_STRUCT_
57 #define GRIT_RESOURCE_MAP_STRUCT_
58 struct GritResourceMap {
59 const char* const name;
62 #endif // GRIT_RESOURCE_MAP_STRUCT_
64 extern const GritResourceMap %(map_name)s[];
65 extern const size_t %(map_name)sSize;
66 ''' % { 'map_name': GetMapName(root) }
69 def _FormatSourceHeader(root):
70 '''Create the header of the C++ source file for the resource mapping.'''
72 map_header_file = None
73 for output in root.GetOutputFiles():
74 if 'rc_header' == output.GetType():
75 rc_header_file = output.GetFilename()
76 elif 'resource_map_header' == output.GetType():
77 map_header_file = output.GetFilename()
78 if not rc_header_file or not map_header_file:
79 raise Exception('resource_map_source output type requires '
80 'resource_map_header and rc_header outputs')
82 // This file is automatically generated by GRIT. Do not edit.
84 #include "%(map_header_file)s"
86 #include "base/basictypes.h"
87 #include "%(rc_header_file)s"
89 const GritResourceMap %(map_name)s[] = {
90 ''' % { 'map_header_file': map_header_file,
91 'rc_header_file': rc_header_file,
92 'map_name': GetMapName(root),
96 def _FormatSourceFooter(root):
97 # Return the footer text.
101 const size_t %(map_name)sSize = arraysize(%(map_name)s);
102 ''' % { 'map_name': GetMapName(root) }
105 def _FormatSource(get_key, root, lang, output_dir):
106 from grit.format import rc_header
107 from grit.node import include, structure, message
108 yield _FormatSourceHeader(root)
109 tids = rc_header.GetIds(root)
111 active_descendants = [item for item in root.ActiveDescendants()]
113 if isinstance(item, (include.IncludeNode,
114 structure.StructureNode,
115 message.MessageNode)):
117 tid = item.attrs['name']
118 if tid in tids and key not in seen:
120 # For messages, only include the active ones
121 if not isinstance(item, message.MessageNode) \
122 or item in active_descendants:
123 yield ' {"%s", %s},\n' % (key, tid)
124 yield _FormatSourceFooter(root)
127 def _GetItemName(item):
128 return item.attrs['name']
131 def _GetItemPath(item):
132 return item.GetInputPath().replace("\\", "/")