Commit 830ebc99 authored by Mike Frysinger's avatar Mike Frysinger Committed by Commit Bot

grit: minify json outputs

This can easily save ~1k if not ~10k+ per message file.  Looking at an
example CrOS image today, this can add up to MB of data in the rootfs.

We don't make this optional for now as it's not clear whether anyone
really cares, and it's easy enough for people to pretty print the json
files after the fact.

Bug: 852585
Change-Id: I1d2331dfc38d404ea03facd3d0a8845f32c4f981
Reviewed-on: https://chromium-review.googlesource.com/c/1320515Reviewed-by: default avatarRobert Flack <flackr@chromium.org>
Commit-Queue: Mike Frysinger <vapier@chromium.org>
Cr-Commit-Position: refs/heads/master@{#608146}
parent 0d836853
...@@ -15,15 +15,11 @@ from grit.node import message ...@@ -15,15 +15,11 @@ from grit.node import message
def Format(root, lang='en', output_dir='.'): def Format(root, lang='en', output_dir='.'):
"""Format the messages as JSON.""" """Format the messages as JSON."""
yield '{\n' yield '{'
encoder = JSONEncoder(ensure_ascii=False) encoder = JSONEncoder(ensure_ascii=False)
format = (' "%s": {\n' format = '"%s":{"message":%s%s}'
' "message": %s%s\n' placeholder_format = '"%i":{"content":"$%i"}'
' }')
placeholder_format = (' "%i": {\n'
' "content": "$%i"\n'
' }')
first = True first = True
for child in root.ActiveDescendants(): for child in root.ActiveDescendants():
if isinstance(child, message.MessageNode): if isinstance(child, message.MessageNode):
...@@ -50,15 +46,15 @@ def Format(root, lang='en', output_dir='.'): ...@@ -50,15 +46,15 @@ def Format(root, lang='en', output_dir='.'):
break break
loc_message = loc_message.replace('$%d' % i, '$%d$' % i) loc_message = loc_message.replace('$%d' % i, '$%d$' % i)
if placeholders: if placeholders:
placeholders += ',\n' placeholders += ','
placeholders += placeholder_format % (i, i) placeholders += placeholder_format % (i, i)
if not first: if not first:
yield ',\n' yield ','
first = False first = False
if placeholders: if placeholders:
placeholders = ',\n "placeholders": {\n%s\n }' % placeholders placeholders = ',"placeholders":{%s}' % placeholders
yield format % (id, loc_message, placeholders) yield format % (id, loc_message, placeholders)
yield '\n}\n' yield '}'
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
"""Unittest for chrome_messages_json.py. """Unittest for chrome_messages_json.py.
""" """
import json
import os import os
import sys import sys
if __name__ == '__main__': if __name__ == '__main__':
...@@ -20,6 +21,10 @@ from grit.tool import build ...@@ -20,6 +21,10 @@ from grit.tool import build
class ChromeMessagesJsonFormatUnittest(unittest.TestCase): class ChromeMessagesJsonFormatUnittest(unittest.TestCase):
# The default unittest diff limit is too low for our unittests.
# Allow the framework to show the full diff output all the time.
maxDiff = None
def testMessages(self): def testMessages(self):
root = util.ParseGrdForUnittest(u""" root = util.ParseGrdForUnittest(u"""
<messages> <messages>
...@@ -96,7 +101,7 @@ class ChromeMessagesJsonFormatUnittest(unittest.TestCase): ...@@ -96,7 +101,7 @@ class ChromeMessagesJsonFormatUnittest(unittest.TestCase):
} }
} }
""" """
self.assertEqual(test.strip(), output.strip()) self.assertEqual(json.loads(test), json.loads(output))
def testTranslations(self): def testTranslations(self):
root = util.ParseGrdForUnittest(""" root = util.ParseGrdForUnittest("""
...@@ -121,7 +126,7 @@ class ChromeMessagesJsonFormatUnittest(unittest.TestCase): ...@@ -121,7 +126,7 @@ class ChromeMessagesJsonFormatUnittest(unittest.TestCase):
} }
} }
""" """
self.assertEqual(test.strip(), output.strip()) self.assertEqual(json.loads(test), json.loads(output))
def testSkipMissingTranslations(self): def testSkipMissingTranslations(self):
grd = """<?xml version="1.0" encoding="UTF-8"?> grd = """<?xml version="1.0" encoding="UTF-8"?>
...@@ -141,12 +146,25 @@ class ChromeMessagesJsonFormatUnittest(unittest.TestCase): ...@@ -141,12 +146,25 @@ class ChromeMessagesJsonFormatUnittest(unittest.TestCase):
build.RcBuilder.ProcessNode(root, DummyOutput('chrome_messages_json', 'fr'), build.RcBuilder.ProcessNode(root, DummyOutput('chrome_messages_json', 'fr'),
buf) buf)
output = buf.getvalue() output = buf.getvalue()
test = u""" test = u'{}'
{ self.assertEqual(test, output)
} def testVerifyMinification(self):
""" root = util.ParseGrdForUnittest(u"""
self.assertEqual(test.strip(), output.strip()) <messages>
<message name="IDS">
<ph name="BEGIN">$1<ex>a</ex></ph>test<ph name="END">$2<ex>b</ex></ph>
</message>
</messages>
""")
buf = StringIO.StringIO()
build.RcBuilder.ProcessNode(root, DummyOutput('chrome_messages_json', 'en'),
buf)
output = buf.getvalue()
test = (u'{"IDS":{"message":"$1$test$2$","placeholders":'
u'{"1":{"content":"$1"},"2":{"content":"$2"}}}}')
self.assertEqual(test, output)
class DummyOutput(object): class DummyOutput(object):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment