diff options
author | Android Chromium Automerger <chromium-automerger@android> | 2014-04-07 16:14:50 +0000 |
---|---|---|
committer | Android Chromium Automerger <chromium-automerger@android> | 2014-04-07 16:14:50 +0000 |
commit | 529c71f7f29a05c176f72df33223359540d28488 (patch) | |
tree | 7fbb40cb3a8e6ca48b3076a5a1e772d455832ad1 | |
parent | 7bb353b32e503652cab6087a93042bbc7ab2f4b6 (diff) | |
parent | 6fb6d62a2a05b7a7c268cc4e6bf0452e376e1990 (diff) | |
download | grit-529c71f7f29a05c176f72df33223359540d28488.tar.gz |
Merge tools/grit from https://chromium.googlesource.com/external/grit-i18n.git at 6fb6d62a2a05b7a7c268cc4e6bf0452e376e1990
This commit was generated by merge_from_chromium.py.
Change-Id: Ieff39dcf549bf443ba65a0f4470176515577de15
12 files changed, 75 insertions, 98 deletions
diff --git a/grit/format/policy_templates/policy_template_generator.py b/grit/format/policy_templates/policy_template_generator.py index 3fad8ca..11d097e 100644 --- a/grit/format/policy_templates/policy_template_generator.py +++ b/grit/format/policy_templates/policy_template_generator.py @@ -5,7 +5,6 @@ import copy -import types class PolicyTemplateGenerator: @@ -104,20 +103,6 @@ class PolicyTemplateGenerator: }) return result - def _PrintPolicyValue(self, item): - '''Produces a string representation for a policy value. Taking care to print - dictionaries in a sorted order.''' - if type(item) == types.StringType: - str_val = "'%s'" % item - elif isinstance(item, dict): - str_val = "{"; - for it in sorted(item.iterkeys()): - str_val += "\'%s\': %s, " % (it, self._PrintPolicyValue(item[it])) - str_val = str_val.rstrip(", ") + "}"; - else: - str_val = str(item) - return str_val; - def _ProcessPolicy(self, policy): '''Processes localized message strings in a policy or a group. Also breaks up the content of 'supported_on' attribute into a list. @@ -137,8 +122,6 @@ class PolicyTemplateGenerator: # Iterate through all the items of an enum-type policy, and add captions. for item in policy['items']: item['caption'] = self._ImportMessage(item['caption']) - elif policy['type'] == 'dict' and 'example_value' in policy: - policy['example_value'] = self._PrintPolicyValue(policy['example_value']) if policy['type'] != 'group': if not 'label' in policy: # If 'label' is not specified, then it defaults to 'caption': diff --git a/grit/format/policy_templates/writers/adml_writer_unittest.py b/grit/format/policy_templates/writers/adml_writer_unittest.py index 41757d9..3600005 100644 --- a/grit/format/policy_templates/writers/adml_writer_unittest.py +++ b/grit/format/policy_templates/writers/adml_writer_unittest.py @@ -18,7 +18,7 @@ from grit.format.policy_templates.writers import adml_writer from grit.format.policy_templates.writers import xml_writer_base_unittest -class AdmlWriterTest(xml_writer_base_unittest.XmlWriterBaseTest): +class AdmlWriterUnittest(xml_writer_base_unittest.XmlWriterBaseTest): def setUp(self): config = { diff --git a/grit/format/policy_templates/writers/admx_writer_unittest.py b/grit/format/policy_templates/writers/admx_writer_unittest.py index 9a2a58e..c99131f 100644 --- a/grit/format/policy_templates/writers/admx_writer_unittest.py +++ b/grit/format/policy_templates/writers/admx_writer_unittest.py @@ -19,7 +19,7 @@ from grit.format.policy_templates.writers import xml_writer_base_unittest from xml.dom import minidom -class AdmxWriterTest(xml_writer_base_unittest.XmlWriterBaseTest): +class AdmxWriterUnittest(xml_writer_base_unittest.XmlWriterBaseTest): def _CreateDocumentElement(self): dom_impl = minidom.getDOMImplementation('') diff --git a/grit/format/policy_templates/writers/doc_writer.py b/grit/format/policy_templates/writers/doc_writer.py index ee45cd5..7cca976 100644 --- a/grit/format/policy_templates/writers/doc_writer.py +++ b/grit/format/policy_templates/writers/doc_writer.py @@ -234,41 +234,32 @@ class DocWriter(xml_formatted_writer.XMLFormattedWriter): if self.IsPolicySupportedOnPlatform(policy, 'mac'): self._AddListExampleMac(examples, policy) - def _PythonDictionaryToMacDictionary(self, dictionary, indent=''): - '''Converts a python dictionary to an equivalent XML plist. - - Returns a list of lines, with one dictionary entry per line.''' - result = [indent + '<dict>'] - indent += ' ' - for k in sorted(dictionary.keys()): - v = dictionary[k] - result.append('%s<key>%s</key>' % (indent, k)) - value_type = type(v) - if value_type == bool: - result.append('%s<%s/>' % (indent, 'true' if v else 'false')) - elif value_type == int: - result.append('%s<integer>%s</integer>' % (indent, v)) - elif value_type == str: - result.append('%s<string>%s</string>' % (indent, v)) - elif value_type == dict: - result += self._PythonDictionaryToMacDictionary(v, indent) - elif value_type == list: - array = [] - if len(v) != 0: - if type(v[0]) == str: - array = ['%s <string>%s</string>' % (indent, x) for x in v] - elif type(v[0]) == dict: - for x in v: - array += self._PythonDictionaryToMacDictionary(x, indent + ' ') - else: - raise Exception('Must be list of string or dict.') - result.append('%s<array>' % indent) - result += array - result.append('%s</array>' % indent) - else: - raise Exception('Invalid example value type %s' % value_type) - result.append(indent[2:] + '</dict>') - return result + def _PythonObjectToPlist(self, obj, indent=''): + '''Converts a python object to an equivalent XML plist. + + Returns a list of lines.''' + obj_type = type(obj) + if obj_type == bool: + return [ '%s<%s/>' % (indent, 'true' if obj else 'false') ] + elif obj_type == int: + return [ '%s<integer>%s</integer>' % (indent, obj) ] + elif obj_type == str: + return [ '%s<string>%s</string>' % (indent, obj) ] + elif obj_type == list: + result = [ '%s<array>' % indent ] + for item in obj: + result += self._PythonObjectToPlist(item, indent + ' ') + result.append('%s</array>' % indent) + return result + elif obj_type == dict: + result = [ '%s<dict>' % indent ] + for key in sorted(obj.keys()): + result.append('%s<key>%s</key>' % (indent + ' ', key)) + result += self._PythonObjectToPlist(obj[key], indent + ' ') + result.append('%s</dict>' % indent) + return result + else: + raise Exception('Invalid object to convert: %s' % obj) def _AddDictionaryExampleMac(self, parent, policy): '''Adds an example value for Mac of a 'dict' policy to a DOM node. @@ -282,7 +273,7 @@ class DocWriter(xml_formatted_writer.XMLFormattedWriter): self.AddElement(parent, 'dt', {}, 'Mac:') mac = self._AddStyledElement(parent, 'dd', ['.monospace', '.pre']) mac_text = ['<key>%s</key>' % (policy['name'])] - mac_text += self._PythonDictionaryToMacDictionary(example_value) + mac_text += self._PythonObjectToPlist(example_value) self.AddText(mac, '\n'.join(mac_text)) def _AddDictionaryExampleWindows(self, parent, policy): diff --git a/grit/format/policy_templates/writers/json_writer.py b/grit/format/policy_templates/writers/json_writer.py index 4739483..673bbf7 100644 --- a/grit/format/policy_templates/writers/json_writer.py +++ b/grit/format/policy_templates/writers/json_writer.py @@ -3,6 +3,8 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. +import json + from textwrap import TextWrapper from grit.format.policy_templates.writers import template_writer @@ -38,28 +40,10 @@ class JsonWriter(template_writer.TemplateWriter): return self.FlattenGroupsAndSortPolicies(policy_list) def WritePolicy(self, policy): - example_value = policy['example_value'] - if policy['type'] == 'string': - example_value_str = '"' + example_value + '"' - elif policy['type'] in ('int', 'int-enum', 'dict'): - example_value_str = str(example_value) - elif policy['type'] == 'list': - if example_value == []: - example_value_str = '[]' - else: - example_value_str = '["%s"]' % '", "'.join(example_value) - elif policy['type'] == 'main': - if example_value == True: - example_value_str = 'true' - else: - example_value_str = 'false' - elif policy['type'] == 'string-enum': - example_value_str = '"%s"' % example_value; - elif policy['type'] == 'external': + if policy['type'] == 'external': # This type can only be set through cloud policy. return - else: - raise Exception('unknown policy type %s:' % policy['type']) + example_value_str = json.dumps(policy['example_value'], sort_keys=True) # Add comma to the end of the previous line. if not self._first_written: diff --git a/grit/format/policy_templates/writers/json_writer_unittest.py b/grit/format/policy_templates/writers/json_writer_unittest.py index 1281c19..00acde3 100644 --- a/grit/format/policy_templates/writers/json_writer_unittest.py +++ b/grit/format/policy_templates/writers/json_writer_unittest.py @@ -235,13 +235,13 @@ class JsonWriterUnittest(writer_unittest_common.WriterUnittestCommon): # Tests a policy group with a single policy of type 'dict'. example = { 'bool': True, - 'int': 10, - 'string': 'abc', - 'list': [1, 2, 3], 'dict': { 'a': 1, 'b': 2, - } + }, + 'int': 10, + 'list': [1, 2, 3], + 'string': 'abc', } grd = self.PrepareTest( '{' @@ -264,8 +264,8 @@ class JsonWriterUnittest(writer_unittest_common.WriterUnittestCommon): ' // Example Dictionary Policy\n' + HEADER_DELIMETER + ' // Example Dictionary Policy\n\n' - ' //"DictionaryPolicy": {\'bool\': True, \'dict\': {\'a\': 1, ' - '\'b\': 2}, \'int\': 10, \'list\': [1, 2, 3], \'string\': \'abc\'}\n\n' + ' //"DictionaryPolicy": {"bool": true, "dict": {"a": 1, ' + '"b": 2}, "int": 10, "list": [1, 2, 3], "string": "abc"}\n\n' '}') self.CompareOutputs(output, expected_output) diff --git a/grit/format/policy_templates/writers/plist_writer.py b/grit/format/policy_templates/writers/plist_writer.py index 46b7ade..2297858 100644 --- a/grit/format/policy_templates/writers/plist_writer.py +++ b/grit/format/policy_templates/writers/plist_writer.py @@ -127,13 +127,16 @@ class PListWriter(xml_formatted_writer.XMLFormattedWriter): self._array = self._AddKeyValuePair(dict, 'pfm_subkeys', 'array') - def Init(self): + def CreatePlistDocument(self): dom_impl = minidom.getDOMImplementation('') doctype = dom_impl.createDocumentType( 'plist', '-//Apple//DTD PLIST 1.0//EN', 'http://www.apple.com/DTDs/PropertyList-1.0.dtd') - self._doc = dom_impl.createDocument(None, 'plist', doctype) + return dom_impl.createDocument(None, 'plist', doctype) + + def Init(self): + self._doc = self.CreatePlistDocument() self._plist = self._doc.documentElement def GetTemplateText(self): diff --git a/grit/format/policy_templates/writers/reg_writer.py b/grit/format/policy_templates/writers/reg_writer.py index 716cd74..beb1590 100644 --- a/grit/format/policy_templates/writers/reg_writer.py +++ b/grit/format/policy_templates/writers/reg_writer.py @@ -4,6 +4,8 @@ # found in the LICENSE file. +import json + from grit.format.policy_templates.writers import template_writer @@ -64,9 +66,10 @@ class RegWriter(template_writer.TemplateWriter): i = i + 1 else: self._StartBlock(key, None, list) - if policy['type'] in ('string', 'dict'): - escaped_str = self._EscapeRegString(str(example_value)) - example_value_str = '"' + escaped_str + '"' + if policy['type'] in ('string', 'string-enum', 'dict'): + example_value_str = json.dumps(example_value, sort_keys=True) + if policy['type'] == 'dict': + example_value_str = '"%s"' % example_value_str elif policy['type'] == 'main': if example_value == True: example_value_str = 'dword:00000001' @@ -74,8 +77,6 @@ class RegWriter(template_writer.TemplateWriter): example_value_str = 'dword:00000000' elif policy['type'] in ('int', 'int-enum'): example_value_str = 'dword:%08x' % example_value - elif policy['type'] == 'string-enum': - example_value_str = '"%s"' % example_value else: raise Exception('unknown policy type %s:' % policy['type']) diff --git a/grit/format/policy_templates/writers/reg_writer_unittest.py b/grit/format/policy_templates/writers/reg_writer_unittest.py index d84599c..d559c9f 100644 --- a/grit/format/policy_templates/writers/reg_writer_unittest.py +++ b/grit/format/policy_templates/writers/reg_writer_unittest.py @@ -216,13 +216,13 @@ class RegWriterUnittest(writer_unittest_common.WriterUnittestCommon): # Tests a policy group with a single policy of type 'dict'. example = { 'bool': True, - 'int': 10, - 'string': 'abc', - 'list': [1, 2, 3], 'dict': { 'a': 1, 'b': 2, - } + }, + 'int': 10, + 'list': [1, 2, 3], + 'string': 'abc', } grd = self.PrepareTest( '{' @@ -244,8 +244,8 @@ class RegWriterUnittest(writer_unittest_common.WriterUnittestCommon): 'Windows Registry Editor Version 5.00', '', '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Chromium]', - '"DictionaryPolicy"="{\'bool\': True, \'dict\': {\'a\': 1, ' - '\'b\': 2}, \'int\': 10, \'list\': [1, 2, 3], \'string\': \'abc\'}"']) + '"DictionaryPolicy"="{"bool": true, "dict": {"a": 1, ' + '"b": 2}, "int": 10, "list": [1, 2, 3], "string": "abc"}"']) self.CompareOutputs(output, expected_output) def testNonSupportedPolicy(self): diff --git a/grit/format/policy_templates/writers/xml_formatted_writer.py b/grit/format/policy_templates/writers/xml_formatted_writer.py index b28d8b6..dad3717 100644 --- a/grit/format/policy_templates/writers/xml_formatted_writer.py +++ b/grit/format/policy_templates/writers/xml_formatted_writer.py @@ -53,7 +53,11 @@ class XMLFormattedWriter(template_writer.TemplateWriter): attribute.value = value parent.setAttributeNode(attribute) - def ToPrettyXml(self, doc): + def AddComment(self, parent, comment): + '''Adds a comment node.''' + parent.appendChild(parent.ownerDocument.createComment(comment)) + + def ToPrettyXml(self, doc, **kwargs): # return doc.toprettyxml(indent=' ') # The above pretty-printer does not print the doctype and adds spaces # around texts, e.g.: @@ -66,7 +70,7 @@ class XMLFormattedWriter(template_writer.TemplateWriter): # So we use the poor man's pretty printer here. It assumes that there are # no mixed-content nodes. # Get all the XML content in a one-line string. - xml = doc.toxml() + xml = doc.toxml(**kwargs) # Determine where the line breaks will be. (They will only be between tags.) lines = xml[1:len(xml) - 1].split('><') indent = '' diff --git a/grit/test_suite_all.py b/grit/test_suite_all.py index 3f5c978..71635fc 100644 --- a/grit/test_suite_all.py +++ b/grit/test_suite_all.py @@ -42,7 +42,10 @@ class TestSuiteAll(unittest.TestSuite): import grit.format.resource_map_unittest import grit.format.policy_templates.policy_template_generator_unittest import grit.format.policy_templates.writers.adm_writer_unittest + import grit.format.policy_templates.writers.adml_writer_unittest + import grit.format.policy_templates.writers.admx_writer_unittest import grit.format.policy_templates.writers.doc_writer_unittest + import grit.format.policy_templates.writers.ios_plist_writer_unittest import grit.format.policy_templates.writers.json_writer_unittest import grit.format.policy_templates.writers.plist_strings_writer_unittest import grit.format.policy_templates.writers.plist_writer_unittest @@ -97,8 +100,14 @@ class TestSuiteAll(unittest.TestSuite): PolicyTemplateGeneratorUnittest, grit.format.policy_templates.writers.adm_writer_unittest. AdmWriterUnittest, + grit.format.policy_templates.writers.adml_writer_unittest. + AdmlWriterUnittest, + grit.format.policy_templates.writers.admx_writer_unittest. + AdmxWriterUnittest, grit.format.policy_templates.writers.doc_writer_unittest. DocWriterUnittest, + grit.format.policy_templates.writers.ios_plist_writer_unittest. + IOSPListWriterUnittest, grit.format.policy_templates.writers.json_writer_unittest. JsonWriterUnittest, grit.format.policy_templates.writers.plist_strings_writer_unittest. diff --git a/grit/tool/build.py b/grit/tool/build.py index 2bb8085..87ee412 100644 --- a/grit/tool/build.py +++ b/grit/tool/build.py @@ -36,8 +36,10 @@ _format_modules = { 'resource_map_source': 'resource_map', 'resource_file_map_source': 'resource_map', } -_format_modules.update((type, 'policy_templates.template_formatter') - for type in 'adm plist plist_strings admx adml doc json reg'.split()) +_format_modules.update( + (type, 'policy_templates.template_formatter') for type in + [ 'adm', 'admx', 'adml', 'reg', 'doc', 'json', + 'plist', 'plist_strings', 'ios_plist' ]) def GetFormatter(type): |