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 '''Miscellaneous node types. | 6 '''Miscellaneous node types. |
7 ''' | 7 ''' |
8 | 8 |
9 import os.path | 9 import os.path |
10 import re | 10 import re |
(...skipping 11 matching lines...) Expand all Loading... | |
22 | 22 |
23 def _ReadFirstIdsFromFile(filename, defines): | 23 def _ReadFirstIdsFromFile(filename, defines): |
24 '''Read the starting resource id values from |filename|. We also | 24 '''Read the starting resource id values from |filename|. We also |
25 expand variables of the form <(FOO) based on defines passed in on | 25 expand variables of the form <(FOO) based on defines passed in on |
26 the command line. | 26 the command line. |
27 | 27 |
28 Returns a tuple, the absolute path of SRCDIR followed by the | 28 Returns a tuple, the absolute path of SRCDIR followed by the |
29 first_ids dictionary. | 29 first_ids dictionary. |
30 ''' | 30 ''' |
31 first_ids_dict = eval(open(filename).read()) | 31 first_ids_dict = eval(open(filename).read()) |
32 | |
33 # TODO(joi@chromium.org): It might make sense to make this a | |
34 # parameter of the .grd file rather than of the resource_ids file. | |
35 src_root_dir = os.path.abspath(os.path.join(os.path.dirname(filename), | 32 src_root_dir = os.path.abspath(os.path.join(os.path.dirname(filename), |
36 first_ids_dict['SRCDIR'])) | 33 first_ids_dict['SRCDIR'])) |
37 | 34 |
38 def ReplaceVariable(matchobj): | 35 def ReplaceVariable(matchobj): |
39 for key, value in defines.iteritems(): | 36 for key, value in defines.iteritems(): |
40 if matchobj.group(1) == key: | 37 if matchobj.group(1) == key: |
41 value = os.path.abspath(value)[len(src_root_dir) + 1:] | 38 value = os.path.abspath(value)[len(src_root_dir) + 1:] |
42 return value | 39 return value |
43 return '' | 40 return '' |
44 | 41 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
130 base.Node.__init__(self) | 127 base.Node.__init__(self) |
131 self.output_language = '' | 128 self.output_language = '' |
132 self.defines = {} | 129 self.defines = {} |
133 | 130 |
134 def _IsValidChild(self, child): | 131 def _IsValidChild(self, child): |
135 from grit.node import empty | 132 from grit.node import empty |
136 return isinstance(child, (ReleaseNode, empty.TranslationsNode, | 133 return isinstance(child, (ReleaseNode, empty.TranslationsNode, |
137 empty.OutputsNode)) | 134 empty.OutputsNode)) |
138 | 135 |
139 def _IsValidAttribute(self, name, value): | 136 def _IsValidAttribute(self, name, value): |
140 if name not in ['base_dir', 'source_lang_id', | 137 if name not in ['base_dir', 'first_ids_file', 'source_lang_id', |
141 'latest_public_release', 'current_release', | 138 'latest_public_release', 'current_release', |
142 'enc_check', 'tc_project']: | 139 'enc_check', 'tc_project']: |
143 return False | 140 return False |
144 if name in ['latest_public_release', 'current_release'] and value.strip( | 141 if name in ['latest_public_release', 'current_release'] and value.strip( |
145 '0123456789') != '': | 142 '0123456789') != '': |
146 return False | 143 return False |
147 return True | 144 return True |
148 | 145 |
149 def MandatoryAttributes(self): | 146 def MandatoryAttributes(self): |
150 return ['latest_public_release', 'current_release'] | 147 return ['latest_public_release', 'current_release'] |
151 | 148 |
152 def DefaultAttributes(self): | 149 def DefaultAttributes(self): |
153 return { | 150 return { |
154 'base_dir' : '.', | 151 'base_dir' : '.', |
152 'first_ids_file': '', | |
155 'source_lang_id' : 'en', | 153 'source_lang_id' : 'en', |
156 'enc_check' : constants.ENCODING_CHECK, | 154 'enc_check' : constants.ENCODING_CHECK, |
157 'tc_project' : 'NEED_TO_SET_tc_project_ATTRIBUTE', | 155 'tc_project' : 'NEED_TO_SET_tc_project_ATTRIBUTE', |
158 } | 156 } |
159 | 157 |
160 def EndParsing(self): | 158 def EndParsing(self): |
161 base.Node.EndParsing(self) | 159 base.Node.EndParsing(self) |
162 if (int(self.attrs['latest_public_release']) | 160 if (int(self.attrs['latest_public_release']) |
163 > int(self.attrs['current_release'])): | 161 > int(self.attrs['current_release'])): |
164 raise exception.Parsing('latest_public_release cannot have a greater ' | 162 raise exception.Parsing('latest_public_release cannot have a greater ' |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
240 if hasattr(self, 'base_dir'): | 238 if hasattr(self, 'base_dir'): |
241 return self.base_dir | 239 return self.base_dir |
242 else: | 240 else: |
243 return self.GetOriginalBaseDir() | 241 return self.GetOriginalBaseDir() |
244 | 242 |
245 def GetOriginalBaseDir(self): | 243 def GetOriginalBaseDir(self): |
246 '''Returns the base directory, as set in the .grd file. | 244 '''Returns the base directory, as set in the .grd file. |
247 ''' | 245 ''' |
248 return self.attrs['base_dir'] | 246 return self.attrs['base_dir'] |
249 | 247 |
248 def GetFirstIdsFile(self): | |
249 '''Returns a usable path to the first_ids file, if set, otherwise | |
250 returns None. | |
251 | |
252 The first_ids_file attribute is by default relative to the | |
253 base_dir of the .grd file, but may be prefixed by GRIT_DIR/, | |
254 which makes it relative to the directory of grit.py | |
255 (e.g. GRIT_DIR/../gritsettings/resource_ids). | |
256 ''' | |
257 if not self.attrs['first_ids_file']: | |
258 return None | |
259 else: | |
tony
2012/03/28 17:31:56
Nit: Remove the 'else' and unindent the rest of th
Jói
2012/03/29 11:36:15
Done.
| |
260 path = self.attrs['first_ids_file'] | |
261 GRIT_DIR_PREFIX = 'GRIT_DIR/' | |
262 if path.startswith(GRIT_DIR_PREFIX): | |
263 return util.PathFromRoot(path[len(GRIT_DIR_PREFIX):]) | |
264 else: | |
265 return self.ToRealPath(path) | |
266 | |
250 def _CollectOutputFiles(self, nodes, output_files): | 267 def _CollectOutputFiles(self, nodes, output_files): |
251 '''Recursively filters the list of nodes that may contain other lists | 268 '''Recursively filters the list of nodes that may contain other lists |
252 in <if> nodes, and collects all the nodes that are not enclosed by | 269 in <if> nodes, and collects all the nodes that are not enclosed by |
253 unsatisfied <if> conditionals and not <if> nodes themselves. | 270 unsatisfied <if> conditionals and not <if> nodes themselves. |
254 | 271 |
255 Args: | 272 Args: |
256 nodes: The list of nodes to filter. | 273 nodes: The list of nodes to filter. |
257 output_files: The list of satisfying nodes. | 274 output_files: The list of satisfying nodes. |
258 ''' | 275 ''' |
259 for node in nodes: | 276 for node in nodes: |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
297 else: | 314 else: |
298 return super(type(self), self).ItemFormatter(t) | 315 return super(type(self), self).ItemFormatter(t) |
299 | 316 |
300 def SetOutputContext(self, output_language, defines): | 317 def SetOutputContext(self, output_language, defines): |
301 self.output_language = output_language | 318 self.output_language = output_language |
302 self.defines = defines | 319 self.defines = defines |
303 | 320 |
304 def SetDefines(self, defines): | 321 def SetDefines(self, defines): |
305 self.defines = defines | 322 self.defines = defines |
306 | 323 |
307 def AssignFirstIds(self, filename_or_stream, first_id_filename, defines): | 324 def AssignFirstIds(self, filename_or_stream, defines): |
308 '''Assign first ids to each grouping node based on values from | 325 '''Assign first ids to each grouping node based on values from the |
309 tools/grit/resource_ids.''' | 326 first_ids file (if specified on the <grit> node). |
327 ''' | |
310 # If the input is a stream, then we're probably in a unit test and | 328 # If the input is a stream, then we're probably in a unit test and |
311 # should skip this step. | 329 # should skip this step. |
312 if type(filename_or_stream) not in (str, unicode): | 330 if type(filename_or_stream) not in (str, unicode): |
313 return | 331 return |
314 | 332 |
315 # TODO(joi@chromium.org): Get rid of this hack by making it | 333 # Nothing to do if the first_ids_filename attribute isn't set. |
316 # possible to specify the resource_ids file to use as an attribute | 334 first_ids_filename = self.GetFirstIdsFile() |
317 # of the <grit> node in the .grd file, and doing so in all Chrome | 335 if not first_ids_filename: |
318 # .grd files. | 336 return |
319 # | |
320 # For now, by default, we use the the file | |
321 # ../gritsettings/resource_ids relative to grit.py. | |
322 if not first_id_filename: | |
323 first_id_filename = os.path.join( | |
324 os.path.dirname(__file__), | |
325 '..', '..', '..', | |
326 'gritsettings', 'resource_ids') | |
327 | 337 |
328 first_ids = None | 338 src_root_dir, first_ids = _ReadFirstIdsFromFile(first_ids_filename, |
339 defines) | |
329 from grit.node import empty | 340 from grit.node import empty |
330 for node in self.inorder(): | 341 for node in self.inorder(): |
331 if isinstance(node, empty.GroupingNode): | 342 if isinstance(node, empty.GroupingNode): |
332 if not first_ids: | |
333 src_root_dir, first_ids = _ReadFirstIdsFromFile(first_id_filename, | |
334 defines) | |
335 filename = os.path.abspath(filename_or_stream)[ | 343 filename = os.path.abspath(filename_or_stream)[ |
336 len(src_root_dir) + 1:] | 344 len(src_root_dir) + 1:] |
337 filename = filename.replace('\\', '/') | 345 filename = filename.replace('\\', '/') |
338 | 346 |
339 # TODO(joi@chromium.org): Generalize this; users other than | |
340 # Chrome might want to use the first_id attribute; could check | |
341 # for first_ids == None to indicate not loaded, first_ids == | |
342 # {} to indicate tried to load but found no resource_ids file. | |
343 if node.attrs['first_id'] != '': | 347 if node.attrs['first_id'] != '': |
344 raise Exception("Don't set the first_id attribute, update " | 348 raise Exception( |
345 "%s instead." % first_id_filename) | 349 "Don't set the first_id attribute when using the first_ids_file " |
350 "attribute on the <grit> node, update %s instead." % | |
351 first_ids_filename) | |
346 | 352 |
347 try: | 353 try: |
348 id_list = first_ids[filename][node.name] | 354 id_list = first_ids[filename][node.name] |
349 except KeyError, e: | 355 except KeyError, e: |
350 print '-' * 78 | 356 print '-' * 78 |
351 print 'Resource id not set for %s (%s)!' % (filename, node.name) | 357 print 'Resource id not set for %s (%s)!' % (filename, node.name) |
352 print ('Please update %s to include an entry for %s. See the ' | 358 print ('Please update %s to include an entry for %s. See the ' |
353 'comments in resource_ids for information on why you need to ' | 359 'comments in resource_ids for information on why you need to ' |
354 'update that file.' % (first_id_filename, filename)) | 360 'update that file.' % (first_ids_filename, filename)) |
355 print '-' * 78 | 361 print '-' * 78 |
356 raise e | 362 raise e |
357 | 363 |
358 try: | 364 try: |
359 node.attrs['first_id'] = str(id_list.pop(0)) | 365 node.attrs['first_id'] = str(id_list.pop(0)) |
360 except IndexError, e: | 366 except IndexError, e: |
361 raise Exception('Please update %s and add a first id for %s (%s).' | 367 raise Exception('Please update %s and add a first id for %s (%s).' |
362 % (first_id_filename, filename, node.name)) | 368 % (first_ids_filename, filename, node.name)) |
363 | 369 |
364 | 370 |
365 class IdentifierNode(base.Node): | 371 class IdentifierNode(base.Node): |
366 '''A node for specifying identifiers that should appear in the resource | 372 '''A node for specifying identifiers that should appear in the resource |
367 header file, and be unique amongst all other resource identifiers, but don't | 373 header file, and be unique amongst all other resource identifiers, but don't |
368 have any other attributes or reference any resources. | 374 have any other attributes or reference any resources. |
369 ''' | 375 ''' |
370 | 376 |
371 def MandatoryAttributes(self): | 377 def MandatoryAttributes(self): |
372 return ['name'] | 378 return ['name'] |
(...skipping 18 matching lines...) Expand all Loading... | |
391 by parameters of the same name. | 397 by parameters of the same name. |
392 ''' | 398 ''' |
393 node = IdentifierNode() | 399 node = IdentifierNode() |
394 node.StartParsing('identifier', parent) | 400 node.StartParsing('identifier', parent) |
395 node.HandleAttribute('name', name) | 401 node.HandleAttribute('name', name) |
396 node.HandleAttribute('id', id) | 402 node.HandleAttribute('id', id) |
397 node.HandleAttribute('comment', comment) | 403 node.HandleAttribute('comment', comment) |
398 node.EndParsing() | 404 node.EndParsing() |
399 return node | 405 return node |
400 Construct = staticmethod(Construct) | 406 Construct = staticmethod(Construct) |
OLD | NEW |