diff options
author | gfeher@chromium.org <gfeher@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-26 12:33:58 +0000 |
---|---|---|
committer | gfeher@chromium.org <gfeher@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-26 12:33:58 +0000 |
commit | b6999c5499e996ed8fc5174e1f1666d5edc3f14c (patch) | |
tree | 2bb612e49eaee4f96d8668cee127a62e96379bd2 /tools/grit | |
parent | 34f9e734afefc9bc1689023bc36f83f4909f6809 (diff) | |
download | chromium_src-b6999c5499e996ed8fc5174e1f1666d5edc3f14c.zip chromium_src-b6999c5499e996ed8fc5174e1f1666d5edc3f14c.tar.gz chromium_src-b6999c5499e996ed8fc5174e1f1666d5edc3f14c.tar.bz2 |
Generate .reg files with example values of policies
BUG=62532
TEST=python:RegWriterUnittest.*
Review URL: http://codereview.chromium.org/5328004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@67451 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools/grit')
5 files changed, 329 insertions, 6 deletions
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 index bce4c87..744eeaa 100644 --- a/tools/grit/grit/format/policy_templates/writers/json_writer_unittest.py +++ b/tools/grit/grit/format/policy_templates/writers/json_writer_unittest.py @@ -34,7 +34,7 @@ class JsonWriterUnittest(writer_unittest_common.WriterUnittestCommon): expected_output.strip()) def testEmpty(self): - # Test PListWriter in case of empty polices. + # Test the handling of an empty policy list. grd = self.PrepareTest( '{' ' "policy_definitions": [],' @@ -107,12 +107,12 @@ class JsonWriterUnittest(writer_unittest_common.WriterUnittestCommon): ' "name": "EnumPolicy",' ' "type": "enum",' ' "items": [' - ' {"name": "ProxyServerDisabled", "value": "0"},' - ' {"name": "ProxyServerAutoDetect", "value": "1"},' + ' {"name": "ProxyServerDisabled", "value": 0},' + ' {"name": "ProxyServerAutoDetect", "value": 1},' ' ],' ' "supported_on": ["chrome.linux:8-"],' ' "annotations": {' - ' "example_value": "1"' + ' "example_value": 1' ' }' ' },' ' ],' diff --git a/tools/grit/grit/format/policy_templates/writers/reg_writer.py b/tools/grit/grit/format/policy_templates/writers/reg_writer.py new file mode 100644 index 0000000..5d03fbd --- /dev/null +++ b/tools/grit/grit/format/policy_templates/writers/reg_writer.py @@ -0,0 +1,77 @@ +# 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 RegWriter objects. + See the constructor of TemplateWriter for description of + arguments. + ''' + return RegWriter(['win'], config, messages) + + +class RegWriter(template_writer.TemplateWriter): + '''Class for generating policy example files in .reg format (for Windows). + The generated files will define all the supported policies with example + values set for them. This class is used by PolicyTemplateGenerator to + write .reg files. + ''' + + NEWLINE = '\r\n' + + def _EscapeRegString(self, string): + return string.replace('\\', '\\\\').replace('\"', '\\\"') + + def _StartBlock(self, suffix): + key = 'HKEY_LOCAL_MACHINE\\' + self.config['win_reg_key_name'] + if suffix: + key = key + '\\' + suffix + if key != self._last_key: + self._out.append('') + self._out.append('[%s]' % key) + self._last_key = key + + def WritePolicy(self, policy): + example_value = policy['annotations']['example_value'] + + if policy['type'] == 'list': + self._StartBlock(policy['name']) + i = 1 + for item in example_value: + escaped_str = self._EscapeRegString(item) + self._out.append('"%d"="%s"' % (i, escaped_str)) + i = i + 1 + else: + self._StartBlock(None) + if policy['type'] == 'string': + escaped_str = self._EscapeRegString(example_value) + example_value_str = '"' + escaped_str + '"' + elif policy['type'] == 'main': + if example_value == True: + example_value_str = 'dword:1' + else: + example_value_str = 'dword:0' + elif policy['type'] == 'enum': + example_value_str = 'dword:%d' % example_value + else: + raise Exception('unknown policy type %s:' % policy['type']) + + self._out.append('"%s"=%s' % (policy['name'], example_value_str)) + + def BeginTemplate(self): + pass + + def EndTemplate(self): + pass + + def Init(self): + self._out = ['Windows Registry Editor Version 5.00'] + self._last_key = None + + def GetTemplateText(self): + return self.NEWLINE.join(self._out) diff --git a/tools/grit/grit/format/policy_templates/writers/reg_writer_unittest.py b/tools/grit/grit/format/policy_templates/writers/reg_writer_unittest.py new file mode 100644 index 0000000..9cb9db7 --- /dev/null +++ b/tools/grit/grit/format/policy_templates/writers/reg_writer_unittest.py @@ -0,0 +1,246 @@ +#!/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.reg_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 RegWriterUnittest(writer_unittest_common.WriterUnittestCommon): + '''Unit tests for RegWriter.''' + + NEWLINE='\r\n' + + def CompareOutputs(self, output, expected_output): + '''Compares the output of the reg_writer with its expected output. + + Args: + output: The output of the reg 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 the handling of an empty policy list. + grd = self.PrepareTest( + '{' + ' "policy_definitions": [],' + ' "placeholders": [],' + '}', '<messages></messages>') + output = self.GetOutput(grd, 'fr', {'_chromium': '1',}, 'reg', 'en') + expected_output = 'Windows Registry Editor Version 5.00' + 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",' + ' "supported_on": ["chrome.win:8-"],' + ' "annotations": {' + ' "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'}, 'reg', 'en') + expected_output = self.NEWLINE.join([ + 'Windows Registry Editor Version 5.00', + '', + '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Google\\Chrome]', + '"MainPolicy"=dword:1']) + 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",' + ' "supported_on": ["chrome.win:8-"],' + ' "annotations": {' + ' "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'}, 'reg', 'en') + expected_output = self.NEWLINE.join([ + 'Windows Registry Editor Version 5.00', + '', + '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Chromium]', + '"StringPolicy"="hello, world! \\\" \\\\"']) + 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},' + ' ],' + ' "supported_on": ["chrome.win:8-"],' + ' "annotations": {' + ' "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'}, 'reg', 'en') + expected_output = self.NEWLINE.join([ + 'Windows Registry Editor Version 5.00', + '', + '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Google\\Chrome]', + '"EnumPolicy"=dword:1']) + 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",' + ' "supported_on": ["chrome.linux:8-"],' + ' "annotations": {' + ' "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'}, 'reg', 'en') + expected_output = self.NEWLINE.join([ + 'Windows Registry Editor Version 5.00', + '', + '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Chromium\\ListPolicy]', + '"1"="foo"', + '"2"="bar"']) + + def testNonSupportedPolicy(self): + # Tests a policy that is not supported on Windows, so it shouldn't + # be included in the .REG file. + grd = self.PrepareTest( + '{' + ' "policy_definitions": [' + ' {' + ' "name": "NonWindowsPolicy",' + ' "type": "list",' + ' "supported_on": ["chrome.mac:8-"],' + ' "annotations": {' + ' "example_value": ["a"]' + ' }' + ' },' + ' ],' + ' "placeholders": [],' + '}', + '<messages>' + ' <message name="IDS_POLICY_NONWINDOWSPOLICY_CAPTION"></message>' + ' <message name="IDS_POLICY_NONWINDOWSPOLICY_DESC"></message>' + '</messages>') + output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'reg', 'en') + expected_output = self.NEWLINE.join([ + 'Windows Registry Editor Version 5.00']) + 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",' + ' "supported_on": ["chrome.win:8-"],' + ' "annotations": {' + ' "example_value": ["a", "b"]' + ' }' + ' },{' + ' "name": "Policy2",' + ' "type": "string",' + ' "supported_on": ["chrome.win:8-"],' + ' "annotations": {' + ' "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'}, 'reg', 'en') + expected_output = self.NEWLINE.join([ + 'Windows Registry Editor Version 5.00', + '', + '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Chromium\\Policy1]', + '"1"="a"', + '"2"="b"', + '', + '[HKEY_LOCAL_MACHINE\\Software\\Policies\\Chromium]', + '"Policy2"="c"']) + self.CompareOutputs(output, expected_output) + + +if __name__ == '__main__': + unittest.main() diff --git a/tools/grit/grit/format/policy_templates/writers/xml_writer_base_unittest.py b/tools/grit/grit/format/policy_templates/writers/xml_writer_base_unittest.py index f5bd429..125856e 100644 --- a/tools/grit/grit/format/policy_templates/writers/xml_writer_base_unittest.py +++ b/tools/grit/grit/format/policy_templates/writers/xml_writer_base_unittest.py @@ -12,7 +12,6 @@ import sys import unittest -from grit.format.policy_templates.writers import admx_writer from xml.dom import minidom diff --git a/tools/grit/grit/node/misc.py b/tools/grit/grit/node/misc.py index 0f37774..2b67cb36 100644 --- a/tools/grit/grit/node/misc.py +++ b/tools/grit/grit/node/misc.py @@ -250,7 +250,8 @@ 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', 'json'): + elif t in ('adm', 'plist', 'plist_strings', 'admx', 'adml', 'doc', 'json', + 'reg'): from grit.format.policy_templates import template_formatter return template_formatter.TemplateFormatter(t) else: |