Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(367)

Side by Side Diff: tools/grit/grit/format/rc_header.py

Issue 2690263004: Add option to GRIT to provide a resource ordering input file. (Closed)
Patch Set: Removed Mac resource ordering file. Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 from grit import exception 9 from grit import exception
10 from grit import util 10 from grit import util
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 for item in root.ActiveDescendants(): 70 for item in root.ActiveDescendants():
71 if isinstance(item, message.MessageNode): 71 if isinstance(item, message.MessageNode):
72 with item: 72 with item:
73 for tid in item.GetTextualIds(): 73 for tid in item.GetTextualIds():
74 if tid in tids and tid not in seen: 74 if tid in tids and tid not in seen:
75 seen.add(tid) 75 seen.add(tid)
76 yield rc_header_format.format(textual_id=tid,numeric_id=tids[tid]) 76 yield rc_header_format.format(textual_id=tid,numeric_id=tids[tid])
77 77
78 78
79 _cached_ids = {} 79 _cached_ids = {}
80 _predetermined_tids = {}
Nico 2017/02/17 21:44:08 nit: python style guide says 2 blank lines between
Alexei Svitkine (slow) 2017/02/17 22:07:51 Done.
81
82 def SetPredeterminedIdsFile(predetermined_ids_file):
83 global _predetermined_tids
84 if predetermined_ids_file:
85 _predetermined_tids = _ReadIdsFromFile(predetermined_ids_file)
86 else:
87 _predetermined_tids = {}
88
89
90 def _ReadIdsFromFile(path):
91 with open(path, "r") as f:
92 content = f.readlines()
93 tids = {} # Maps textual id to numeric id
94 for line in content:
95 tid, id = line.split()
96 tids[tid] = int(id)
97 return tids
80 98
81 99
82 def GetIds(root): 100 def GetIds(root):
83 '''Return a dictionary mapping textual ids to numeric ids for the given tree. 101 '''Return a dictionary mapping textual ids to numeric ids for the given tree.
84 102
85 Args: 103 Args:
86 root: A GritNode. 104 root: A GritNode.
87 ''' 105 '''
106 global _cached_ids
107 global _predetermined_tids
88 # TODO(benrg): Since other formatters use this, it might make sense to move it 108 # TODO(benrg): Since other formatters use this, it might make sense to move it
89 # and _ComputeIds to GritNode and store the cached ids as an attribute. On the 109 # and _ComputeIds to GritNode and store the cached ids as an attribute. On the
90 # other hand, GritNode has too much random stuff already. 110 # other hand, GritNode has too much random stuff already.
91 if root not in _cached_ids: 111 if root not in _cached_ids:
92 _cached_ids[root] = _ComputeIds(root) 112 _cached_ids[root] = _ComputeIds(root, _predetermined_tids)
93 return _cached_ids[root] 113 return _cached_ids[root]
94 114
95 115
96 def _ComputeIds(root): 116 def _ComputeIds(root, predetermined_tids):
97 from grit.node import empty, include, message, misc, structure 117 from grit.node import empty, include, message, misc, structure
98 118
99 ids = {} # Maps numeric id to textual id 119 ids = {} # Maps numeric id to textual id
100 tids = {} # Maps textual id to numeric id 120 tids = {} # Maps textual id to numeric id
101 id_reasons = {} # Maps numeric id to text id and a human-readable explanation 121 id_reasons = {} # Maps numeric id to text id and a human-readable explanation
102 group = None 122 group = None
103 last_id = None 123 last_id = None
104 124
125 predetermined_ids = {}
126 if predetermined_ids:
Nico 2017/02/17 21:44:08 given the previous lien, won't predetermined_ids a
Alexei Svitkine (slow) 2017/02/17 22:07:51 Woops, this is meant to be predetermined_tids orig
127 for tid, id in predetermined_tids.iteritems():
128 predetermined_ids[id] = tid
129
105 for item in root: 130 for item in root:
106 if isinstance(item, empty.GroupingNode): 131 if isinstance(item, empty.GroupingNode):
107 # Note: this won't work if any GroupingNode can be contained inside 132 # Note: this won't work if any GroupingNode can be contained inside
108 # another. 133 # another.
109 group = item 134 group = item
110 last_id = None 135 last_id = None
111 continue 136 continue
112 137
113 assert not item.GetTextualIds() or isinstance(item, 138 assert not item.GetTextualIds() or isinstance(item,
114 (include.IncludeNode, message.MessageNode, 139 (include.IncludeNode, message.MessageNode,
115 misc.IdentifierNode, structure.StructureNode)) 140 misc.IdentifierNode, structure.StructureNode))
116 141
117 # Resources that use the RES protocol don't need 142 # Resources that use the RES protocol don't need
118 # any numerical ids generated, so we skip them altogether. 143 # any numerical ids generated, so we skip them altogether.
119 # This is accomplished by setting the flag 'generateid' to false 144 # This is accomplished by setting the flag 'generateid' to false
120 # in the GRD file. 145 # in the GRD file.
121 if item.attrs.get('generateid', 'true') == 'false': 146 if item.attrs.get('generateid', 'true') == 'false':
122 continue 147 continue
123 148
124 for tid in item.GetTextualIds(): 149 for tid in item.GetTextualIds():
125 if util.SYSTEM_IDENTIFIERS.match(tid): 150 if util.SYSTEM_IDENTIFIERS.match(tid):
126 # Don't emit a new ID for predefined IDs 151 # Don't emit a new ID for predefined IDs
127 continue 152 continue
128 153
129 if tid in tids: 154 if tid in tids:
130 continue 155 continue
131 156
157 if predetermined_tids and tid in predetermined_tids:
158 id = predetermined_tids[tid]
159 reason = "from predetermined_tids map"
160
132 # Some identifier nodes can provide their own id, 161 # Some identifier nodes can provide their own id,
133 # and we use that id in the generated header in that case. 162 # and we use that id in the generated header in that case.
134 if hasattr(item, 'GetId') and item.GetId(): 163 elif hasattr(item, 'GetId') and item.GetId():
135 id = long(item.GetId()) 164 id = long(item.GetId())
136 reason = 'returned by GetId() method' 165 reason = 'returned by GetId() method'
137 166
138 elif ('offset' in item.attrs and group and 167 elif ('offset' in item.attrs and group and
139 group.attrs.get('first_id', '') != ''): 168 group.attrs.get('first_id', '') != ''):
140 offset_text = item.attrs['offset'] 169 offset_text = item.attrs['offset']
141 parent_text = group.attrs['first_id'] 170 parent_text = group.attrs['first_id']
142 171
143 try: 172 try:
144 offset_id = long(offset_text) 173 offset_id = long(offset_text)
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 # Don't fail when 'offset' is specified, as the base and the 0th 219 # Don't fail when 'offset' is specified, as the base and the 0th
191 # offset will have the same ID. 220 # offset will have the same ID.
192 if id in id_reasons and not 'offset' in item.attrs: 221 if id in id_reasons and not 'offset' in item.attrs:
193 raise exception.IdRangeOverlap('ID %d was assigned to both %s and %s.' 222 raise exception.IdRangeOverlap('ID %d was assigned to both %s and %s.'
194 % (id, id_reasons[id], reason)) 223 % (id, id_reasons[id], reason))
195 224
196 if id < 101: 225 if id < 101:
197 print ('WARNING: Numeric resource IDs should be greater than 100 to\n' 226 print ('WARNING: Numeric resource IDs should be greater than 100 to\n'
198 'avoid conflicts with system-defined resource IDs.') 227 'avoid conflicts with system-defined resource IDs.')
199 228
229 if tid not in predetermined_tids and id in predetermined_ids:
230 raise exception.IdRangeOverlap('ID %d overlaps between %s and %s'
231 % (id, tid, predetermined_ids[tid]))
232
200 ids[id] = tid 233 ids[id] = tid
201 tids[tid] = id 234 tids[tid] = id
202 id_reasons[id] = reason 235 id_reasons[id] = reason
203 236
204 return tids 237 return tids
OLDNEW
« no previous file with comments | « no previous file | tools/grit/grit/format/rc_header_unittest.py » ('j') | tools/grit/grit/tool/build.py » ('J')

Powered by Google App Engine
This is Rietveld 408576698