OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
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. | |
5 | |
6 '''The <output> and <file> elements. | |
7 ''' | |
8 | |
9 import os | |
10 | |
11 import grit.format.rc_header | |
12 | |
13 from grit import xtb_reader | |
14 from grit.node import base | |
15 | |
16 | |
17 class FileNode(base.Node): | |
18 '''A <file> element.''' | |
19 | |
20 def __init__(self): | |
21 super(FileNode, self).__init__() | |
22 self.re = None | |
23 self.should_load_ = True | |
24 | |
25 def IsTranslation(self): | |
26 return True | |
27 | |
28 def GetLang(self): | |
29 return self.attrs['lang'] | |
30 | |
31 def DisableLoading(self): | |
32 self.should_load_ = False | |
33 | |
34 def MandatoryAttributes(self): | |
35 return ['path', 'lang'] | |
36 | |
37 def RunPostSubstitutionGatherer(self, debug=False): | |
38 if not self.should_load_: | |
39 return | |
40 | |
41 root = self.GetRoot() | |
42 defs = getattr(root, 'defines', {}) | |
43 target_platform = getattr(root, 'target_platform', '') | |
44 | |
45 xtb_file = open(self.ToRealPath(self.GetInputPath())) | |
46 try: | |
47 lang = xtb_reader.Parse(xtb_file, | |
48 self.UberClique().GenerateXtbParserCallback( | |
49 self.attrs['lang'], debug=debug), | |
50 defs=defs, | |
51 target_platform=target_platform) | |
52 except: | |
53 print "Exception during parsing of %s" % self.GetInputPath() | |
54 raise | |
55 # Translation console uses non-standard language codes 'iw' and 'no' for | |
56 # Hebrew and Norwegian Bokmal instead of 'he' and 'nb' used in Chrome. | |
57 # Note that some Chrome's .grd still use 'no' instead of 'nb', but 'nb' is | |
58 # always used for generated .pak files. | |
59 ALTERNATIVE_LANG_CODE_MAP = { 'he': 'iw', 'nb': 'no' } | |
60 assert (lang == self.attrs['lang'] or | |
61 lang == ALTERNATIVE_LANG_CODE_MAP[self.attrs['lang']]), ( | |
62 'The XTB file you reference must contain messages in the language ' | |
63 'specified\nby the \'lang\' attribute.') | |
64 | |
65 def GetInputPath(self): | |
66 return os.path.expandvars(self.attrs['path']) | |
67 | |
68 | |
69 class OutputNode(base.Node): | |
70 '''An <output> element.''' | |
71 | |
72 def MandatoryAttributes(self): | |
73 return ['filename', 'type'] | |
74 | |
75 def DefaultAttributes(self): | |
76 return { | |
77 'lang' : '', # empty lang indicates all languages | |
78 'language_section' : 'neutral', # defines a language neutral section | |
79 'context' : '', | |
80 'fallback_to_default_layout' : 'true', | |
81 } | |
82 | |
83 def GetType(self): | |
84 return self.attrs['type'] | |
85 | |
86 def GetLanguage(self): | |
87 '''Returns the language ID, default 'en'.''' | |
88 return self.attrs['lang'] | |
89 | |
90 def GetContext(self): | |
91 return self.attrs['context'] | |
92 | |
93 def GetFilename(self): | |
94 return self.attrs['filename'] | |
95 | |
96 def GetOutputFilename(self): | |
97 path = None | |
98 if hasattr(self, 'output_filename'): | |
99 path = self.output_filename | |
100 else: | |
101 path = self.attrs['filename'] | |
102 return os.path.expandvars(path) | |
103 | |
104 def GetFallbackToDefaultLayout(self): | |
105 return self.attrs['fallback_to_default_layout'].lower() == 'true' | |
106 | |
107 def _IsValidChild(self, child): | |
108 return isinstance(child, EmitNode) | |
109 | |
110 class EmitNode(base.ContentNode): | |
111 ''' An <emit> element.''' | |
112 | |
113 def DefaultAttributes(self): | |
114 return { 'emit_type' : 'prepend'} | |
115 | |
116 def GetEmitType(self): | |
117 '''Returns the emit_type for this node. Default is 'append'.''' | |
118 return self.attrs['emit_type'] | |
119 | |
OLD | NEW |