| 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 |
| 11 import xml.sax | 11 import xml.sax |
| 12 import xml.sax.handler | 12 import xml.sax.handler |
| 13 | 13 |
| 14 from grit import exception | 14 from grit import exception |
| 15 from grit import util | 15 from grit import util |
| 16 from grit.format import rc_header |
| 16 from grit.node import base | 17 from grit.node import base |
| 17 from grit.node import mapping | 18 from grit.node import mapping |
| 18 from grit.node import misc | 19 from grit.node import misc |
| 19 | 20 |
| 20 | 21 |
| 21 class StopParsingException(Exception): | 22 class StopParsingException(Exception): |
| 22 '''An exception used to stop parsing.''' | 23 '''An exception used to stop parsing.''' |
| 23 pass | 24 pass |
| 24 | 25 |
| 25 | 26 |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 self.parent.endElement(name) | 133 self.parent.endElement(name) |
| 133 | 134 |
| 134 def characters(self, content): | 135 def characters(self, content): |
| 135 self.parent.characters(content) | 136 self.parent.characters(content) |
| 136 | 137 |
| 137 def ignorableWhitespace(self, whitespace): | 138 def ignorableWhitespace(self, whitespace): |
| 138 self.parent.ignorableWhitespace(whitespace) | 139 self.parent.ignorableWhitespace(whitespace) |
| 139 | 140 |
| 140 | 141 |
| 141 def Parse(filename_or_stream, dir=None, stop_after=None, first_ids_file=None, | 142 def Parse(filename_or_stream, dir=None, stop_after=None, first_ids_file=None, |
| 142 debug=False, defines=None, tags_to_ignore=None, target_platform=None): | 143 debug=False, defines=None, tags_to_ignore=None, target_platform=None, |
| 144 predetermined_ids_file=None): |
| 143 '''Parses a GRD file into a tree of nodes (from grit.node). | 145 '''Parses a GRD file into a tree of nodes (from grit.node). |
| 144 | 146 |
| 145 If filename_or_stream is a stream, 'dir' should point to the directory | 147 If filename_or_stream is a stream, 'dir' should point to the directory |
| 146 notionally containing the stream (this feature is only used in unit tests). | 148 notionally containing the stream (this feature is only used in unit tests). |
| 147 | 149 |
| 148 If 'stop_after' is provided, the parsing will stop once the first node | 150 If 'stop_after' is provided, the parsing will stop once the first node |
| 149 with this name has been fully parsed (including all its contents). | 151 with this name has been fully parsed (including all its contents). |
| 150 | 152 |
| 151 If 'debug' is true, lots of information about the parsing events will be | 153 If 'debug' is true, lots of information about the parsing events will be |
| 152 printed out during parsing of the file. | 154 printed out during parsing of the file. |
| 153 | 155 |
| 154 If 'first_ids_file' is non-empty, it is used to override the setting for the | 156 If 'first_ids_file' is non-empty, it is used to override the setting for the |
| 155 first_ids_file attribute of the <grit> root node. Note that the first_ids_file | 157 first_ids_file attribute of the <grit> root node. Note that the first_ids_file |
| 156 parameter should be relative to the cwd, even though the first_ids_file | 158 parameter should be relative to the cwd, even though the first_ids_file |
| 157 attribute of the <grit> node is relative to the grd file. | 159 attribute of the <grit> node is relative to the grd file. |
| 158 | 160 |
| 159 If 'target_platform' is set, this is used to determine the target | 161 If 'target_platform' is set, this is used to determine the target |
| 160 platform of builds, instead of using |sys.platform|. | 162 platform of builds, instead of using |sys.platform|. |
| 161 | 163 |
| 162 Args: | 164 Args: |
| 163 filename_or_stream: './bla.xml' | 165 filename_or_stream: './bla.xml' |
| 164 dir: None (if filename_or_stream is a filename) or '.' | 166 dir: None (if filename_or_stream is a filename) or '.' |
| 165 stop_after: 'inputs' | 167 stop_after: 'inputs' |
| 166 first_ids_file: 'GRIT_DIR/../gritsettings/resource_ids' | 168 first_ids_file: 'GRIT_DIR/../gritsettings/resource_ids' |
| 167 debug: False | 169 debug: False |
| 168 defines: dictionary of defines, like {'chromeos': '1'} | 170 defines: dictionary of defines, like {'chromeos': '1'} |
| 169 target_platform: None or the value that would be returned by sys.platform | 171 target_platform: None or the value that would be returned by sys.platform |
| 170 on your target platform. | 172 on your target platform. |
| 173 predetermined_ids_file: File path to a file containing a pre-determined |
| 174 mapping from resource names to resource ids which will be used to assign |
| 175 resource ids to those resources. |
| 171 | 176 |
| 172 Return: | 177 Return: |
| 173 Subclass of grit.node.base.Node | 178 Subclass of grit.node.base.Node |
| 174 | 179 |
| 175 Throws: | 180 Throws: |
| 176 grit.exception.Parsing | 181 grit.exception.Parsing |
| 177 ''' | 182 ''' |
| 178 | 183 |
| 179 if dir is None and isinstance(filename_or_stream, types.StringType): | 184 if dir is None and isinstance(filename_or_stream, types.StringType): |
| 180 dir = util.dirname(filename_or_stream) | 185 dir = util.dirname(filename_or_stream) |
| 181 | 186 |
| 187 rc_header.SetPredeterminedIdsFile(predetermined_ids_file) |
| 182 handler = GrdContentHandler(stop_after=stop_after, debug=debug, dir=dir, | 188 handler = GrdContentHandler(stop_after=stop_after, debug=debug, dir=dir, |
| 183 defines=defines, tags_to_ignore=tags_to_ignore, | 189 defines=defines, tags_to_ignore=tags_to_ignore, |
| 184 target_platform=target_platform) | 190 target_platform=target_platform) |
| 185 try: | 191 try: |
| 186 xml.sax.parse(filename_or_stream, handler) | 192 xml.sax.parse(filename_or_stream, handler) |
| 187 except StopParsingException: | 193 except StopParsingException: |
| 188 assert stop_after | 194 assert stop_after |
| 189 pass | 195 pass |
| 190 except: | 196 except: |
| 191 if not debug: | 197 if not debug: |
| (...skipping 20 matching lines...) Expand all Loading... |
| 212 handler.root.attrs['first_ids_file'] = first_ids_file | 218 handler.root.attrs['first_ids_file'] = first_ids_file |
| 213 # Assign first ids to the nodes that don't have them. | 219 # Assign first ids to the nodes that don't have them. |
| 214 handler.root.AssignFirstIds(filename_or_stream, defines) | 220 handler.root.AssignFirstIds(filename_or_stream, defines) |
| 215 | 221 |
| 216 return handler.root | 222 return handler.root |
| 217 | 223 |
| 218 | 224 |
| 219 if __name__ == '__main__': | 225 if __name__ == '__main__': |
| 220 util.ChangeStdoutEncoding() | 226 util.ChangeStdoutEncoding() |
| 221 print unicode(Parse(sys.argv[1])) | 227 print unicode(Parse(sys.argv[1])) |
| OLD | NEW |