summaryrefslogtreecommitdiffstats
path: root/tools/grit
diff options
context:
space:
mode:
authorgfeher@chromium.org <gfeher@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-11-02 15:49:22 +0000
committergfeher@chromium.org <gfeher@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-11-02 15:49:22 +0000
commit2ff94fdf547169086f91a86cbbf7e1accedadd5f (patch)
tree6a3abdb4ac9bf5395905b3d2caffa57fd038398a /tools/grit
parent4453c26ac93a402f617c88ba777769ccceb491b4 (diff)
downloadchromium_src-2ff94fdf547169086f91a86cbbf7e1accedadd5f.zip
chromium_src-2ff94fdf547169086f91a86cbbf7e1accedadd5f.tar.gz
chromium_src-2ff94fdf547169086f91a86cbbf7e1accedadd5f.tar.bz2
Generate example JSON policy configuration files
Also fix filtering of single (non-groupped) policies by platforms. BUG=56531 TEST=JsonWriterUnittest.*,PolicyTemplateGeneratorUnittest.testPolicyFiltering Review URL: http://codereview.chromium.org/4164006 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@64748 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools/grit')
-rw-r--r--tools/grit/grit/format/policy_templates/policy_template_generator_unittest.py40
-rw-r--r--tools/grit/grit/format/policy_templates/writers/json_writer.py65
-rw-r--r--tools/grit/grit/format/policy_templates/writers/json_writer_unittest.py234
-rw-r--r--tools/grit/grit/format/policy_templates/writers/template_writer.py2
-rw-r--r--tools/grit/grit/node/misc.py2
-rw-r--r--tools/grit/grit/tool/build.py2
6 files changed, 325 insertions, 20 deletions
diff --git a/tools/grit/grit/format/policy_templates/policy_template_generator_unittest.py b/tools/grit/grit/format/policy_templates/policy_template_generator_unittest.py
index 83738ad..7401a6d 100644
--- a/tools/grit/grit/format/policy_templates/policy_template_generator_unittest.py
+++ b/tools/grit/grit/format/policy_templates/policy_template_generator_unittest.py
@@ -266,38 +266,44 @@ class PolicyTemplateGeneratorUnittest(unittest.TestCase):
'annotations': {'platforms': ['eee']}
},
]
+ },{
+ 'name': 'SinglePolicy',
+ 'type': 'int',
+ 'annotations': {'platforms': ['eee']}
}]
+ # This writer accumulates the list of policies it is asked to write.
+ # This list is stored in the result_list member variable and can
+ # be used later for assertions.
class LocalMockWriter(mock_writer.MockWriter):
- def __init__(self, platforms, expected_list):
+ def __init__(self, platforms):
self.platforms = platforms
- self.expected_list = expected_list
self.policy_name = None
self.result_list = []
def BeginPolicyGroup(self, group):
self.group = group;
self.result_list.append('begin_' + group['name'])
def EndPolicyGroup(self):
+ self.result_list.append('end_group')
self.group = None
def WritePolicy(self, policy):
- self.tester.assertEquals(policy['name'][0:6], self.group['name'])
self.result_list.append(policy['name'])
def IsPolicySupported(self, policy):
# Call the original (non-mock) implementation of this method.
return template_writer.TemplateWriter.IsPolicySupported(self, policy)
- def Test(self):
- self.tester.assertEquals(
- self.result_list,
- self.expected_list)
- self.do_test(
- MessagesMock(),
- policy_defs_mock,
- LocalMockWriter(['eee'], ['begin_Group2', 'Group2Policy3']))
- self.do_test(
- MessagesMock(),
- policy_defs_mock,
- LocalMockWriter(
- ['ddd', 'bbb'],
- ['begin_Group1', 'Group1Policy1', 'Group1Policy2']))
+
+ local_mock_writer = LocalMockWriter(['eee'])
+ self.do_test(MessagesMock(), policy_defs_mock, local_mock_writer)
+ # Test that only policies of platform 'eee' were written:
+ self.assertEquals(
+ local_mock_writer.result_list,
+ ['begin_Group2', 'Group2Policy3', 'end_group', 'SinglePolicy'])
+
+ local_mock_writer = LocalMockWriter(['ddd', 'bbb'])
+ self.do_test(MessagesMock(), policy_defs_mock, local_mock_writer)
+ # Test that only policies of platforms 'ddd' and 'bbb' were written:
+ self.assertEquals(
+ local_mock_writer.result_list,
+ ['begin_Group1', 'Group1Policy1', 'Group1Policy2', 'end_group'])
def testSorting(self):
# Tests that policies are sorted correctly.
diff --git a/tools/grit/grit/format/policy_templates/writers/json_writer.py b/tools/grit/grit/format/policy_templates/writers/json_writer.py
new file mode 100644
index 0000000..776e4f5
--- /dev/null
+++ b/tools/grit/grit/format/policy_templates/writers/json_writer.py
@@ -0,0 +1,65 @@
+# Copyright (c) 2010 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+
+from xml.dom import minidom
+from grit.format.policy_templates.writers import template_writer
+
+
+def GetWriter(config, messages):
+ '''Factory method for creating JsonWriter objects.
+ See the constructor of TemplateWriter for description of
+ arguments.
+ '''
+ return JsonWriter(['linux'], config, messages)
+
+
+class JsonWriter(template_writer.TemplateWriter):
+ '''Class for generating policy files in JSON format (for Linux). The
+ generated files will define all the supported policies with example values
+ set for them. This class is used by PolicyTemplateGenerator to write .json
+ files.
+ '''
+
+ def WritePolicy(self, policy):
+ example_value = policy['annotations']['example_value']
+ if policy['type'] == 'string':
+ example_value_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'] == 'enum':
+ example_value_str = example_value
+ else:
+ raise Exception('unknown policy type %s:' % policy['type'])
+
+ # Add comme to the end of the previous line.
+ if not self._first_written:
+ self._out[-1] += ','
+
+ line = ' "%s": %s' % (policy['name'], example_value_str)
+ self._out.append(line)
+
+ self._first_written = False
+
+ def BeginTemplate(self):
+ self._out.append('{')
+
+ def EndTemplate(self):
+ self._out.append('}')
+
+ def Init(self):
+ self._out = []
+ # The following boolean member is true until the first policy is written.
+ self._first_written = True
+
+ def GetTemplateText(self):
+ return '\n'.join(self._out)
diff --git a/tools/grit/grit/format/policy_templates/writers/json_writer_unittest.py b/tools/grit/grit/format/policy_templates/writers/json_writer_unittest.py
new file mode 100644
index 0000000..35d84f0
--- /dev/null
+++ b/tools/grit/grit/format/policy_templates/writers/json_writer_unittest.py
@@ -0,0 +1,234 @@
+#!/usr/bin/python2.4
+# Copyright (c) 2010 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+'''Unit tests for grit.format.policy_templates.writers.json_writer'''
+
+
+import os
+import sys
+if __name__ == '__main__':
+ sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), '../../../..'))
+
+import unittest
+
+from grit.format.policy_templates.writers import writer_unittest_common
+
+
+class JsonWriterUnittest(writer_unittest_common.WriterUnittestCommon):
+ '''Unit tests for JsonWriter.'''
+
+ def CompareOutputs(self, output, expected_output):
+ '''Compares the output of the json_writer with its expected output.
+
+ Args:
+ output: The output of the json writer as returned by grit.
+ expected_output: The expected output.
+
+ Raises:
+ AssertionError: if the two strings are not equivalent.
+ '''
+ self.assertEquals(
+ output.strip(),
+ expected_output.strip())
+
+ def testEmpty(self):
+ # Test PListWriter in case of empty polices.
+ grd = self.PrepareTest(
+ '{'
+ ' "policy_definitions": [],'
+ ' "placeholders": [],'
+ '}', '<messages></messages>')
+ output = self.GetOutput(grd, 'fr', {'_chromium': '1',}, 'json', 'en')
+ expected_output = '{\n}'
+ self.CompareOutputs(output, expected_output)
+
+ def testMainPolicy(self):
+ # Tests a policy group with a single policy of type 'main'.
+ grd = self.PrepareTest(
+ '{'
+ ' "policy_definitions": ['
+ ' {'
+ ' "name": "MainPolicy",'
+ ' "type": "main",'
+ ' "annotations": {'
+ ' "platforms": ["linux"],'
+ ' "example_value": True'
+ ' }'
+ ' },'
+ ' ],'
+ ' "placeholders": [],'
+ '}',
+ '<messages>'
+ ' <message name="IDS_POLICY_MAINPOLICY_CAPTION"></message>'
+ ' <message name="IDS_POLICY_MAINPOLICY_DESC"></message>'
+ '</messages>')
+ output = self.GetOutput(grd, 'fr', {'_google_chrome' : '1'}, 'json', 'en')
+ expected_output = (
+ '{\n'
+ ' "MainPolicy": true\n'
+ '}')
+ self.CompareOutputs(output, expected_output)
+
+ def testStringPolicy(self):
+ # Tests a policy group with a single policy of type 'string'.
+ grd = self.PrepareTest(
+ '{'
+ ' "policy_definitions": ['
+ ' {'
+ ' "name": "StringPolicy",'
+ ' "type": "string",'
+ ' "annotations": {'
+ ' "platforms": ["linux"],'
+ ' "example_value": "hello, world!"'
+ ' }'
+ ' },'
+ ' ],'
+ ' "placeholders": [],'
+ '}',
+ '<messages>'
+ ' <message name="IDS_POLICY_STRINGPOLICY_CAPTION"></message>'
+ ' <message name="IDS_POLICY_STRINGPOLICY_DESC"></message>'
+ '</messages>')
+ output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'json', 'en')
+ expected_output = (
+ '{\n'
+ ' "StringPolicy": "hello, world!"\n'
+ '}')
+ self.CompareOutputs(output, expected_output)
+
+ def testEnumPolicy(self):
+ # Tests a policy group with a single policy of type 'enum'.
+ grd = self.PrepareTest(
+ '{'
+ ' "policy_definitions": ['
+ ' {'
+ ' "name": "EnumPolicy",'
+ ' "type": "enum",'
+ ' "items": ['
+ ' {"name": "ProxyServerDisabled", "value": "0"},'
+ ' {"name": "ProxyServerAutoDetect", "value": "1"},'
+ ' ],'
+ ' "annotations": {'
+ ' "platforms": ["linux"],'
+ ' "example_value": "1"'
+ ' }'
+ ' },'
+ ' ],'
+ ' "placeholders": [],'
+ '}',
+ '<messages>'
+ ' <message name="IDS_POLICY_ENUMPOLICY_CAPTION"></message>'
+ ' <message name="IDS_POLICY_ENUMPOLICY_DESC"></message>'
+ ' <message name="IDS_POLICY_ENUM_PROXYSERVERDISABLED_CAPTION">'
+ ' </message>'
+ ' <message name="IDS_POLICY_ENUM_PROXYSERVERAUTODETECT_CAPTION">'
+ ' </message>'
+ '</messages>')
+ output = self.GetOutput(grd, 'fr', {'_google_chrome': '1'}, 'json', 'en')
+ expected_output = (
+ '{\n'
+ ' "EnumPolicy": 1\n'
+ '}')
+ self.CompareOutputs(output, expected_output)
+
+ def testListPolicy(self):
+ # Tests a policy group with a single policy of type 'list'.
+ grd = self.PrepareTest(
+ '{'
+ ' "policy_definitions": ['
+ ' {'
+ ' "name": "ListPolicy",'
+ ' "type": "list",'
+ ' "annotations": {'
+ ' "platforms": ["linux"],'
+ ' "example_value": ["foo", "bar"]'
+ ' }'
+ ' },'
+ ' ],'
+ ' "placeholders": [],'
+ '}',
+ '<messages>'
+ ' <message name="IDS_POLICY_LISTPOLICY_DESC"></message>'
+ ' <message name="IDS_POLICY_LISTPOLICY_CAPTION"></message>'
+ ' <message name="IDS_POLICY_LISTPOLICY_LABEL"></message>'
+ '</messages>')
+ output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'json', 'en')
+ expected_output = (
+ '{\n'
+ ' "ListPolicy": ["foo", "bar"]\n'
+ '}')
+ self.CompareOutputs(output, expected_output)
+
+ def testNonSupportedPolicy(self):
+ # Tests a policy that is not supported on Linux, so it shouldn't
+ # be included in the JSON file.
+ grd = self.PrepareTest(
+ '{'
+ ' "policy_definitions": ['
+ ' {'
+ ' "name": "NonLinuxPolicy",'
+ ' "type": "list",'
+ ' "annotations": {'
+ ' "platforms": ["win"],'
+ ' "example_value": ["a"]'
+ ' }'
+ ' },'
+ ' ],'
+ ' "placeholders": [],'
+ '}',
+ '<messages>'
+ ' <message name="IDS_POLICY_NONLINUXPOLICY_CAPTION"></message>'
+ ' <message name="IDS_POLICY_NONLINUXPOLICY_DESC"></message>'
+ '</messages>')
+ output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'json', 'en')
+ expected_output = '{\n}'
+ self.CompareOutputs(output, expected_output)
+
+ def testPolicyGroup(self):
+ # Tests a policy group that has more than one policies.
+ grd = self.PrepareTest(
+ '{'
+ ' "policy_definitions": ['
+ ' {'
+ ' "name": "Group1",'
+ ' "type": "group",'
+ ' "policies": [{'
+ ' "name": "Policy1",'
+ ' "type": "list",'
+ ' "annotations": {'
+ ' "platforms": ["linux"],'
+ ' "example_value": ["a", "b"]'
+ ' }'
+ ' },{'
+ ' "name": "Policy2",'
+ ' "type": "string",'
+ ' "annotations": {'
+ ' "platforms": ["linux"],'
+ ' "example_value": "c"'
+ ' }'
+ ' }],'
+ ' },'
+ ' ],'
+ ' "placeholders": [],'
+ '}',
+ '<messages>'
+ ' <message name="IDS_POLICY_GROUP1_CAPTION"></message>'
+ ' <message name="IDS_POLICY_GROUP1_DESC"></message>'
+ ' <message name="IDS_POLICY_POLICY1_DESC"></message>'
+ ' <message name="IDS_POLICY_POLICY2_DESC"></message>'
+ ' <message name="IDS_POLICY_POLICY1_CAPTION"></message>'
+ ' <message name="IDS_POLICY_POLICY2_CAPTION"></message>'
+ '</messages>')
+ output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'json', 'en')
+ expected_output = (
+ '{\n'
+ ' "Policy1": ["a", "b"],\n'
+ ' "Policy2": "c"\n'
+ '}')
+ self.CompareOutputs(output, expected_output)
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/tools/grit/grit/format/policy_templates/writers/template_writer.py b/tools/grit/grit/format/policy_templates/writers/template_writer.py
index 4af1e458..aa4f233 100644
--- a/tools/grit/grit/format/policy_templates/writers/template_writer.py
+++ b/tools/grit/grit/format/policy_templates/writers/template_writer.py
@@ -90,7 +90,7 @@ class TemplateWriter(object):
# Nesting of groups is currently not supported.
self.WritePolicy(child_policy)
self.EndPolicyGroup()
- else:
+ elif self.IsPolicySupported(policy):
self.WritePolicy(policy)
self.EndTemplate()
return self.GetTemplateText()
diff --git a/tools/grit/grit/node/misc.py b/tools/grit/grit/node/misc.py
index eb1e054..0f37774 100644
--- a/tools/grit/grit/node/misc.py
+++ b/tools/grit/grit/node/misc.py
@@ -250,7 +250,7 @@ class GritNode(base.Node):
elif t == 'js_map_format':
from grit.format import js_map_format
return js_map_format.TopLevel()
- elif t in ('adm', 'plist', 'plist_strings', 'admx', 'adml', 'doc'):
+ elif t in ('adm', 'plist', 'plist_strings', 'admx', 'adml', 'doc', 'json'):
from grit.format.policy_templates import template_formatter
return template_formatter.TemplateFormatter(t)
else:
diff --git a/tools/grit/grit/tool/build.py b/tools/grit/grit/tool/build.py
index c750705..4b7ddd9 100644
--- a/tools/grit/grit/tool/build.py
+++ b/tools/grit/grit/tool/build.py
@@ -182,7 +182,7 @@ are exported to translation interchange files (e.g. XMB files), etc.
'resource_map_source', 'resource_file_map_source'):
encoding = 'cp1252'
elif output.GetType() in ('js_map_format', 'plist', 'plist_strings',
- 'doc'):
+ 'doc', 'json'):
encoding = 'utf_8'
else:
# TODO(gfeher) modify here to set utf-8 encoding for admx/adml