OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python2.4 |
| 2 # Copyright (c) 2010 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. |
| 5 |
| 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.''' |
| 9 |
| 10 import os |
| 11 |
| 12 from grit import util |
| 13 from grit.format import interface |
| 14 |
| 15 def GetMapName(root): |
| 16 '''Get the name of the resource map based on the header file name. E.g., |
| 17 if our header filename is theme_resources.h, we name our resource map |
| 18 kThemeResourcesMap. |
| 19 |
| 20 |root| is the grd file root.''' |
| 21 outputs = root.GetOutputFiles() |
| 22 rc_header_file = None |
| 23 for output in outputs: |
| 24 if 'rc_header' == output.GetType(): |
| 25 rc_header_file = output.GetFilename() |
| 26 if not rc_header_file: |
| 27 raise Exception('unable to find resource header filename') |
| 28 filename = os.path.splitext(os.path.split(rc_header_file)[1])[0] |
| 29 filename = filename[0].upper() + filename[1:] |
| 30 while filename.find('_') != -1: |
| 31 pos = filename.find('_') |
| 32 if pos >= len(filename): |
| 33 break |
| 34 filename = filename[:pos] + filename[pos + 1].upper() + filename[pos + 2:] |
| 35 return 'k' + filename |
| 36 |
| 37 |
| 38 class HeaderTopLevel(interface.ItemFormatter): |
| 39 '''Create the header file for the resource mapping. This file just declares |
| 40 an array of name/value pairs.''' |
| 41 def Format(self, item, lang='en', begin_item=True, output_dir='.'): |
| 42 if not begin_item: |
| 43 return '' |
| 44 return '''\ |
| 45 // Copyright (c) %(year)d The Chromium Authors. All rights reserved. |
| 46 // Use of this source code is governed by a BSD-style license that can be |
| 47 // found in the LICENSE file. |
| 48 // This file is automatically generated by GRIT. Do not edit. |
| 49 |
| 50 #include <stddef.h> |
| 51 |
| 52 #ifndef GRIT_RESOURCE_MAP_STRUCT_ |
| 53 #define GRIT_RESOURCE_MAP_STRUCT_ |
| 54 struct GritResourceMap { |
| 55 const char* const name; |
| 56 int value; |
| 57 }; |
| 58 #endif // GRIT_RESOURCE_MAP_STRUCT_ |
| 59 |
| 60 extern const GritResourceMap %(map_name)s[]; |
| 61 extern const size_t %(map_name)sSize; |
| 62 ''' % { 'year': util.GetCurrentYear(), |
| 63 'map_name': GetMapName(item.GetRoot()), |
| 64 } |
| 65 |
| 66 |
| 67 class SourceTopLevel(interface.ItemFormatter): |
| 68 '''Create the C++ source file for the resource mapping. This class handles |
| 69 the header/footer of the file.''' |
| 70 def Format(self, item, lang='en', begin_item=True, output_dir='.'): |
| 71 if begin_item: |
| 72 grit_root = item.GetRoot() |
| 73 outputs = grit_root.GetOutputFiles() |
| 74 rc_header_file = None |
| 75 map_header_file = None |
| 76 for output in outputs: |
| 77 if 'rc_header' == output.GetType(): |
| 78 rc_header_file = output.GetFilename() |
| 79 elif 'resource_map_header' == output.GetType(): |
| 80 map_header_file = output.GetFilename() |
| 81 if not rc_header_file or not map_header_file: |
| 82 raise Exception('resource_map_source output type requires ' |
| 83 'resource_map_header and rc_header outputs') |
| 84 |
| 85 return '''\ |
| 86 // Copyright (c) %(year)d The Chromium Authors. All rights reserved. |
| 87 // Use of this source code is governed by a BSD-style license that can be |
| 88 // found in the LICENSE file. |
| 89 // This file is automatically generated by GRIT. Do not edit. |
| 90 |
| 91 #include "%(map_header_file)s" |
| 92 |
| 93 #include "base/basictypes.h" |
| 94 #include "%(rc_header_file)s" |
| 95 |
| 96 const GritResourceMap %(map_name)s[] = { |
| 97 ''' % { 'year': util.GetCurrentYear(), |
| 98 'map_header_file': map_header_file, |
| 99 'rc_header_file': rc_header_file, |
| 100 'map_name': GetMapName(item.GetRoot()), |
| 101 } |
| 102 else: |
| 103 # Return the footer text. |
| 104 return '''\ |
| 105 }; |
| 106 |
| 107 const size_t %(map_name)sSize = arraysize(%(map_name)s); |
| 108 ''' % { 'map_name': GetMapName(item.GetRoot()) } |
| 109 |
| 110 |
| 111 class SourceInclude(interface.ItemFormatter): |
| 112 '''Populate the resource mapping. For each include, we map a string to |
| 113 the resource ID.''' |
| 114 def Format(self, item, lang='en', begin_item=True, output_dir='.'): |
| 115 if not begin_item: |
| 116 return '' |
| 117 return ' {"%s", %s},\n' % (item.attrs['name'], item.attrs['name']) |
| 118 |
| 119 |
| 120 class SourceFileInclude(interface.ItemFormatter): |
| 121 '''Populate the resource mapping. For each include, we map a filename to |
| 122 the resource ID.''' |
| 123 def Format(self, item, lang='en', begin_item=True, output_dir='.'): |
| 124 if not begin_item: |
| 125 return '' |
| 126 filename = item.attrs['file'].replace("\\", "/") |
| 127 return ' {"%s", %s},\n' % (filename, item.attrs['name']) |
OLD | NEW |