diff options
Diffstat (limited to 'tools')
7 files changed, 241 insertions, 161 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 efbf769..165cc91 100644 --- a/tools/grit/grit/format/policy_templates/policy_template_generator.py +++ b/tools/grit/grit/format/policy_templates/policy_template_generator.py @@ -22,19 +22,20 @@ class PolicyTemplateGenerator: policy_groups: The policies are organized into groups, and each policy has a name and type. The user will see a GUI interface for assigning values to policies, by using the templates generated here. The traversed - data structure is a dictionary of policy group names to groups. Each - group is a dictionary and has an embedded dictionary of policies under - the key 'policies'. Example: - policy_groups = { - 'PolicyGroup1': { - 'policies': { - 'Policy1Name': {'type': 'string'} - 'Policy2Name': {'type': 'main'} - 'Policy3Name': {'type': 'enum', 'items': {...}} - } + data structure is a list of policy groups. Each group is a dictionary + and has an embedded list of policies under the key 'policies'. + Example: + policy_groups = [ + { + 'name': 'PolicyGroup1', + 'policies': [ + {'name': 'Policy1Name', 'type': 'string'} + {'name': 'Policy2Name', 'type': 'main'} + {'name': 'Policy3Name', 'type': 'enum', 'items': [...]} + ] }, - 'PolicyGroup2': {...} - } + {'name': 'PolicyGroup2', ...} + ] See chrome/app/policy.policy_templates.json for an example and more details. ''' @@ -44,54 +45,69 @@ class PolicyTemplateGenerator: self._messages = messages self._AddMessagesToPolicies() - def _AddMessagesToItem(self, item_name, item, warn_no_desc): + def _AddMessagesToItem(self, item_name, item, needs_caption, needs_desc): '''Adds localized message strings to an item of the policy data structure (self._policy_groups). Args: - item_name: The name of the item. + item_name: The base of the grd name of the item. item: The item which will get its message strings now. - warn_no_desc: A boolean value. If it is True, warnings will be - printed in case there are no description strings for item in - self._messages. + needs_caption: A boolean value. If it is True, an exception will be raised + in case there is no caption string for item in self._messages. + needs_desc: A boolean value. If it is True, an exception will be raised + in case there is no description string for item in self._messages. + + Raises: + Exception() if a required message string was not found. ''' # The keys for the item's messages in self._messages: - caption_id = 'IDS_POLICY_%s_CAPTION' % item_name - desc_id = 'IDS_POLICY_%s_DESC' % item_name + caption_name = 'IDS_POLICY_%s_CAPTION' % item_name + desc_name = 'IDS_POLICY_%s_DESC' % item_name # Copy the messages from self._messages to item: - if caption_id in self._messages: - item['caption'] = self._messages[caption_id] + if caption_name in self._messages: + item['caption'] = self._messages[caption_name] + elif needs_caption: + raise Exception('No localized caption for %s (missing %s).' % + (item_name, caption_name)) + if desc_name in self._messages: + item['desc'] = self._messages[desc_name] + elif needs_desc: + raise Exception('No localized description for %s (missing %s).' % + (item_name, desc_name)) + + def _AddMessagesToPolicy(self, policy): + '''Adds localized message strings to a policy. + + Args: + policy: The data structure of the policy that will get message strings + here. + ''' + full_name = policy['name'].upper() + if policy['type'] == 'main': + # In this case, both caption and description are optional. + self._AddMessagesToItem(full_name, policy, False, False) else: - print 'Warning: no localized message for ' + caption_id - if desc_id in self._messages: - item['desc'] = self._messages[desc_id] - elif warn_no_desc: - print 'Warning: no localized message for ' + desc_id + # Add caption for this policy. + self._AddMessagesToItem(full_name, policy, True, False) + if policy['type'] == 'enum': + # Iterate through all the items of an enum-type policy, and add captions. + for item in policy['items']: + self._AddMessagesToItem('ENUM_' + item['name'].upper(), item, + True, False) def _AddMessagesToPolicies(self): '''Adds localized message strings to each item of the policy data structure (self._policy_groups). ''' # Iterate through all the policy groups. - for group_name, group in self._policy_groups.iteritems(): + for group in self._policy_groups: # Get caption and description for this group. - group_name = 'GROUP_' + group_name.upper() - self._AddMessagesToItem(group_name, group, True) + group_name = 'GROUP_' + group['name'].upper() + self._AddMessagesToItem(group_name, group, True, True) if 'policies' in group: # Iterate through all the policies in the current group. - for policy_name, policy in group['policies'].iteritems(): - if policy['type'] == 'enum': - # Iterate through all the items of an enum-type policy. - for item_name, item in policy['items'].iteritems(): - self._AddMessagesToItem('ENUM_' + item_name.upper(), item, False) - elif policy['type'] == 'main': - # In this case, messages are inherited from the group. - policy['caption'] = group['caption'] - policy['desc'] = group['desc'] - continue - # Get caption for this policy. - full_name = policy_name.upper() - self._AddMessagesToItem(full_name, policy, False) + for policy in group['policies']: + self._AddMessagesToPolicy(policy) def GetTemplateText(self, template_writer): '''Generates the text of the template from the arguments given @@ -106,11 +122,11 @@ class PolicyTemplateGenerator: ''' template_writer.Prepare() template_writer.BeginTemplate() - for group_name, group in self._policy_groups.iteritems(): - template_writer.BeginPolicyGroup(group_name, group) + for group in self._policy_groups: + template_writer.BeginPolicyGroup(group) if 'policies' in group: - for policy_name, policy in group['policies'].iteritems(): - template_writer.WritePolicy(policy_name, policy) + for policy in group['policies']: + template_writer.WritePolicy(policy) 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 7f5923b9..61debe5 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 @@ -12,7 +12,7 @@ from writers.mock_writer import MockWriter class PolicyTemplateGeneratorUnittest(unittest.TestCase): '''Unit tests for policy_template_generator.py.''' - def do_test(self, strings, policy_groups, writer): + def do_test(self, messages, policy_groups, writer): '''Executes a test case. Creates and invokes an instance of PolicyTemplateGenerator with @@ -30,7 +30,7 @@ class PolicyTemplateGeneratorUnittest(unittest.TestCase): mock_writer.MockWriter. ''' writer.tester = self - policy_generator = PolicyTemplateGenerator(strings, policy_groups) + policy_generator = PolicyTemplateGenerator(messages, policy_groups) res = policy_generator.GetTemplateText(writer) writer.Test() return res @@ -59,7 +59,7 @@ class PolicyTemplateGeneratorUnittest(unittest.TestCase): def testEmptyGroups(self): # Test that in case of three empty policy groups, each one is passed to # the writer. - strings_mock = { + messages_mock = { 'IDS_POLICY_GROUP_GROUP1_CAPTION': None, 'IDS_POLICY_GROUP_GROUP1_DESC': None, 'IDS_POLICY_GROUP_GROUP2_CAPTION': None, @@ -67,18 +67,18 @@ class PolicyTemplateGeneratorUnittest(unittest.TestCase): 'IDS_POLICY_GROUP_GROUP3_CAPTION': None, 'IDS_POLICY_GROUP_GROUP3_DESC': None, } - policies_mock = { - 'Group1': {'tag': 'T1'}, - 'Group2': {'tag': 'T2'}, - 'Group3': {'tag': 'T3'}, - } + policies_mock = [ + {'name': 'Group1', 'tag': 'T1'}, + {'name': 'Group2', 'tag': 'T2'}, + {'name': 'Group3', 'tag': 'T3'}, + ] class LocalMockWriter(MockWriter): def __init__(self): self.log = '' self.set = set() - def BeginPolicyGroup(self, group_name, group): + def BeginPolicyGroup(self, group): self.log += '[' - self.set.add ( (group_name, group['tag']) ) + self.set.add ( (group['name'], group['tag']) ) def EndPolicyGroup(self): self.log += ']' def Test(self): @@ -86,35 +86,35 @@ class PolicyTemplateGeneratorUnittest(unittest.TestCase): self.tester.assertEquals( self.set, set([('Group1', 'T1'), ('Group2', 'T2'), ('Group3', 'T3')])) - self.do_test(strings_mock, policies_mock, LocalMockWriter()) + self.do_test(messages_mock, policies_mock, LocalMockWriter()) def testGroupTexts(self): # Test that GUI strings are assigned correctly to policy groups. - strings_mock = { + messages_mock = { 'IDS_POLICY_GROUP_GROUP1_CAPTION': 'string1', 'IDS_POLICY_GROUP_GROUP1_DESC': 'string2', 'IDS_POLICY_GROUP_GROUP2_CAPTION': 'string3', 'IDS_POLICY_GROUP_GROUP2_DESC': 'string4', } - policies_mock = { - 'Group1': {}, - 'Group2': {}, - } + policy_groups_mock = [ + {'name': 'Group1'}, + {'name': 'Group2'}, + ] class LocalMockWriter(MockWriter): - def BeginPolicyGroup(self, group_name, group): - if group_name == 'Group1': + def BeginPolicyGroup(self, group): + if group['name'] == 'Group1': self.tester.assertEquals(group['caption'], 'string1') self.tester.assertEquals(group['desc'], 'string2') - elif group_name == 'Group2': + elif group['name'] == 'Group2': self.tester.assertEquals(group['caption'], 'string3') self.tester.assertEquals(group['desc'], 'string4') else: self.tester.fail() - self.do_test(strings_mock, policies_mock, LocalMockWriter()) + self.do_test(messages_mock, policy_groups_mock, LocalMockWriter()) def testPolicies(self): # Test that policies are passed correctly to the writer. - strings_mock = { + messages_mock = { 'IDS_POLICY_GROUP_GROUP1_CAPTION': None, 'IDS_POLICY_GROUP_GROUP1_DESC': None, 'IDS_POLICY_GROUP_GROUP2_CAPTION': None, @@ -123,39 +123,41 @@ class PolicyTemplateGeneratorUnittest(unittest.TestCase): 'IDS_POLICY_GROUP1POLICY2_CAPTION': None, 'IDS_POLICY_GROUP2POLICY3_CAPTION': None, } - policies_mock = { - 'Group1': { - 'policies': { - 'Group1Policy1': {'type': 'string'}, - 'Group1Policy2': {'type': 'string'} - } + policy_groups_mock = [ + { + 'name': 'Group1', + 'policies': [ + {'name': 'Group1Policy1', 'type': 'string'}, + {'name': 'Group1Policy2', 'type': 'string'}, + ] }, - 'Group2': { - 'policies': { - 'Group2Policy3': {'type': 'string'} - } + { + 'name': 'Group2', + 'policies': [ + {'name': 'Group2Policy3', 'type': 'string'}, + ] } - } + ] class LocalMockWriter(MockWriter): def __init__(self): self.policy_name = None self.policy_set = set() - def BeginPolicyGroup(self, group_name, group): - self.group_name = group_name + def BeginPolicyGroup(self, group): + self.group = group; def EndPolicyGroup(self): - self.group_name = None - def WritePolicy(self, group_name, group): - self.tester.assertEquals(group_name[0:6], self.group_name) - self.policy_set.add(group_name) + self.group = None + def WritePolicy(self, group): + self.tester.assertEquals(group['name'][0:6], self.group['name']) + self.policy_set.add(group['name']) def Test(self): self.tester.assertEquals( self.policy_set, set(['Group1Policy1', 'Group1Policy2', 'Group2Policy3'])) - self.do_test(strings_mock, policies_mock, LocalMockWriter()) + self.do_test(messages_mock, policy_groups_mock, LocalMockWriter()) def testPolicyTexts(self): # Test that GUI strings are assigned correctly to policies. - strings_mock = { + messages_mock = { 'IDS_POLICY_POLICY1_CAPTION': 'string1', 'IDS_POLICY_POLICY1_DESC': 'string2', 'IDS_POLICY_POLICY2_CAPTION': 'string3', @@ -163,30 +165,31 @@ class PolicyTemplateGeneratorUnittest(unittest.TestCase): 'IDS_POLICY_GROUP_GROUP1_CAPTION': None, 'IDS_POLICY_GROUP_GROUP1_DESC': None, } - policies_mock = { - 'Group1': { - 'policies': { - 'Policy1': {'type': 'string'}, - 'Policy2': {'type': 'string'} - } + policy_groups_mock = [ + { + 'name': 'Group1', + 'policies': [ + {'name': 'Policy1', 'type': 'string'}, + {'name': 'Policy2', 'type': 'string'} + ] } - } + ] class LocalMockWriter(MockWriter): - def WritePolicy(self, policy_name, policy): - if policy_name == 'Policy1': + def WritePolicy(self, policy): + if policy['name'] == 'Policy1': self.tester.assertEquals(policy['caption'], 'string1') self.tester.assertEquals(policy['desc'], 'string2') - elif policy_name == 'Policy2': + elif policy['name'] == 'Policy2': self.tester.assertEquals(policy['caption'], 'string3') self.tester.assertEquals(policy['desc'], 'string4') else: self.tester.fail() - self.do_test(strings_mock, policies_mock, LocalMockWriter()) + self.do_test(messages_mock, policy_groups_mock, LocalMockWriter()) def testEnumTexts(self): # Test that GUI strings are assigned correctly to enums # (aka dropdown menus). - strings_mock = { + messages_mock = { 'IDS_POLICY_ENUM_ITEM1_CAPTION': 'string1', 'IDS_POLICY_ENUM_ITEM2_CAPTION': 'string2', 'IDS_POLICY_ENUM_ITEM3_CAPTION': 'string3', @@ -194,26 +197,26 @@ class PolicyTemplateGeneratorUnittest(unittest.TestCase): 'IDS_POLICY_GROUP_GROUP1_CAPTION': None, 'IDS_POLICY_GROUP_GROUP1_DESC': None, } - policies_mock = { - 'Group1': { - 'policies': { - 'Policy1': { - 'type': 'enum', - 'items': { - 'item1': {'value': '0'}, - 'item2': {'value': '1'}, - 'item3': {'value': '3'}, - } - } - } + policy_groups_mock = [ + { + 'name': 'Group1', + 'policies': [{ + 'name': 'Policy1', + 'type': 'enum', + 'items': [ + {'name': 'item1', 'value': '0'}, + {'name': 'item2', 'value': '1'}, + {'name': 'item3', 'value': '3'}, + ] + }] } - } + ] class LocalMockWriter(MockWriter): - def WritePolicy(self, policy_name, policy): - self.tester.assertEquals(policy['items']['item1']['caption'], 'string1') - self.tester.assertEquals(policy['items']['item2']['caption'], 'string2') - self.tester.assertEquals(policy['items']['item3']['caption'], 'string3') - self.do_test(strings_mock, policies_mock, LocalMockWriter()) + 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()) if __name__ == '__main__': diff --git a/tools/grit/grit/format/policy_templates/template_formatter.py b/tools/grit/grit/format/policy_templates/template_formatter.py index 47fd474..2e5633a 100644 --- a/tools/grit/grit/format/policy_templates/template_formatter.py +++ b/tools/grit/grit/format/policy_templates/template_formatter.py @@ -58,7 +58,7 @@ class TemplateFormatter(interface.ItemFormatter): return '' self._lang = lang - self._SetBranding(item) + self._GetFlags(item) self._policy_groups = None self._messages = {} self._ParseGritNodes(item) @@ -74,24 +74,28 @@ class TemplateFormatter(interface.ItemFormatter): ''' policy_generator = \ PolicyTemplateGenerator(self._messages, self._policy_groups) - writer = self._writer_module.GetWriter(self._build) + writer = self._writer_module.GetWriter(self._info, self._messages) str = policy_generator.GetTemplateText(writer) return str - def _SetBranding(self, item): - '''Sets self._branding and self._build based on the -D_chromium or - -D_google_chrome command line switch of grit. + def _GetFlags(self, item): + '''Sets values in self._info based on the -D flags + passed to grit. Args: item: The <grit> root node of the grit tree. ''' defines = item.defines + + self._info = {} + if 'mac_bundle_id' in defines: + self._info['mac_bundle_id'] = defines['mac_bundle_id'] if '_chromium' in defines: - self._branding = 'Chromium' - self._build = 'chromium' + self._info['app_name'] = 'Chromium' + self._info['build'] = 'chromium' elif '_google_chrome' in defines: - self._branding = 'Google Chrome' - self._build = 'chrome' + self._info['app_name'] = 'Google Chrome' + self._info['build'] = 'chrome' else: raise Exception('Unknown build') @@ -104,7 +108,7 @@ class TemplateFormatter(interface.ItemFormatter): ''' msg_name = message.GetTextualIds()[0] msg_txt = message.Translate(self._lang) - msg_txt = msg_txt.replace('$1', self._branding) + msg_txt = msg_txt.replace('$1', self._info['app_name']) lines = msg_txt.split('\n') lines = [line.strip() for line in lines] msg_txt = "\\n".join(lines) 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 e7b7c36..51dea06 100644 --- a/tools/grit/grit/format/policy_templates/writers/adm_writer.py +++ b/tools/grit/grit/format/policy_templates/writers/adm_writer.py @@ -6,8 +6,8 @@ from template_writer import TemplateWriter -def GetWriter(build): - return AdmWriter(build) +def GetWriter(info, messages): + return AdmWriter(info, messages) class AdmWriter(TemplateWriter): @@ -17,7 +17,8 @@ class AdmWriter(TemplateWriter): TYPE_TO_INPUT = { 'string': 'EDITTEXT', - 'enum': 'DROPDOWNLIST'} + 'enum': 'DROPDOWNLIST', + 'list': 'LISTBOX'} NEWLINE = '\r\n' def _AddGuiString(self, name, value): @@ -45,9 +46,10 @@ class AdmWriter(TemplateWriter): self._PrintLine('SUPPORTED !!SUPPORTED_WINXPSP2') self._PrintLine('#endif', -1) - def WritePolicy(self, policy_name, policy): - type = policy['type'] - if type == 'main': + def WritePolicy(self, policy): + policy_type = policy['type'] + policy_name = policy['name'] + if policy_type == 'main': self._PrintLine('VALUENAME "%s"' % policy_name ) self._PrintLine('VALUEON NUMERIC 1') self._PrintLine('VALUEOFF NUMERIC 0') @@ -57,25 +59,31 @@ class AdmWriter(TemplateWriter): self._AddGuiString(policy_part_name, policy['caption']) self._PrintLine() + if policy_type == 'list': + self._PrintLine('KEYNAME "%s\\%s"' % (self._key_name, policy_name)) + # Print the PART ... END PART section: self._PrintLine( - 'PART !!%s %s' % (policy_part_name, self.TYPE_TO_INPUT[type]), + 'PART !!%s %s' % (policy_part_name, self.TYPE_TO_INPUT[policy_type]), 1) - self._PrintLine('VALUENAME "%s"' % policy_name) - if type == 'enum': + if policy_type == 'list': + self._PrintLine('VALUEPREFIX ""') + else: + self._PrintLine('VALUENAME "%s"' % policy_name) + if policy_type == 'enum': self._PrintLine('ITEMLIST', 1) - for item_name, item in policy['items'].iteritems(): - self._PrintLine( - 'NAME !!%s_DropDown VALUE NUMERIC %s' % (item_name, item['value'])) - self._AddGuiString(item_name + '_DropDown', item['caption']) + for item in policy['items']: + self._PrintLine('NAME !!%s_DropDown VALUE NUMERIC %s' % + (item['name'], item['value'])) + self._AddGuiString(item['name'] + '_DropDown', item['caption']) self._PrintLine('END ITEMLIST', -1) self._PrintLine('END PART', -1) - def BeginPolicyGroup(self, group_name, group): - group_explain_name = group_name + '_Explain' - self._AddGuiString(group_name + '_Policy', group['caption']) + def BeginPolicyGroup(self, group): + group_explain_name = group['name'] + '_Explain' + self._AddGuiString(group['name'] + '_Policy', group['caption']) self._AddGuiString(group_explain_name, group['desc']) - self._PrintLine('POLICY !!%s_Policy' % group_name, 1) + self._PrintLine('POLICY !!%s_Policy' % group['name'], 1) self._WriteSupported() self._PrintLine('EXPLAIN !!' + group_explain_name) @@ -84,28 +92,28 @@ class AdmWriter(TemplateWriter): self._PrintLine() def BeginTemplate(self): - # TODO(gfeher): Move this string to .grd. self._AddGuiString('SUPPORTED_WINXPSP2', - 'At least Microsoft Windows XP SP2') + self.messages['IDS_POLICY_WIN_SUPPORTED_WINXPSP2']) self._PrintLine('CLASS MACHINE', 1) - if self.build == 'chrome': + if self.info['build'] == 'chrome': self._AddGuiString('google', 'Google') self._AddGuiString('googlechrome', 'Google Chrome') self._PrintLine('CATEGORY !!google', 1) self._PrintLine('CATEGORY !!googlechrome', 1) - self._PrintLine('KEYNAME "Software\\Policies\\Google\\Google Chrome"') - elif self.build == 'chromium': + self._key_name = 'Software\\Policies\\Google\\Google Chrome' + elif self.info['build'] == 'chromium': self._AddGuiString('chromium', 'Chromium') self._PrintLine('CATEGORY !!chromium', 1) - self._PrintLine('KEYNAME "Software\\Policies\\Chromium"') + self._key_name = 'Software\\Policies\\Chromium' + self._PrintLine('KEYNAME "%s"' % self._key_name) self._PrintLine() def EndTemplate(self): - if self.build == 'chrome': + if self.info['build'] == 'chrome': self._PrintLine('END CATEGORY', -1) self._PrintLine('END CATEGORY', -1) self._PrintLine('', -1) - elif self.build == 'chromium': + elif self.info['build'] == 'chromium': self._PrintLine('END CATEGORY', -1) self._PrintLine('', -1) 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 8700b68..fd1f8c9 100644 --- a/tools/grit/grit/format/policy_templates/writers/mock_writer.py +++ b/tools/grit/grit/format/policy_templates/writers/mock_writer.py @@ -13,10 +13,10 @@ class MockWriter(TemplateWriter): def __init__(self): pass - def WritePolicy(self, policy_name, policy): + def WritePolicy(self, policy): pass - def BeginPolicyGroup(self, group_name, group): + def BeginPolicyGroup(self, group): pass def EndPolicyGroup(self): 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 cb80fb2..b853b7d 100644 --- a/tools/grit/grit/format/policy_templates/writers/template_writer.py +++ b/tools/grit/grit/format/policy_templates/writers/template_writer.py @@ -8,14 +8,25 @@ class TemplateWriter(object): The methods of this class will be called by PolicyTemplateGenerator. ''' - def __init__(self, build): + def __init__(self, info, messages): '''Initializes a TemplateWriter object. Args: - build: 'chrome' or 'chromium' + info: A dictionary of extra information required to generate the template. + Currently it contains three keys: + 'build': 'chrome' or 'chromium' + 'branding': 'Google Chrome' or 'Chromium' + 'mac_bundle_id': The Mac bundle id of Chrome. (Only set when building + for Mac.) + messages: List of all the message strings from the grd file. Most of them + are also present in the policy data structures that are passed to + methods. That is the preferred way of accessing them, this should only + be used in exceptional cases. An example for its use is the + IDS_POLICY_WIN_SUPPORTED_WINXPSP2 message in ADM files, because that + can not be associated with any policy or group. ''' - assert build in ['chrome', 'chromium'] - self.build = build + self.info = info + self.messages = messages def Prepare(self): '''Initializes the internal buffer where the template will be @@ -23,22 +34,20 @@ class TemplateWriter(object): ''' raise NotImplementedError() - def WritePolicy(self, policy_name, policy): + def WritePolicy(self, policy): '''Appends the template text corresponding to a policy into the internal buffer. Args: - policy_name: The name of the policy that has to be written. policy: The policy as it is found in the JSON file. ''' raise NotImplementedError() - def BeginPolicyGroup(self, group_name, group): + def BeginPolicyGroup(self, group): '''Appends the template text corresponding to the beginning of a policy group into the internal buffer. Args: - group_name: The name of the policy group that has to be written. group: The policy group as it is found in the JSON file. ''' raise NotImplementedError() diff --git a/tools/grit/grit/format/policy_templates/writers/xml_formatted_writer.py b/tools/grit/grit/format/policy_templates/writers/xml_formatted_writer.py new file mode 100644 index 0000000..6b9a475 --- /dev/null +++ b/tools/grit/grit/format/policy_templates/writers/xml_formatted_writer.py @@ -0,0 +1,40 @@ +# 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.minidom import Document +from xml.dom.minidom import Node +from template_writer import TemplateWriter + + +class XMLFormattedWriter(TemplateWriter): + '''Helper class for generating XML-based templates. + ''' + + def AddElement(self, parent, name, attrs = {}, text = None): + ''' + Adds a new XML Element as a child to an existing element or the Document. + + Args: + parent: An XML element or the document, where the new element will be + added. + name: The name of the new element. + attrs: A dictionary of the attributes' names and values for the new + element. + text: Text content for the new element. + + Returns: + The created new element. + ''' + if isinstance(parent, Document): + doc = parent + else: + doc = parent.ownerDocument + element = doc.createElement(name) + for key, value in attrs.iteritems(): + element.attributes[key] = value + if text: + element.appendChild( doc.createTextNode(text) ) + parent.appendChild(element) + return element |