diff options
author | gfeher@chromium.org <gfeher@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-09-15 08:13:24 +0000 |
---|---|---|
committer | gfeher@chromium.org <gfeher@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-09-15 08:13:24 +0000 |
commit | 1b1d0e0b4e66d3b65f2617fd14543426516b734d (patch) | |
tree | a148a14b6e8a0775e29b3f54bd0bc73b80f7b775 /tools/grit | |
parent | 13c70b80c24bea374bd921d2b3c885d32026c985 (diff) | |
download | chromium_src-1b1d0e0b4e66d3b65f2617fd14543426516b734d.zip chromium_src-1b1d0e0b4e66d3b65f2617fd14543426516b734d.tar.gz chromium_src-1b1d0e0b4e66d3b65f2617fd14543426516b734d.tar.bz2 |
Add annotations to the policy template metafile
The annotations include a list of supported platforms for each policy. Depending on this, only the supported policies are written for each template type.
BUG=54665
TEST=PolicyTemplateGeneratorUnittest.testPolicyFiltering, {Adm,Plist,PlistStrings}WriterUnittest.testNonSupportedPolicy, {Admx,Adml}WriterUnittest.testPlatform
Review URL: http://codereview.chromium.org/3303027
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@59483 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools/grit')
14 files changed, 321 insertions, 44 deletions
diff --git a/tools/grit/grit/format/policy_templates/policy_template_generator.py b/tools/grit/grit/format/policy_templates/policy_template_generator.py index 165cc91..e4fbbfe 100644 --- a/tools/grit/grit/format/policy_templates/policy_template_generator.py +++ b/tools/grit/grit/format/policy_templates/policy_template_generator.py @@ -109,6 +109,24 @@ class PolicyTemplateGenerator: for policy in group['policies']: self._AddMessagesToPolicy(policy) + def _GetPoliciesForWriter(self, template_writer, group): + '''Filters the list of policies in a group for a writer. + + Args: + template_writer: The writer object. + group: The dictionary of the policy group. + + Returns: The list of policies of the policy group that are compatible + with the writer. + ''' + if not 'policies' in group: + return [] + result = [] + for policy in group['policies']: + if template_writer.IsPolicySupported(policy): + result.append(policy) + return result + def GetTemplateText(self, template_writer): '''Generates the text of the template from the arguments given to the constructor, using a given TemplateWriter. @@ -123,10 +141,12 @@ class PolicyTemplateGenerator: template_writer.Prepare() template_writer.BeginTemplate() for group in self._policy_groups: - template_writer.BeginPolicyGroup(group) - if 'policies' in group: - for policy in group['policies']: + policies = self._GetPoliciesForWriter(template_writer, group) + if policies: + # Only write nonempty groups. + template_writer.BeginPolicyGroup(group) + for policy in policies: template_writer.WritePolicy(policy) - template_writer.EndPolicyGroup() + template_writer.EndPolicyGroup() template_writer.EndTemplate() return template_writer.GetTemplateText() 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 61debe5..aab761b 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 @@ -3,10 +3,16 @@ # found in the LICENSE file. +import os +import sys +if __name__ == '__main__': + sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), '../../..')) + import unittest -from policy_template_generator import PolicyTemplateGenerator -from writers.mock_writer import MockWriter +from grit.format.policy_templates import policy_template_generator +from grit.format.policy_templates.writers import mock_writer +from grit.format.policy_templates.writers import template_writer class PolicyTemplateGeneratorUnittest(unittest.TestCase): @@ -30,7 +36,9 @@ class PolicyTemplateGeneratorUnittest(unittest.TestCase): mock_writer.MockWriter. ''' writer.tester = self - policy_generator = PolicyTemplateGenerator(messages, policy_groups) + policy_generator = policy_template_generator.PolicyTemplateGenerator( + messages, + policy_groups) res = policy_generator.GetTemplateText(writer) writer.Test() return res @@ -38,7 +46,7 @@ class PolicyTemplateGeneratorUnittest(unittest.TestCase): def testSequence(self): # Test the sequence of invoking the basic PolicyWriter operations, # in case of empty input data structures. - class LocalMockWriter(MockWriter): + class LocalMockWriter(mock_writer.MockWriter): def __init__(self): self.log = 'init;' def Prepare(self): @@ -57,8 +65,7 @@ class PolicyTemplateGeneratorUnittest(unittest.TestCase): self.assertEquals(result, 'writer_result_string') def testEmptyGroups(self): - # Test that in case of three empty policy groups, each one is passed to - # the writer. + # Test that empty policy groups are not passed to the writer. messages_mock = { 'IDS_POLICY_GROUP_GROUP1_CAPTION': None, 'IDS_POLICY_GROUP_GROUP1_DESC': None, @@ -68,24 +75,48 @@ class PolicyTemplateGeneratorUnittest(unittest.TestCase): 'IDS_POLICY_GROUP_GROUP3_DESC': None, } policies_mock = [ - {'name': 'Group1', 'tag': 'T1'}, - {'name': 'Group2', 'tag': 'T2'}, - {'name': 'Group3', 'tag': 'T3'}, + {'name': 'Group1', 'policies': []}, + {'name': 'Group2', 'policies': []}, + {'name': 'Group3', 'policies': []}, ] - class LocalMockWriter(MockWriter): + class LocalMockWriter(mock_writer.MockWriter): def __init__(self): self.log = '' - self.set = set() def BeginPolicyGroup(self, group): self.log += '[' - self.set.add ( (group['name'], group['tag']) ) def EndPolicyGroup(self): self.log += ']' def Test(self): - self.tester.assertEquals(self.log, '[][][]') - self.tester.assertEquals( - self.set, - set([('Group1', 'T1'), ('Group2', 'T2'), ('Group3', 'T3')])) + self.tester.assertEquals(self.log, '') + self.do_test(messages_mock, policies_mock, LocalMockWriter()) + + def testGroups(self): + # Test that policy groups are passed to the writer in the correct order. + messages_mock = { + 'IDS_POLICY_GROUP_GROUP1_CAPTION': None, + 'IDS_POLICY_GROUP_GROUP1_DESC': None, + 'IDS_POLICY_GROUP_GROUP2_CAPTION': None, + 'IDS_POLICY_GROUP_GROUP2_DESC': None, + 'IDS_POLICY_GROUP_GROUP3_CAPTION': None, + 'IDS_POLICY_GROUP_GROUP3_DESC': None, + 'IDS_POLICY_TAG1_CAPTION': None, + 'IDS_POLICY_TAG2_CAPTION': None, + 'IDS_POLICY_TAG3_CAPTION': None, + } + policies_mock = [ + {'name': 'Group1', 'policies': [{'name': 'TAG1', 'type': 'mock'}]}, + {'name': 'Group2', 'policies': [{'name': 'TAG2', 'type': 'mock'}]}, + {'name': 'Group3', 'policies': [{'name': 'TAG3', 'type': 'mock'}]}, + ] + class LocalMockWriter(mock_writer.MockWriter): + def __init__(self): + self.log = '' + def BeginPolicyGroup(self, group): + self.log += '[' + group['policies'][0]['name'] + def EndPolicyGroup(self): + self.log += ']' + def Test(self): + self.tester.assertEquals(self.log, '[TAG1][TAG2][TAG3]') self.do_test(messages_mock, policies_mock, LocalMockWriter()) def testGroupTexts(self): @@ -100,7 +131,7 @@ class PolicyTemplateGeneratorUnittest(unittest.TestCase): {'name': 'Group1'}, {'name': 'Group2'}, ] - class LocalMockWriter(MockWriter): + class LocalMockWriter(mock_writer.MockWriter): def BeginPolicyGroup(self, group): if group['name'] == 'Group1': self.tester.assertEquals(group['caption'], 'string1') @@ -113,7 +144,7 @@ class PolicyTemplateGeneratorUnittest(unittest.TestCase): self.do_test(messages_mock, policy_groups_mock, LocalMockWriter()) def testPolicies(self): - # Test that policies are passed correctly to the writer. + # Test that policies are passed to the writer in the correct order. messages_mock = { 'IDS_POLICY_GROUP_GROUP1_CAPTION': None, 'IDS_POLICY_GROUP_GROUP1_DESC': None, @@ -138,21 +169,21 @@ class PolicyTemplateGeneratorUnittest(unittest.TestCase): ] } ] - class LocalMockWriter(MockWriter): + class LocalMockWriter(mock_writer.MockWriter): def __init__(self): self.policy_name = None - self.policy_set = set() + self.policy_list = [] def BeginPolicyGroup(self, group): self.group = group; def EndPolicyGroup(self): self.group = None - def WritePolicy(self, group): - self.tester.assertEquals(group['name'][0:6], self.group['name']) - self.policy_set.add(group['name']) + def WritePolicy(self, policy): + self.tester.assertEquals(policy['name'][0:6], self.group['name']) + self.policy_list.append(policy['name']) def Test(self): self.tester.assertEquals( - self.policy_set, - set(['Group1Policy1', 'Group1Policy2', 'Group2Policy3'])) + self.policy_list, + ['Group1Policy1', 'Group1Policy2', 'Group2Policy3']) self.do_test(messages_mock, policy_groups_mock, LocalMockWriter()) def testPolicyTexts(self): @@ -174,7 +205,7 @@ class PolicyTemplateGeneratorUnittest(unittest.TestCase): ] } ] - class LocalMockWriter(MockWriter): + class LocalMockWriter(mock_writer.MockWriter): def WritePolicy(self, policy): if policy['name'] == 'Policy1': self.tester.assertEquals(policy['caption'], 'string1') @@ -211,13 +242,79 @@ class PolicyTemplateGeneratorUnittest(unittest.TestCase): }] } ] - class LocalMockWriter(MockWriter): + class LocalMockWriter(mock_writer.MockWriter): def WritePolicy(self, policy): self.tester.assertEquals(policy['items'][0]['caption'], 'string1') self.tester.assertEquals(policy['items'][1]['caption'], 'string2') self.tester.assertEquals(policy['items'][2]['caption'], 'string3') self.do_test(messages_mock, policy_groups_mock, LocalMockWriter()) + def testPolicyFiltering(self): + # Test that policies are filtered correctly based on their annotations. + messages_mock = { + 'IDS_POLICY_GROUP_GROUP1_CAPTION': None, + 'IDS_POLICY_GROUP_GROUP1_DESC': None, + 'IDS_POLICY_GROUP_GROUP2_CAPTION': None, + 'IDS_POLICY_GROUP_GROUP2_DESC': None, + 'IDS_POLICY_GROUP1POLICY1_CAPTION': None, + 'IDS_POLICY_GROUP1POLICY2_CAPTION': None, + 'IDS_POLICY_GROUP2POLICY3_CAPTION': None, + } + policy_groups_mock = [{ + 'name': 'Group1', + 'policies': [ + { + 'name': 'Group1Policy1', + 'type': 'string', + 'annotations': {'platforms': ['aaa', 'bbb', 'ccc']} + }, + { + 'name': 'Group1Policy2', + 'type': 'string', + 'annotations': {'platforms': ['ddd']} + }, + ] + },{ + 'name': 'Group2', + 'policies': [ + { + 'name': 'Group2Policy3', + 'type': 'string', + 'annotations': {'platforms': ['eee']} + }, + ] + }] + class LocalMockWriter(mock_writer.MockWriter): + def __init__(self, platforms, expected_list): + 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.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( + messages_mock, + policy_groups_mock, + LocalMockWriter(['eee'], ['begin_Group2', 'Group2Policy3'])) + self.do_test( + messages_mock, + policy_groups_mock, + LocalMockWriter( + ['ddd', 'bbb'], + ['begin_Group1', 'Group1Policy1', 'Group1Policy2'])) if __name__ == '__main__': unittest.main() diff --git a/tools/grit/grit/format/policy_templates/writers/adm_writer.py b/tools/grit/grit/format/policy_templates/writers/adm_writer.py index 68987fb..e3a079f 100644 --- a/tools/grit/grit/format/policy_templates/writers/adm_writer.py +++ b/tools/grit/grit/format/policy_templates/writers/adm_writer.py @@ -6,12 +6,12 @@ from grit.format.policy_templates.writers import template_writer -def GetWriter(info, messages): +def GetWriter(config, messages): '''Factory method for creating AdmWriter objects. See the constructor of TemplateWriter for description of arguments. ''' - return AdmWriter(info, messages) + return AdmWriter(['win'], config, messages) class AdmWriter(template_writer.TemplateWriter): diff --git a/tools/grit/grit/format/policy_templates/writers/adm_writer_unittest.py b/tools/grit/grit/format/policy_templates/writers/adm_writer_unittest.py index ec47e33..c82abcb 100644 --- a/tools/grit/grit/format/policy_templates/writers/adm_writer_unittest.py +++ b/tools/grit/grit/format/policy_templates/writers/adm_writer_unittest.py @@ -71,6 +71,7 @@ chromium="Chromium"''' 'policies': [{ 'name': 'MainPolicy', 'type': 'main', + 'annotations': {'platforms': ['win']} }], }, ], @@ -119,6 +120,7 @@ MainGroup_Explain="Description of main."''' 'policies': [{ 'name': 'StringPolicy', 'type': 'string', + 'annotations': {'platforms': ['win']} }], }, ], @@ -172,7 +174,8 @@ StringPolicy_Part="Caption of policy." 'items': [ {'name': 'ProxyServerDisabled', 'value': '0'}, {'name': 'ProxyServerAutoDetect', 'value': '1'}, - ] + ], + 'annotations': {'platforms': ['win']} }], }, ], @@ -234,6 +237,7 @@ ProxyServerAutoDetect_DropDown="Option2" 'policies': [{ 'name': 'ListPolicy', 'type': 'list', + 'annotations': {'platforms': ['win']} }], }, ], @@ -275,6 +279,42 @@ ListPolicy_Part="Caption of list policy." ''' self.CompareOutputs(output, expected_output) + def testNonSupportedPolicy(self): + # Tests a policy that is not supported on Windows, so it shouldn't + # be included in the ADM file. + grd = self.PrepareTest(''' + { + 'policy_groups': [ + { + 'name': 'NonWinGroup', + 'policies': [{ + 'name': 'NonWinPolicy', + 'type': 'list', + 'annotations': {'platforms': ['linux', 'mac']} + }], + }, + ], + 'placeholders': [], + }''', ''' + <messages> + <message name="IDS_POLICY_GROUP_NONWINGROUP_CAPTION">Group caption.</message> + <message name="IDS_POLICY_GROUP_NONWINGROUP_DESC">Group description.</message> + <message name="IDS_POLICY_NONWINPOLICY_CAPTION">Caption of list policy.</message> + <message name="IDS_POLICY_WIN_SUPPORTED_WINXPSP2">At least Windows 3.16</message> + </messages> + ''') + output = self.GetOutput(grd, 'fr', {'_chromium' : '1'}, 'adm', 'en') + expected_output = '''CLASS MACHINE + CATEGORY !!chromium + KEYNAME "Software\\Policies\\Chromium" + + END CATEGORY + +[Strings] +SUPPORTED_WINXPSP2="At least Windows 3.16" +chromium="Chromium" +''' + self.CompareOutputs(output, expected_output) if __name__ == '__main__': unittest.main() diff --git a/tools/grit/grit/format/policy_templates/writers/adml_writer.py b/tools/grit/grit/format/policy_templates/writers/adml_writer.py index fa40104..c6af9f1 100644 --- a/tools/grit/grit/format/policy_templates/writers/adml_writer.py +++ b/tools/grit/grit/format/policy_templates/writers/adml_writer.py @@ -6,12 +6,12 @@ from xml.dom import minidom from grit.format.policy_templates.writers import xml_formatted_writer -def GetWriter(info, messages): +def GetWriter(config, messages): '''Factory method for instanciating the ADMLWriter. Every Writer needs a GetWriter method because the TemplateFormatter uses this method to instantiate a Writer. ''' - return ADMLWriter(info, messages) + return ADMLWriter(['win'], config, messages) class ADMLWriter(xml_formatted_writer.XMLFormattedWriter): diff --git a/tools/grit/grit/format/policy_templates/writers/adml_writer_unittest.py b/tools/grit/grit/format/policy_templates/writers/adml_writer_unittest.py index e345ae0..c21ab65 100644 --- a/tools/grit/grit/format/policy_templates/writers/adml_writer_unittest.py +++ b/tools/grit/grit/format/policy_templates/writers/adml_writer_unittest.py @@ -31,7 +31,7 @@ class AdmlWriterTest(xml_writer_base_unittest.XmlWriterBaseTest): messages = { 'IDS_POLICY_WIN_SUPPORTED_WINXPSP2': 'Supported on Test OS or higher' } - self.writer = adml_writer.ADMLWriter(config, messages) + self.writer = adml_writer.GetWriter(config, messages) self.writer.Prepare() def _InitWriterForAddingPolicyGroups(self, writer): @@ -181,6 +181,19 @@ class AdmlWriterTest(xml_writer_base_unittest.XmlWriterBaseTest): '</listBox>') self.AssertXMLEquals(output, expected_output) + def testPlatform(self): + # Test that the writer correctly chooses policies of platform Windows. + self.assertTrue(self.writer.IsPolicySupported({ + 'annotations': { + 'platforms': ['win', 'zzz', 'aaa'] + } + })) + self.assertFalse(self.writer.IsPolicySupported({ + 'annotations': { + 'platforms': ['mac', 'linux', 'aaa'] + } + })) + if __name__ == '__main__': unittest.main() diff --git a/tools/grit/grit/format/policy_templates/writers/admx_writer.py b/tools/grit/grit/format/policy_templates/writers/admx_writer.py index 5eb67ca..e266944 100644 --- a/tools/grit/grit/format/policy_templates/writers/admx_writer.py +++ b/tools/grit/grit/format/policy_templates/writers/admx_writer.py @@ -11,7 +11,7 @@ def GetWriter(config, messages): GetWriter method because the TemplateFormatter uses this method to instantiate a Writer. ''' - return ADMXWriter(config, messages) + return ADMXWriter(['win'], config, messages) class ADMXWriter(xml_formatted_writer.XMLFormattedWriter): diff --git a/tools/grit/grit/format/policy_templates/writers/admx_writer_unittest.py b/tools/grit/grit/format/policy_templates/writers/admx_writer_unittest.py index 532733c..634d0b5 100644 --- a/tools/grit/grit/format/policy_templates/writers/admx_writer_unittest.py +++ b/tools/grit/grit/format/policy_templates/writers/admx_writer_unittest.py @@ -40,7 +40,7 @@ class AdmxWriterTest(xml_writer_base_unittest.XmlWriterBaseTest): } # Grit messages. messages = {} - self.writer = admx_writer.ADMXWriter(config, messages) + self.writer = admx_writer.GetWriter(config, messages) self.writer.Prepare() def testEmpty(self): @@ -155,6 +155,19 @@ class AdmxWriterTest(xml_writer_base_unittest.XmlWriterBaseTest): self.AssertXMLEquals(output, expected_output) + def testPlatform(self): + # Test that the writer correctly chooses policies of platform Windows. + self.assertTrue(self.writer.IsPolicySupported({ + 'annotations': { + 'platforms': ['win', 'zzz', 'aaa'] + } + })) + self.assertFalse(self.writer.IsPolicySupported({ + 'annotations': { + 'platforms': ['mac', 'linux', 'aaa'] + } + })) + if __name__ == '__main__': unittest.main() diff --git a/tools/grit/grit/format/policy_templates/writers/mock_writer.py b/tools/grit/grit/format/policy_templates/writers/mock_writer.py index fd1f8c9..97b9e47 100644 --- a/tools/grit/grit/format/policy_templates/writers/mock_writer.py +++ b/tools/grit/grit/format/policy_templates/writers/mock_writer.py @@ -34,5 +34,8 @@ class MockWriter(TemplateWriter): def GetTemplateText(self): pass + def IsPolicySupported(self, policy): + return True + def Test(self): pass diff --git a/tools/grit/grit/format/policy_templates/writers/plist_strings_writer.py b/tools/grit/grit/format/policy_templates/writers/plist_strings_writer.py index 51c43e9..faec9f8 100644 --- a/tools/grit/grit/format/policy_templates/writers/plist_strings_writer.py +++ b/tools/grit/grit/format/policy_templates/writers/plist_strings_writer.py @@ -11,7 +11,7 @@ def GetWriter(config, messages): See the constructor of TemplateWriter for description of arguments. ''' - return PListStringsWriter(config, messages) + return PListStringsWriter(['mac'], config, messages) class PListStringsWriter(template_writer.TemplateWriter): diff --git a/tools/grit/grit/format/policy_templates/writers/plist_strings_writer_unittest.py b/tools/grit/grit/format/policy_templates/writers/plist_strings_writer_unittest.py index d8b3506..b604511 100644 --- a/tools/grit/grit/format/policy_templates/writers/plist_strings_writer_unittest.py +++ b/tools/grit/grit/format/policy_templates/writers/plist_strings_writer_unittest.py @@ -57,6 +57,7 @@ Chromium.pfm_description = "Chromium preferen\\"ces"; 'policies': [{ 'name': 'MainPolicy', 'type': 'main', + 'annotations': {'platforms': ['mac']}, }], }, ], @@ -93,6 +94,7 @@ MainPolicy.pfm_description = "Title of main."; 'policies': [{ 'name': 'StringPolicy', 'type': 'string', + 'annotations': {'platforms': ['mac']}, }], }, ], @@ -133,7 +135,8 @@ StringPolicy.pfm_description = "Description of group.\\nWith a newline."; 'items': [ {'name': 'ProxyServerDisabled', 'value': '0'}, {'name': 'ProxyServerAutoDetect', 'value': '1'}, - ] + ], + 'annotations': {'platforms': ['mac']}, }], }, ], @@ -163,6 +166,42 @@ EnumPolicy.pfm_description = "0 - Option1\\n1 - Option2\\nDescription of policy. ''' self.assertEquals(output.strip(), expected_output.strip()) + def testNonSupportedPolicy(self): + # Tests a policy that is not supported on Mac, so its strings shouldn't + # be included in the plist string table. + grd = self.PrepareTest(''' + { + 'policy_groups': [ + { + 'name': 'NonMacGroup', + 'policies': [{ + 'name': 'NonMacPolicy', + 'type': 'string', + 'annotations': {'platforms': ['win', 'linux']}, + }], + }, + ], + 'placeholders': [], + }''', ''' + <messages> + <message name="IDS_POLICY_GROUP_NONMACGROUP_CAPTION">Caption of group.</message> + <message name="IDS_POLICY_GROUP_NONMACGROUP_DESC">Description of group.</message> + <message name="IDS_POLICY_NONMACPOLICY_CAPTION">Caption of policy.</message> + <message name="IDS_POLICY_MAC_CHROME_PREFERENCES">$1 preferences</message> + </messages> + ''' ) + output = self.GetOutput( + grd, + 'fr', + {'_google_chrome': '1', 'mac_bundle_id': 'com.example.Test2'}, + 'plist_strings', + 'en') + expected_output = \ +'''Google Chrome.pfm_title = "Google Chrome"; +Google Chrome.pfm_description = "Google Chrome preferences"; + ''' + self.assertEquals(output.strip(), expected_output.strip()) + if __name__ == '__main__': unittest.main() diff --git a/tools/grit/grit/format/policy_templates/writers/plist_writer.py b/tools/grit/grit/format/policy_templates/writers/plist_writer.py index aff1d05..37deb94 100644 --- a/tools/grit/grit/format/policy_templates/writers/plist_writer.py +++ b/tools/grit/grit/format/policy_templates/writers/plist_writer.py @@ -12,7 +12,7 @@ def GetWriter(config, messages): See the constructor of TemplateWriter for description of arguments. ''' - return PListWriter(config, messages) + return PListWriter(['mac'], config, messages) class PListWriter(xml_formatted_writer.XMLFormattedWriter): diff --git a/tools/grit/grit/format/policy_templates/writers/plist_writer_unittest.py b/tools/grit/grit/format/policy_templates/writers/plist_writer_unittest.py index 35af4a6..157a258 100644 --- a/tools/grit/grit/format/policy_templates/writers/plist_writer_unittest.py +++ b/tools/grit/grit/format/policy_templates/writers/plist_writer_unittest.py @@ -87,6 +87,7 @@ class PListWriterUnittest(writer_unittest_common.WriterUnittestCommon): 'policies': [{ 'name': 'MainPolicy', 'type': 'main', + 'annotations': {'platforms': ['mac']}, }], }, ], @@ -132,6 +133,7 @@ class PListWriterUnittest(writer_unittest_common.WriterUnittestCommon): 'policies': [{ 'name': 'StringPolicy', 'type': 'string', + 'annotations': {'platforms': ['mac']}, }], }, ], @@ -182,7 +184,8 @@ class PListWriterUnittest(writer_unittest_common.WriterUnittestCommon): 'items': [ {'name': 'ProxyServerDisabled', 'value': '0'}, {'name': 'ProxyServerAutoDetect', 'value': '1'}, - ] + ], + 'annotations': {'platforms': ['mac']}, }], }, ], @@ -227,6 +230,39 @@ class PListWriterUnittest(writer_unittest_common.WriterUnittestCommon): </array>''') self.assertEquals(output.strip(), expected_output.strip()) + def testNonSupportedPolicy(self): + # Tests a policy that is not supported on Mac, so it shouldn't + # be included in the plist file. + grd = self.PrepareTest(''' + { + 'policy_groups': [ + { + 'name': 'NonMacGroup', + 'policies': [{ + 'name': 'NonMacPolicy', + 'type': 'string', + 'annotations': {'platforms': ['win', 'linux']}, + }], + }, + ], + 'placeholders': [], + }''', ''' + <messages> + <message name="IDS_POLICY_GROUP_NONMACGROUP_CAPTION">This is not tested here. (1)</message> + <message name="IDS_POLICY_GROUP_NONMACGROUP_DESC">This is not tested here. (2)</message> + <message name="IDS_POLICY_NONMACPOLICY_CAPTION">This is not tested here. (3)</message> + </messages> + ''' ) + output = self.GetOutput( + grd, + 'fr', + {'_google_chrome': '1', 'mac_bundle_id': 'com.example.Test2'}, + 'plist', + 'en') + expected_output = \ + self._GetExpectedOutputs('Google Chrome', 'com.example.Test2', '''<array/>''') + self.assertEquals(output.strip(), expected_output.strip()) + 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 7894d06..ccc730a 100644 --- a/tools/grit/grit/format/policy_templates/writers/template_writer.py +++ b/tools/grit/grit/format/policy_templates/writers/template_writer.py @@ -8,10 +8,11 @@ class TemplateWriter(object): The methods of this class will be called by PolicyTemplateGenerator. ''' - def __init__(self, config, messages): + def __init__(self, platforms, config, messages): '''Initializes a TemplateWriter object. Args: + platforms: List of platforms for which this writer can write policies. config: A dictionary of information required to generate the template. It contains some key-value pairs, including the following examples: 'build': 'chrome' or 'chromium' @@ -25,6 +26,7 @@ class TemplateWriter(object): IDS_POLICY_WIN_SUPPORTED_WINXPSP2 message in ADM files, because that cannot be associated with any policy or group. ''' + self.platforms = platforms self.config = config self.messages = messages @@ -78,3 +80,17 @@ class TemplateWriter(object): The generated template from the the internal buffer as a string. ''' raise NotImplementedError() + + def IsPolicySupported(self, policy): + '''Checks if the writer is interested in writing a given policy. + + Args: + policy: The dictionary of the policy. + + Returns: + True if the writer chooses to include 'policy' in its output. + ''' + for platform in self.platforms: + if platform in policy['annotations']['platforms']: + return True + return False |