Skip to content

Commit

Permalink
Allow GRD reader to process a custom GRDP file stored in a folder dif…
Browse files Browse the repository at this point in the history
…ferent from its parent GRD file.

As of today, GRIT assumes that files referenced from the GRD file
are relative to a path stored in the root <grit> node. This change
fixes it by allowing grd reader to process a GRDP file stored in
an arbitrary path as long as the file exists.

BUG=729620
TEST=python tools/grit/grit.py unit
R=benrg@chromium.org,joi@chromium.org,thakis@chromium.org

Review-Url: https://codereview.chromium.org/2923103002
Cr-Commit-Position: refs/heads/master@{#482829}
  • Loading branch information
xiaoshu authored and Commit Bot committed Jun 28, 2017
1 parent 11b33cc commit d93265c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,7 @@ Xiang Long <xiang.long@intel.com>
Xiangze Zhang <xiangze.zhang@intel.com>
Xiaofeng Zhang <xiaofeng.zhang@intel.com>
Xiaolei Yu <dreifachstein@gmail.com>
Xiaoshu Zhang <xiaoshu@amazon.com>
Xiaoyin Liu <xiaoyin.l@outlook.com>
Xinchao He <hexinchao@gmail.com>
Xing Zhang <xzhang@adobe.com>
Expand Down
7 changes: 0 additions & 7 deletions tools/grit/grit/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,6 @@ class TooManyExamples(Parsing):
pass


class GotPathExpectedFilenameOnly(Parsing):
'''The 'filename' attribute of <output> and the 'file' attribute of <part>
must be bare filenames, not paths.
'''
pass


class FileNotFound(Parsing):
'''The resource file was not found.
'''
Expand Down
11 changes: 4 additions & 7 deletions tools/grit/grit/grd_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,10 @@ def endElement(self, name):
partnode = self.stack[-1]
partnode.started_inclusion = True
# Add the contents of the sub-grd file as children of the <part> node.
partname = partnode.GetInputPath()
if os.path.dirname(partname):
# TODO(benrg): Remove this limitation. (The problem is that GRIT
# assumes that files referenced from the GRD file are relative to
# a path stored in the root <grit> node.)
raise exception.GotPathExpectedFilenameOnly()
partname = os.path.join(self.dir, partname)
partname = os.path.join(self.dir, partnode.GetInputPath())
# Check the GRDP file exists.
if not os.path.exists(partname):
raise exception.FileNotFound()
# Exceptions propagate to the handler in grd_reader.Parse().
xml.sax.parse(partname, GrdPartContentHandler(self))

Expand Down
18 changes: 16 additions & 2 deletions tools/grit/grit/grd_reader_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,13 @@ def testUseNameForIdWithIfElse(self):
self.failUnless(hello.GetCliques()[0].GetId() == 'IDS_HELLO')

def testPartInclusion(self):
arbitrary_path_grd = u'''\
<grit-part>
<message name="IDS_TEST5" desc="test5">test5</message>
</grit-part>'''
arbitrary_path_grd_file = os.path.join(
util.TempDir({'arbitrary_path.grp': arbitrary_path_grd}).GetPath(),
'arbitrary_path.grp')
top_grd = u'''\
<grit latest_public_release="2" current_release="3">
<release seq="3">
Expand All @@ -219,9 +226,10 @@ def testPartInclusion(self):
test
</message>
<part file="sub.grp" />
<part file="%s" />
</messages>
</release>
</grit>'''
</grit>''' % arbitrary_path_grd_file
sub_grd = u'''\
<grit-part>
<message name="IDS_TEST2" desc="test2">test2</message>
Expand Down Expand Up @@ -252,9 +260,14 @@ def testPartInclusion(self):
test3
</message>
</part>
<part file="%s">
<message desc="test5" name="IDS_TEST5">
test5
</message>
</part>
</messages>
</release>
</grit>'''
</grit>''' % arbitrary_path_grd_file
with util.TempDir({'sub.grp': sub_grd,
'subsub.grp': subsub_grd}) as temp_dir:
output = grd_reader.Parse(StringIO.StringIO(top_grd), temp_dir.GetPath())
Expand All @@ -272,6 +285,7 @@ def testPartInclusionFailure(self):
(exception.UnexpectedContent, u'<part file="x">fnord</part>'),
(exception.UnexpectedChild,
u'<part file="x"><output filename="x" type="y" /></part>'),
(exception.FileNotFound, u'<part file="yet_created_x" />'),
]
for raises, data in part_failures:
data = StringIO.StringIO(template % data)
Expand Down

0 comments on commit d93265c

Please sign in to comment.