OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 '''Item formatters for RC headers. | 6 '''Item formatters for RC headers. |
7 ''' | 7 ''' |
8 | 8 |
9 import re | 9 import re |
10 | 10 |
11 from grit.format import interface | 11 from grit.format import interface |
12 from grit import exception | 12 from grit import exception |
13 from grit import util | 13 from grit import util |
14 | 14 |
15 from grit.extern import FP | 15 from grit.extern import FP |
16 | 16 |
17 | 17 |
18 class TopLevel(interface.ItemFormatter): | 18 class TopLevel(interface.ItemFormatter): |
19 '''Writes the necessary preamble for a resource.h file.''' | 19 '''Writes the necessary preamble for a resource.h file.''' |
20 | 20 |
21 def Format(self, item, lang='', begin_item=True, output_dir='.'): | 21 def Format(self, item, lang='', output_dir='.'): |
22 if not begin_item: | 22 header_string = '''// Copyright (c) Google Inc. %d |
23 return '' | |
24 else: | |
25 header_string = '''// Copyright (c) Google Inc. %d | |
26 // All rights reserved. | 23 // All rights reserved. |
27 // This file is automatically generated by GRIT. Do not edit. | 24 // This file is automatically generated by GRIT. Do not edit. |
28 | 25 |
29 #pragma once | 26 #pragma once |
30 ''' % (util.GetCurrentYear()) | 27 ''' % (util.GetCurrentYear()) |
31 # Check for emit nodes under the rc_header. If any emit node | 28 # Check for emit nodes under the rc_header. If any emit node |
32 # is present, we assume it means the GRD file wants to override | 29 # is present, we assume it means the GRD file wants to override |
33 # the default header, with no includes. | 30 # the default header, with no includes. |
34 for output_node in item.GetOutputFiles(): | 31 for output_node in item.GetOutputFiles(): |
35 if output_node.GetType() == 'rc_header': | 32 if output_node.GetType() == 'rc_header': |
36 for child in output_node.children: | 33 for child in output_node.children: |
37 if child.name == 'emit': | 34 if child.name == 'emit': |
38 if child.attrs['emit_type'] == 'prepend': | 35 if child.attrs['emit_type'] == 'prepend': |
39 return header_string | 36 return header_string |
40 # else print out the default header with include | 37 # else print out the default header with include |
41 return header_string + ''' | 38 return header_string + ''' |
42 #include <atlres.h> | 39 #include <atlres.h> |
43 | 40 |
44 ''' | 41 ''' |
45 | 42 |
46 | 43 |
47 class EmitAppender(interface.ItemFormatter): | 44 class EmitAppender(interface.ItemFormatter): |
48 '''Adds the content of the <emit> nodes to the RC header file.''' | 45 '''Adds the content of the <emit> nodes to the RC header file.''' |
49 | 46 |
50 def Format(self, item, lang='', begin_item=True, output_dir='.'): | 47 def Format(self, item, lang='', output_dir='.'): |
51 if not begin_item: | 48 return '%s\n' % (item.GetCdata()) |
52 return '' | |
53 else: | |
54 return '%s\n' % (item.GetCdata()) | |
55 | 49 |
56 class Item(interface.ItemFormatter): | 50 class Item(interface.ItemFormatter): |
57 '''Writes the #define line(s) for a single item in a resource.h file. If | 51 '''Writes the #define line(s) for a single item in a resource.h file. If |
58 your node has multiple IDs that need to be defined (as is the case e.g. for | 52 your node has multiple IDs that need to be defined (as is the case e.g. for |
59 dialog resources) it should define a function GetTextIds(self) that returns | 53 dialog resources) it should define a function GetTextIds(self) that returns |
60 a list of textual IDs (strings). Otherwise the formatter will use the | 54 a list of textual IDs (strings). Otherwise the formatter will use the |
61 'name' attribute of the node.''' | 55 'name' attribute of the node.''' |
62 | 56 |
63 # All IDs allocated so far, mapped to the textual ID they represent. | 57 # All IDs allocated so far, mapped to the textual ID they represent. |
64 # Used to detect and resolve collisions. | 58 # Used to detect and resolve collisions. |
65 ids_ = {} | 59 ids_ = {} |
66 | 60 |
67 # All textual IDs allocated so far, mapped to the numerical ID they | 61 # All textual IDs allocated so far, mapped to the numerical ID they |
68 # represent. Used when literal IDs are being defined in the 'identifiers' | 62 # represent. Used when literal IDs are being defined in the 'identifiers' |
69 # section of the GRD file to define other message IDs. | 63 # section of the GRD file to define other message IDs. |
70 tids_ = {} | 64 tids_ = {} |
71 | 65 |
72 def _VerifyId(self, id, tid, msg_if_error): | 66 def _VerifyId(self, id, tid, msg_if_error): |
73 if id in self.ids_ and self.ids_[id] != tid: | 67 if id in self.ids_ and self.ids_[id] != tid: |
74 raise exception.IdRangeOverlap(msg_if_error + | 68 raise exception.IdRangeOverlap(msg_if_error + |
75 '\nUse the first_id attribute on grouping nodes (<structures>,\n' | 69 '\nUse the first_id attribute on grouping nodes (<structures>,\n' |
76 '<includes>, <messages> and <ids>) to fix this problem.') | 70 '<includes>, <messages> and <ids>) to fix this problem.') |
77 if id < 101: | 71 if id < 101: |
78 print ('WARNING: Numeric resource IDs should be greater than 100 to avoid\
n' | 72 print ('WARNING: Numeric resource IDs should be greater than 100 to avoid\
n' |
79 'conflicts with system-defined resource IDs.') | 73 'conflicts with system-defined resource IDs.') |
80 | 74 |
81 def Format(self, item, lang='', begin_item=True, output_dir='.'): | 75 def Format(self, item, lang='', output_dir='.'): |
82 if not begin_item: | |
83 return '' | |
84 | |
85 # Resources that use the RES protocol don't need | 76 # Resources that use the RES protocol don't need |
86 # any numerical ids generated, so we skip them altogether. | 77 # any numerical ids generated, so we skip them altogether. |
87 # This is accomplished by setting the flag 'generateid' to false | 78 # This is accomplished by setting the flag 'generateid' to false |
88 # in the GRD file. | 79 # in the GRD file. |
89 if 'generateid' in item.attrs: | 80 if 'generateid' in item.attrs: |
90 if item.attrs['generateid'] == 'false': | 81 if item.attrs['generateid'] == 'false': |
91 return '' | 82 return '' |
92 | 83 |
93 text_ids = item.GetTextualIds() | 84 text_ids = item.GetTextualIds() |
94 | 85 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 self._VerifyId(id, tid, | 162 self._VerifyId(id, tid, |
172 'Wanted to make numeric value for ID %s (%d) follow the numeric value
of\n' | 163 'Wanted to make numeric value for ID %s (%d) follow the numeric value
of\n' |
173 'the previous ID in the .grd file, but it was already used.' % (tid, i
d)) | 164 'the previous ID in the .grd file, but it was already used.' % (tid, i
d)) |
174 | 165 |
175 if tid not in self.ids_.values(): | 166 if tid not in self.ids_.values(): |
176 self.ids_[id] = tid | 167 self.ids_[id] = tid |
177 self.tids_[tid] = id | 168 self.tids_[tid] = id |
178 lines.append('#define %s %d\n' % (tid, id)) | 169 lines.append('#define %s %d\n' % (tid, id)) |
179 return ''.join(lines) | 170 return ''.join(lines) |
180 | 171 |
OLD | NEW |