| 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 '''Class for reading GRD files into memory, without processing them. | 6 '''Class for reading GRD files into memory, without processing them. |
| 7 ''' | 7 ''' |
| 8 | 8 |
| 9 import os.path | 9 import os.path |
| 10 import types | 10 import types |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 if self.ignore_depth == 0: | 101 if self.ignore_depth == 0: |
| 102 if self.stack[-1]: | 102 if self.stack[-1]: |
| 103 self.stack[-1].AppendContent(content) | 103 self.stack[-1].AppendContent(content) |
| 104 | 104 |
| 105 def ignorableWhitespace(self, whitespace): | 105 def ignorableWhitespace(self, whitespace): |
| 106 # TODO(joi) This is not supported by expat. Should use a different XML par
ser? | 106 # TODO(joi) This is not supported by expat. Should use a different XML par
ser? |
| 107 pass | 107 pass |
| 108 | 108 |
| 109 | 109 |
| 110 def Parse(filename_or_stream, dir=None, flexible_root=False, | 110 def Parse(filename_or_stream, dir=None, flexible_root=False, |
| 111 stop_after=None, debug=False, first_id_filename=None, | 111 stop_after=None, first_ids_file=None, debug=False, |
| 112 defines=None, tags_to_ignore=None): | 112 defines=None, tags_to_ignore=None): |
| 113 '''Parses a GRD file into a tree of nodes (from grit.node). | 113 '''Parses a GRD file into a tree of nodes (from grit.node). |
| 114 | 114 |
| 115 If flexible_root is False, the root node must be a <grit> element. Otherwise | 115 If flexible_root is False, the root node must be a <grit> element. Otherwise |
| 116 it can be any element. The "own" directory of the file will only be fixed up | 116 it can be any element. The "own" directory of the file will only be fixed up |
| 117 if the root node is a <grit> element. | 117 if the root node is a <grit> element. |
| 118 | 118 |
| 119 'dir' should point to the directory of the input file, or be the full path | 119 'dir' should point to the directory of the input file, or be the full path |
| 120 to the input file (the filename will be stripped). | 120 to the input file (the filename will be stripped). |
| 121 | 121 |
| 122 If 'stop_after' is provided, the parsing will stop once the first node | 122 If 'stop_after' is provided, the parsing will stop once the first node |
| 123 with this name has been fully parsed (including all its contents). | 123 with this name has been fully parsed (including all its contents). |
| 124 | 124 |
| 125 If 'debug' is true, lots of information about the parsing events will be | 125 If 'debug' is true, lots of information about the parsing events will be |
| 126 printed out during parsing of the file. | 126 printed out during parsing of the file. |
| 127 | 127 |
| 128 If first_id_filename is provided, then we use the provided path instead of | 128 If 'first_ids_file' is non-empty, it is used to override the setting |
| 129 resources_id to gather the first id values for resources. | 129 for the first_ids_file attribute of the <grit> root node. |
| 130 | 130 |
| 131 Args: | 131 Args: |
| 132 filename_or_stream: './bla.xml' (must be filename if dir is None) | 132 filename_or_stream: './bla.xml' (must be filename if dir is None) |
| 133 dir: '.' or None (only if filename_or_stream is a filename) | 133 dir: '.' or None (only if filename_or_stream is a filename) |
| 134 flexible_root: True | False | 134 flexible_root: True | False |
| 135 stop_after: 'inputs' | 135 stop_after: 'inputs' |
| 136 first_ids_file: 'GRIT_DIR/../gritsettings/resource_ids' |
| 136 debug: False | 137 debug: False |
| 137 first_id_filename: None | |
| 138 defines: dictionary of defines, like {'chromeos': '1'} | 138 defines: dictionary of defines, like {'chromeos': '1'} |
| 139 | 139 |
| 140 Return: | 140 Return: |
| 141 Subclass of grit.node.base.Node | 141 Subclass of grit.node.base.Node |
| 142 | 142 |
| 143 Throws: | 143 Throws: |
| 144 grit.exception.Parsing | 144 grit.exception.Parsing |
| 145 ''' | 145 ''' |
| 146 handler = GrdContentHandler(stop_after=stop_after, debug=debug, | 146 handler = GrdContentHandler(stop_after=stop_after, debug=debug, |
| 147 defines=defines, tags_to_ignore=tags_to_ignore) | 147 defines=defines, tags_to_ignore=tags_to_ignore) |
| 148 try: | 148 try: |
| 149 xml.sax.parse(filename_or_stream, handler) | 149 xml.sax.parse(filename_or_stream, handler) |
| 150 except StopParsingException: | 150 except StopParsingException: |
| 151 assert stop_after | 151 assert stop_after |
| 152 pass | 152 pass |
| 153 except: | 153 except: |
| 154 if not debug: | 154 if not debug: |
| 155 print "parse exception: run GRIT with the -x flag to debug .grd problems" | 155 print "parse exception: run GRIT with the -x flag to debug .grd problems" |
| 156 raise | 156 raise |
| 157 | 157 |
| 158 if not flexible_root or hasattr(handler.root, 'SetOwnDir'): | 158 if not flexible_root or hasattr(handler.root, 'SetOwnDir'): |
| 159 assert isinstance(filename_or_stream, types.StringType) or dir != None | 159 assert isinstance(filename_or_stream, types.StringType) or dir != None |
| 160 if not dir: | 160 if not dir: |
| 161 dir = util.dirname(filename_or_stream) | 161 dir = util.dirname(filename_or_stream) |
| 162 if len(dir) == 0: | 162 if len(dir) == 0: |
| 163 dir = '.' | 163 dir = '.' |
| 164 # Fix up the base_dir so it is relative to the input file. | 164 # Fix up the base_dir so it is relative to the input file. |
| 165 handler.root.SetOwnDir(dir) | 165 handler.root.SetOwnDir(dir) |
| 166 | 166 |
| 167 # Assign first ids to the nodes that don't have them. | 167 if isinstance(handler.root, misc.GritNode): |
| 168 if isinstance(handler.root, misc.GritNode) and first_id_filename != '': | 168 if first_ids_file: |
| 169 handler.root.AssignFirstIds(filename_or_stream, first_id_filename, defines) | 169 handler.root.attrs['first_ids_file'] = first_ids_file |
| 170 # Assign first ids to the nodes that don't have them. |
| 171 handler.root.AssignFirstIds(filename_or_stream, defines) |
| 170 | 172 |
| 171 return handler.root | 173 return handler.root |
| 172 | 174 |
| 173 | 175 |
| 174 if __name__ == '__main__': | 176 if __name__ == '__main__': |
| 175 util.ChangeStdoutEncoding() | 177 util.ChangeStdoutEncoding() |
| 176 print unicode(Parse(sys.argv[1])) | 178 print unicode(Parse(sys.argv[1])) |
| OLD | NEW |