diff options
author | thakis@chromium.org <thakis@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-05-14 04:32:56 +0000 |
---|---|---|
committer | thakis@chromium.org <thakis@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-05-14 04:32:56 +0000 |
commit | 21afaa9258b7da139d17b249a53a2068e6367fa5 (patch) | |
tree | 4fd0f58afcce1e8b959f0156b751a05e1405a4e5 /tools | |
parent | 87fcc9c82cda79d2d35c0eaf536c8780e5084f3c (diff) | |
download | chromium_src-21afaa9258b7da139d17b249a53a2068e6367fa5.zip chromium_src-21afaa9258b7da139d17b249a53a2068e6367fa5.tar.gz chromium_src-21afaa9258b7da139d17b249a53a2068e6367fa5.tar.bz2 |
grit: Don't parse <messages> and children in --inputs and --outputs mode.
This speeds up `gclient runhooks` by 2 seconds (~10%) on my system.
`gclient runhooks` runs grit_info in inputs and outputs mode about 20
times each. Interestingly, the speedup comes from making a single
invocation each about 1s faster (for the grd file that contains almost
all our strings).
BUG=82230
TEST=None
Review URL: http://codereview.chromium.org/6973066
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@85374 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools')
-rw-r--r-- | tools/grit/grit/grd_reader.py | 45 | ||||
-rwxr-xr-x | tools/grit/grit_info.py | 6 |
2 files changed, 35 insertions, 16 deletions
diff --git a/tools/grit/grit/grd_reader.py b/tools/grit/grit/grd_reader.py index 82d06d9..9e72e38 100644 --- a/tools/grit/grit/grd_reader.py +++ b/tools/grit/grit/grd_reader.py @@ -24,7 +24,8 @@ class StopParsingException(Exception): class GrdContentHandler(xml.sax.handler.ContentHandler): - def __init__(self, stop_after=None, debug=False, defines=None): + def __init__(self, + stop_after=None, debug=False, defines=None, tags_to_ignore=None): # Invariant of data: # 'root' is the root of the parse tree being created, or None if we haven't # parsed out any elements. @@ -36,17 +37,28 @@ class GrdContentHandler(xml.sax.handler.ContentHandler): self.stop_after = stop_after self.debug = debug self.defines = defines + self.tags_to_ignore = tags_to_ignore or set() + self.ignore_depth = 0 def startElement(self, name, attrs): assert not self.root or len(self.stack) > 0 + if name in self.tags_to_ignore: + self.ignore_depth += 1 + if self.debug: + print "Ignoring element %s and its children" % name + + if self.ignore_depth != 0: + return + if self.debug: attr_list = [] for attr in attrs.getNames(): attr_list.append('%s="%s"' % (attr, attrs.getValue(attr))) if len(attr_list) == 0: attr_list = ['(none)'] attr_list = ' '.join(attr_list) - print "Starting parsing of element %s with attributes %r" % (name, attr_list) + print ("Starting parsing of element %s with attributes %r" % + (name, attr_list)) typeattr = None if 'type' in attrs.getNames(): @@ -72,18 +84,23 @@ class GrdContentHandler(xml.sax.handler.ContentHandler): node.HandleAttribute(attr, attrs.getValue(attr)) def endElement(self, name): - if self.debug: - print "End parsing of element %s" % name - # Pop - self.stack[-1].EndParsing() - assert len(self.stack) > 0 - self.stack = self.stack[:-1] - if self.stop_after and name == self.stop_after: - raise StopParsingException() + if self.ignore_depth == 0: + if self.debug: + print "End parsing of element %s" % name + # Pop + self.stack[-1].EndParsing() + assert len(self.stack) > 0 + self.stack = self.stack[:-1] + if self.stop_after and name == self.stop_after: + raise StopParsingException() + + if name in self.tags_to_ignore: + self.ignore_depth -= 1 def characters(self, content): - if self.stack[-1]: - self.stack[-1].AppendContent(content) + if self.ignore_depth == 0: + if self.stack[-1]: + self.stack[-1].AppendContent(content) def ignorableWhitespace(self, whitespace): # TODO(joi) This is not supported by expat. Should use a different XML parser? @@ -92,7 +109,7 @@ class GrdContentHandler(xml.sax.handler.ContentHandler): def Parse(filename_or_stream, dir=None, flexible_root=False, stop_after=None, debug=False, first_id_filename=None, - defines=None): + defines=None, tags_to_ignore=None): '''Parses a GRD file into a tree of nodes (from grit.node). If flexible_root is False, the root node must be a <grit> element. Otherwise @@ -127,7 +144,7 @@ def Parse(filename_or_stream, dir=None, flexible_root=False, grit.exception.Parsing ''' handler = GrdContentHandler(stop_after=stop_after, debug=debug, - defines=defines) + defines=defines, tags_to_ignore=tags_to_ignore) try: xml.sax.parse(filename_or_stream, handler) except StopParsingException: diff --git a/tools/grit/grit_info.py b/tools/grit/grit_info.py index 55119ad..c0e0ed8 100755 --- a/tools/grit/grit_info.py +++ b/tools/grit/grit_info.py @@ -16,7 +16,8 @@ from grit import util def Outputs(filename, defines): - grd = grd_reader.Parse(filename, defines=defines) + grd = grd_reader.Parse( + filename, defines=defines, tags_to_ignore=set(['messages'])) target = [] lang_folders = {} @@ -47,7 +48,8 @@ def Outputs(filename, defines): def Inputs(filename, defines): - grd = grd_reader.Parse(filename, debug=False, defines=defines) + grd = grd_reader.Parse( + filename, debug=False, defines=defines, tags_to_ignore=set(['messages'])) files = [] for node in grd: if (node.name == 'structure' or node.name == 'skeleton' or |