diff options
author | markusheintz@chromium.org <markusheintz@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-09-27 12:05:29 +0000 |
---|---|---|
committer | markusheintz@chromium.org <markusheintz@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-09-27 12:05:29 +0000 |
commit | 9bb297aa85d5684b17d11bbcbc26d1a893bbe43f (patch) | |
tree | e29560ccc601d295b85711cea980360cd01ed1bd /tools/grit | |
parent | 49d24eb92bfe3f6fe1b397d21e7021bd9a20ca65 (diff) | |
download | chromium_src-9bb297aa85d5684b17d11bbcbc26d1a893bbe43f.zip chromium_src-9bb297aa85d5684b17d11bbcbc26d1a893bbe43f.tar.gz chromium_src-9bb297aa85d5684b17d11bbcbc26d1a893bbe43f.tar.bz2 |
Change the ADMX/ADML Writer to map policy groups to "category" elements and policies to "policy" element. This will fix issue 55722 for ADMX/ADML policy templates.
BUG=55722
TEST=admx_writer_unittest.py, adml_writer_unittest.py
Review URL: http://codereview.chromium.org/3439011
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@60629 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools/grit')
5 files changed, 341 insertions, 334 deletions
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 b18ac39..de4c82d 100644 --- a/tools/grit/grit/format/policy_templates/writers/adml_writer.py +++ b/tools/grit/grit/format/policy_templates/writers/adml_writer.py @@ -29,11 +29,6 @@ class ADMLWriter(xml_formatted_writer.XMLFormattedWriter): # describe the presentation of Policy-Groups and Policies. _presentation_table_elem = None - # The active ADML "presentation" element. At any given point in time this - # contains the "presentation" element for the Policy-Group that is processed. - _active_presentation_elem = None - - def _AddString(self, parent, id, text): ''' Adds an ADML "string" element to the passed parent. The following ADML snippet contains an example: @@ -48,128 +43,61 @@ class ADMLWriter(xml_formatted_writer.XMLFormattedWriter): string_elem = self.AddElement(parent, 'string', {'id': id}) string_elem.appendChild(self._doc.createTextNode(text)) - def _AddStringPolicy(self, presentation_elem, policy_name, caption): - '''Generates ADML elements for a String-Policy. A String-Policy does not - require any ADML "string" elements. The presentation of a String-Policy is - defined by an ADML "textBox" element. The text-box contains an ADML "label" - element that contains the text of the text-box label. The following ADML - snipped shows an example: - - <presentation ...> - ... - <textBox refId="$(policy_name)"> - <label>$(caption)</label> - </textBox> - </presentation> - - Args: - presentation_elem: The ADML "presentation" element of the policy group - that includes the policy. - policy_name: Policy name. - caption: Caption assisiated with the Policy. - ''' - # A String-Policy requires no additional ADML "string" elements. - - # Add ADML "presentation" elements that are required by a String-Policy to - # the presentation-table. - textbox_elem = self.AddElement(presentation_elem, 'textBox', - {'refId': policy_name}) - label_elem = self.AddElement(textbox_elem, 'label') - label_elem.appendChild(self._doc.createTextNode(caption)) - - def _AddEnumPolicy(self, string_table_elem, presentation_elem, - policy_name, caption, items): - '''Generates ADML elements for an Enum-Policy. For each enum item an ADML - "string" element is added to the string-table. The presentation of an - Enum-Policy is defined by the ADML "dropdownList" element. The following - ADML snippet shows an example: - - <stringTable> - ... - <string id="$(item_name)">$(description)</string> - </stringTable> - - <presentation ...> - ... - <dropdownList refId="$(policy_name)">$(caption)</dropdownlist> - </presentation> - - Args: - string_table_elem: The ADML "stringTable" element to which the ADML - "string" elements are added. - presentation_elem: The ADML "presentation" element of the policy group - that includes the policy. - policy_name: Policy name. - caption: Caption associated with the Policy. - items: The enum items. - ''' - # Add ADML "string" elements to the string-table that are required by an - # Enum-Policy. - for item in items: - self._AddString(string_table_elem, item['name'], item['caption']) - - # Add ADML "presentation" elements to the presentation-table that are - # required by an Enum-Policy. - dropdownlist_elem = self.AddElement(presentation_elem, 'dropdownList', - {'refId': policy_name}) - dropdownlist_elem.appendChild(self._doc.createTextNode(caption)) - - def _AddListPolicy(self, string_table_elem, presentation_elem, policy_name, - caption): - '''Generates ADML elements for a List-Policy. Each List-Policy requires an - additional ADML "string" element that contains a description of the items - that can be added to the list. The presentation of a List-Policy is defined - by an ADML "listBox" element. The following ADML snippet shows an example: - + def WritePolicy(self, policy): + '''Generates the ADML elements for a Policy. <stringTable> ... - <string id="$(policy_name)Desc">$(caption)</string> + <string id="$(policy_group_name)">$(caption)</string> + <string id="$(policy_group_name)_Explain">$(description)</string> </stringTable> - <presentation ...> + <presentationTables> ... - <listBox refId="$(policy_name)Desc">$(caption)</listBox> - </presentation> - - Args: - string_table_elem: The ADML "stringTable" element to which the ADML - "string" elements are added. - presentation_elem: The ADML "presentation" element of the policy group - that includes the policy. - policy_name: Policy name. - caption: Caption assisiated with the Policy. - ''' - # Add ADML "string" elements to the string-table that are required by a - # List-Policy. - self._AddString(string_table_elem, policy_name + 'Desc', caption) - - # Add ADML "presentation" elements to the presentation-table that are - # required by a List-Policy. - listbox_elem = self.AddElement(presentation_elem, 'listBox', - {'refId': policy_name + 'Desc'}) - listbox_elem.appendChild(self._doc.createTextNode(caption)) - - def WritePolicy(self, policy): - '''Generates the ADML elements for a Policy. + <presentation id=$(policy_group_name)/> + </presentationTables> Args: policy: The Policy to generate ADML elements for. ''' policy_type = policy['type'] policy_name = policy['name'] - presentation_elem = self._active_presentation_elem - string_table_elem = self._string_table_elem + if 'caption' in policy: + policy_caption = policy['caption'] + else: + policy_caption = policy_name + if 'desc' in policy: + policy_description = policy['desc'] + elif 'desc' in self._active_group: + policy_description = self._active_group['desc'] + else: + policy_description = policy_name + + self._AddString(self._string_table_elem, policy_name, policy_caption) + self._AddString(self._string_table_elem, policy_name + '_Explain', + policy_description) + presentation_elem = self.AddElement( + self._presentation_table_elem, 'presentation', {'id': policy_name}) + if policy_type == 'main': - # Nothing needs to be done for a Main-Policy. pass elif policy_type == 'string': - self._AddStringPolicy(presentation_elem, policy_name, policy['caption']) + textbox_elem = self.AddElement(presentation_elem, 'textBox', + {'refId': policy_name}) + label_elem = self.AddElement(textbox_elem, 'label') + label_elem.appendChild(self._doc.createTextNode(policy_caption)) elif policy_type == 'enum': - self._AddEnumPolicy(string_table_elem, presentation_elem, policy_name, - policy['caption'], policy['items']) + for item in policy['items']: + self._AddString(self._string_table_elem, item['name'], item['caption']) + dropdownlist_elem = self.AddElement(presentation_elem, 'dropdownList', + {'refId': policy_name}) + dropdownlist_elem.appendChild(self._doc.createTextNode(policy_caption)) elif policy_type == 'list': - self._AddListPolicy(string_table_elem, presentation_elem, policy_name, - policy['caption']) + self._AddString(self._string_table_elem, + policy_name + 'Desc', + policy_caption) + listbox_elem = self.AddElement(presentation_elem, 'listBox', + {'refId': policy_name + 'Desc'}) + listbox_elem.appendChild(self._doc.createTextNode(policy_caption)) else: raise Exception('Unknown policy type %s.' % policy_type) @@ -182,30 +110,15 @@ class ADMLWriter(xml_formatted_writer.XMLFormattedWriter): elements that define the visual presentation of the Policy-Goup's Policies. The following ADML snippet shows an example: - <stringTable> - ... - <string id="$(policy_group_name)">$(caption)</string> - <string id="$(policy_group_name)_Explain">$(description)</string> - </stringTable> - - <presentationTables> - ... - <presentation id=$(policy_group_name)/> - </presentationTables> - Args: group: The Policy-Group to generate ADML elements for. ''' - # Add ADML "string" elements to the string-table that are required by a - # Policy-Group. - id = group['name'] - self._AddString(self._string_table_elem, id, group['caption']) - self._AddString(self._string_table_elem, id + '_Explain', group['desc']) - - # Add ADML "presentation" elements to the presentation-table that are - # required by a Policy-Group. - self._active_presentation_elem = self.AddElement( - self._presentation_table_elem, 'presentation', {'id': id}) + self._active_group = group; + if len(group['policies']) > 1: + # Add ADML "string" elements to the string-table that are required by a + # Policy-Group. + id = group['name'] + '_group' + self._AddString(self._string_table_elem, id, group['name']) def _AddBaseStrings(self, string_table_elem, build): ''' Adds ADML "string" elements to the string-table that are referenced by 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 6c2a9c2..d6924ca 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 @@ -39,23 +39,26 @@ class AdmlWriterTest(xml_writer_base_unittest.XmlWriterBaseTest): called before the method "BeginPolicyGroup" can be called. It initializes attributes of the writer. ''' - dom_impl = minidom.getDOMImplementation('') - writer._doc = dom_impl.createDocument( - None, 'policyDefinitionRessources', None) - writer._presentation_table_elem = self.writer.AddElement( - writer._doc.documentElement, 'presentationTable') - writer._string_table_elem = self.writer.AddElement( - writer._doc.documentElement, 'stringTable') - - def _InitWriterForAddingPolicies(self, writer): + writer.BeginTemplate() + + def _InitWriterForAddingPolicies(self, writer, policy): '''Initialize the writer for adding policies. This method must be called before the method "WritePolicy" can be called. It initializes attributes of the writer. ''' self._InitWriterForAddingPolicyGroups(writer) - writer._active_presentation_elem = self.writer.AddElement( - self.writer._presentation_table_elem, 'presentation', - {'id': 'PolicyGroupStub'}) + policy_group = { + 'name': 'PolicyGroup', + 'caption': 'Test Caption', + 'desc': 'This is the test description of the test policy group.', + 'policies': policy, + } + writer.BeginPolicyGroup(policy_group) + + string_elements =\ + self.writer._string_table_elem.getElementsByTagName('string') + for elem in string_elements: + self.writer._string_table_elem.removeChild(elem) def testEmpty(self): self.writer.BeginTemplate() @@ -70,64 +73,89 @@ class AdmlWriterTest(xml_writer_base_unittest.XmlWriterBaseTest): self.AssertXMLEquals(output, expected_output) def testPolicyGroup(self): - self._InitWriterForAddingPolicyGroups(self.writer) empty_policy_group = { 'name': 'PolicyGroup', 'caption': 'Test Caption', 'desc': 'This is the test description of the test policy group.', + 'policies': [ + {'name': 'PolicyStub2', + 'type': 'main'}, + {'name': 'PolicyStub1', + 'type': 'main'}, + ], } + self._InitWriterForAddingPolicyGroups(self.writer) self.writer.BeginPolicyGroup(empty_policy_group) self.writer.EndPolicyGroup # Assert generated string elements. output = self.GetXMLOfChildren(self.writer._string_table_elem) expected_output = ( - '<string id="PolicyGroup">\n Test Caption\n</string>\n<string' - ' id="PolicyGroup_Explain">\n This is the test description of the' - ' test policy group.\n</string>') + '<string id="SUPPORTED_TESTOS">\n' + ' Supported on Test OS or higher\n' + '</string>\n' + '<string id="PolicyGroup_group">\n' + ' PolicyGroup\n' + '</string>') self.AssertXMLEquals(output, expected_output) # Assert generated presentation elements. output = self.GetXMLOfChildren(self.writer._presentation_table_elem) - expected_output = '<presentation id="PolicyGroup"/>' + expected_output = '' self.AssertXMLEquals(output, expected_output) def testMainPolicy(self): - self. _InitWriterForAddingPolicies(self.writer) main_policy = { 'name': 'DummyMainPolicy', 'type': 'main', } + self. _InitWriterForAddingPolicies(self.writer, main_policy) self.writer.WritePolicy(main_policy) # Assert generated string elements. output = self.GetXMLOfChildren(self.writer._string_table_elem) - expected_output = '' + expected_output = ( + '<string id="DummyMainPolicy">\n' + ' DummyMainPolicy\n' + '</string>\n' + '<string id="DummyMainPolicy_Explain">\n' + ' This is the test description of the test policy group.\n' + '</string>') self.AssertXMLEquals(output, expected_output) # Assert generated presentation elements. - output = self.GetXMLOfChildren(self.writer._active_presentation_elem) - expected_output = '' + output = self.GetXMLOfChildren(self.writer._presentation_table_elem) + expected_output = '<presentation id="DummyMainPolicy"/>' self.AssertXMLEquals(output, expected_output) def testStringPolicy(self): - self. _InitWriterForAddingPolicies(self.writer) string_policy = { 'name': 'StringPolicyStub', 'type': 'string', 'caption': 'String Policy Caption', 'desc': 'This is a test description.', } + self. _InitWriterForAddingPolicies(self.writer, string_policy) self.writer.WritePolicy(string_policy) # Assert generated string elements. output = self.GetXMLOfChildren(self.writer._string_table_elem) - expected_output = '' + expected_output = ( + '<string id="StringPolicyStub">\n' + ' String Policy Caption\n' + '</string>\n' + '<string id="StringPolicyStub_Explain">\n' + ' This is a test description.\n' + '</string>') self.AssertXMLEquals(output, expected_output) # Assert generated presentation elements. - output = self.GetXMLOfChildren(self.writer._active_presentation_elem) + output = self.GetXMLOfChildren(self.writer._presentation_table_elem) expected_output = ( - '<textBox refId="StringPolicyStub">\n <label>\n' - ' String Policy Caption\n </label>\n</textBox>') + '<presentation id="StringPolicyStub">\n' + ' <textBox refId="StringPolicyStub">\n' + ' <label>\n' + ' String Policy Caption\n' + ' </label>\n' + ' </textBox>\n' + '</presentation>') self.AssertXMLEquals(output, expected_output) def testEnumPolicy(self): - self. _InitWriterForAddingPolicies(self.writer) enum_policy = { 'name': 'EnumPolicyStub', 'type': 'enum', @@ -146,39 +174,64 @@ class AdmlWriterTest(xml_writer_base_unittest.XmlWriterBaseTest): }, ], } + self. _InitWriterForAddingPolicies(self.writer, enum_policy) self.writer.WritePolicy(enum_policy) # Assert generated string elements. output = self.GetXMLOfChildren(self.writer._string_table_elem) expected_output = ( - '<string id="item 1">\n Caption Item 1\n</string>\n<string id="item' - ' 2">\n Caption Item 2\n</string>') + '<string id="EnumPolicyStub">\n' + ' Enum Policy Caption\n' + '</string>\n' + '<string id="EnumPolicyStub_Explain">\n' + ' This is a test description.\n' + '</string>\n' + '<string id="item 1">\n' + ' Caption Item 1\n' + '</string>\n' + '<string id="item 2">\n' + ' Caption Item 2\n' + '</string>') self.AssertXMLEquals(output, expected_output) # Assert generated presentation elements. - output = self.GetXMLOfChildren(self.writer._active_presentation_elem) + output = self.GetXMLOfChildren(self.writer._presentation_table_elem) expected_output = ( - '<dropdownList refId="EnumPolicyStub">\n Enum Policy Caption\n' - '</dropdownList>') + '<presentation id="EnumPolicyStub">\n' + ' <dropdownList refId="EnumPolicyStub">\n' + ' Enum Policy Caption\n' + ' </dropdownList>\n' + '</presentation>') self.AssertXMLEquals(output, expected_output) def testListPolicy(self): - self. _InitWriterForAddingPolicies(self.writer) list_policy = { 'name': 'ListPolicyStub', 'type': 'list', 'caption': 'List Policy Caption', 'desc': 'This is a test description.', } + self. _InitWriterForAddingPolicies(self.writer, list_policy) self.writer.WritePolicy(list_policy) # Assert generated string elements. output = self.GetXMLOfChildren(self.writer._string_table_elem) expected_output = ( - '<string id="ListPolicyStubDesc">\n List Policy Caption\n</string>') + '<string id="ListPolicyStub">\n' + ' List Policy Caption\n' + '</string>\n' + '<string id="ListPolicyStub_Explain">\n' + ' This is a test description.\n' + '</string>\n' + '<string id="ListPolicyStubDesc">\n' + ' List Policy Caption\n' + '</string>') self.AssertXMLEquals(output, expected_output) # Assert generated presentation elements. - output = self.GetXMLOfChildren(self.writer._active_presentation_elem) + output = self.GetXMLOfChildren(self.writer._presentation_table_elem) expected_output = ( - '<listBox refId="ListPolicyStubDesc">\n List Policy Caption\n' - '</listBox>') + '<presentation id="ListPolicyStub">\n' + ' <listBox refId="ListPolicyStubDesc">\n' + ' List Policy Caption\n' + ' </listBox>\n' + '</presentation>') self.AssertXMLEquals(output, expected_output) def testPlatform(self): 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 11a2007..f48680e 100644 --- a/tools/grit/grit/format/policy_templates/writers/admx_writer.py +++ b/tools/grit/grit/format/policy_templates/writers/admx_writer.py @@ -26,11 +26,6 @@ class ADMXWriter(xml_formatted_writer.XMLFormattedWriter): # are generated. _active_policies_elem = None - # The active ADMX "policy" element. At any point in time, the active policy - # element contains the element being generated for the Policy-Group currently - # being processed. - _active_policy_elem = None - def _AdmlString(self, name): '''Creates a reference to the named string in an ADML file. Args: @@ -119,12 +114,12 @@ class ADMXWriter(xml_formatted_writer.XMLFormattedWriter): for the root level, each level refers to its parent. Since the root level category has no parent it does not require a parent reference. ''' - categories_elem = self.AddElement(parent, 'categories') + self._categories_elem = self.AddElement(parent, 'categories') category_name = None for category in categories: parent_category_name = category_name category_name = category - self._AddCategory(categories_elem, category_name, + self._AddCategory(self._categories_elem, category_name, self._AdmlString(category_name), parent_category_name) def _AddSupportedOn(self, parent, supported_os): @@ -155,29 +150,7 @@ class ADMXWriter(xml_formatted_writer.XMLFormattedWriter): def _AddStringPolicy(self, parent, name): '''Generates ADMX elements for a String-Policy and adds them to the - passed parent node. A String-Policy is mapped to an ADMX "text" element. - The parent of the ADMX "text" element must be an ADMX "elements" element, - which must by a child of the ADMX "policy" element that corresponds to the - Policy-Group that contains the String-Policy. The following example shows - how the JSON specification of the String-Policy "DisabledPluginsList" of the - Policy-Group "DisabledPlugins" is mapped to ADMX: - - { - 'name': 'DisabledPlugins', - 'policies': [{ - 'name': 'DisabledPluginsList', - 'type' : 'string', - }, ... ] - } - - <policy ... name="DisabledPlugins" ...> - ... - <elements> - <text id="DisabledPluginsList" valueName="DisabledPluginsList"/> - ... - </elements> - </policy> - + passed parent node. ''' attributes = { 'id': name, @@ -187,44 +160,7 @@ class ADMXWriter(xml_formatted_writer.XMLFormattedWriter): def _AddEnumPolicy(self, parent, name, items): '''Generates ADMX elements for an Enum-Policy and adds them to the - passed parent element. An Enum-Policy is mapped to an ADMX "enum" element. - The parent of the "enum" element must be the ADMX "elements" element of the - ADMX "policy" element that corresponds to the Policy-Group that contains the - Enum-Policy. The following example shows how the JSON specification of the - Enum-Policy "ProxyServerMode" of the Policy-Group "Proxy" is mapped to ADMX: - - { - 'name': 'Proxy', - 'policies': [{ - 'name': 'ProxyServerMode', - 'type': 'enum', - 'items': [ - {'name': 'ProxyServerDisabled', 'value': '0'}, - {'name': 'ProxyServerAutoDetect', 'value': '1'}, - ... - ], - }, ... ] - } - - <policy ... name="Proxy" ...> - ... - <elements> - <enum id="ProxyServerMode" valueName="ProxyServerMode"> - <item displayName="$(string.ProxyServerDisabled)"> - <value> - <decimal value="0"/> - </value> - </item> - <item displayName="$(string.ProxyServerAutoDetect)"> - <value> - <decimal value="1"/> - </value> - </item> - ... - </enum> - ... - </elements> - </policy> + passed parent element. ''' attributes = { 'id': name, @@ -240,27 +176,7 @@ class ADMXWriter(xml_formatted_writer.XMLFormattedWriter): def _AddListPolicy(self, parent, name): '''Generates ADMX XML elements for a List-Policy and adds them to the - passed parent element. A List-Policy is mapped to an ADMX "list" element. - The parent of the "list" element must be the ADMX "elements" element of the - ADMX "policy" element that corresponds to the Policy-Group that contains the - List-Policy. The following example shows how the JSON specification of the - List-Policy "ExtensionInstallBlacklist" of the Policy-Group - "ExtensionInstallBlacklist" is mapped to ADMX: - - { - 'name': 'ExtensionInstallBlacklist', - 'policies': [{ - 'name': 'ExtensionInstallBlacklist', - 'type': 'list', - }] - }, - - <policy ... name="ExtensionInstallBlacklist" ...> - ... - <elements> - <list id="ExtensionInstallBlacklistDesc" valuePrefix=""/> - </elements> - </policy> + passed parent element. ''' attributes = { # The ID must be in sync with ID of the corresponding element in the ADML @@ -307,46 +223,61 @@ class ADMXWriter(xml_formatted_writer.XMLFormattedWriter): '''Generates AMDX elements for a Policy. There are four different policy types: Main-Policy, String-Policy, Enum-Policy and List-Policy. ''' + policies_elem = self._active_policies_elem policy_type = policy['type'] policy_name = policy['name'] + + attributes = { + 'name': policy_name, + 'class': self.config['win_group_policy_class'], + 'displayName': self._AdmlString(policy_name), + 'explainText': self._AdmlStringExplain(policy_name), + 'presentation': self._AdmlPresentation(policy_name), + 'key': self.config['win_reg_key_name'], + } + # Store the current "policy" AMDX element in self for later use by the + # WritePolicy method. + policy_elem = self.AddElement(policies_elem, 'policy', + attributes) + self.AddElement(policy_elem, 'parentCategory', + {'ref': self._active_policy_group_name}) + self.AddElement(policy_elem, 'supportedOn', + {'ref': self.config['win_supported_os']}) if policy_type == 'main': - self._AddMainPolicy(self._active_policy_elem) + self.AddAttribute(policy_elem, 'valueName', policy_name) + self._AddMainPolicy(policy_elem) elif policy_type == 'string': - parent = self._GetElements(self._active_policy_elem) + parent = self._GetElements(policy_elem) self._AddStringPolicy(parent, policy_name) elif policy_type == 'enum': - parent = self._GetElements(self._active_policy_elem) + parent = self._GetElements(policy_elem) self._AddEnumPolicy(parent, policy_name, policy['items']) elif policy_type == 'list': - parent = self._GetElements(self._active_policy_elem) + parent = self._GetElements(policy_elem) self._AddListPolicy(parent, policy_name) else: raise Exception('Unknown policy type %s.' % policy_type) def BeginPolicyGroup(self, group): - '''Generates ADMX elements for a Policy-Group and adds them to the ADMX - policies element. A Policy-Group is mapped to an ADMX "policy" element. All - ADMX policy elements are grouped under the ADMX "policies" element. + '''Generates ADMX elements for a Policy-Group. ''' - policies_elem = self._active_policies_elem - policy_group_name = group['name'] - attributes = { - 'name': policy_group_name, - 'class': self.config['win_group_policy_class'], - 'displayName': self._AdmlString(policy_group_name), - 'explainText': self._AdmlStringExplain(policy_group_name), - 'presentation': self._AdmlPresentation(policy_group_name), - 'key': self.config['win_reg_key_name'], - 'valueName': policy_group_name, - } - # Store the current "policy" AMDX element in self for later use by the - # WritePolicy method. - self._active_policy_elem = self.AddElement(policies_elem, 'policy', - attributes) - self.AddElement(self._active_policy_elem, 'parentCategory', - {'ref': self.config['win_category_path'][-1]}) - self.AddElement(self._active_policy_elem, 'supportedOn', - {'ref': self.config['win_supported_os']}) + self._active_policy_group_name = self.config['win_category_path'][-1] + # If a policy group contains more than one policy then create a new + # category. + if len(group['policies']) > 1: + policy_group_name = group['name'] + attributes = { + 'name': policy_group_name, + 'displayName': self._AdmlString(policy_group_name + '_group'), + } + category_elem = self.AddElement(self._categories_elem, + 'category', + attributes) + attributes = { + 'ref': self.config['win_category_path'][-1], + } + self.AddElement(category_elem, 'parentCategory', attributes) + self._active_policy_group_name = policy_group_name def BeginTemplate(self): '''Generates the skeleton of the ADMX template. An ADMX template contains 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 dcf58b3..04352c7 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 @@ -43,60 +43,135 @@ class AdmxWriterTest(xml_writer_base_unittest.XmlWriterBaseTest): self.writer = admx_writer.GetWriter(config, messages) self.writer.Init() + def _GetPoliciesElement(self, doc): + node_list = doc.getElementsByTagName('policies') + self.assertTrue(node_list.length == 1) + return node_list.item(0) + + def _GetCategoriesElement(self, doc): + node_list = doc.getElementsByTagName('categories') + self.assertTrue(node_list.length == 1) + return node_list.item(0) + def testEmpty(self): self.writer.BeginTemplate() self.writer.EndTemplate() output = self.writer.GetTemplateText() expected_output = ( - '<?xml version="1.0" ?>\n<policyDefinitions revision="1.0"' - ' schemaVersion="1.0">\n <policyNamespaces>\n <target' - ' namespace="ADMXWriter.Test.Namespace" prefix="test_prefix"/>\n' - ' <using namespace="Microsoft.Policies.Windows" prefix="windows"/>' - '\n </policyNamespaces>\n <resources' - ' minRequiredRevision="1.0"/>\n <supportedOn>\n' - ' <definitions>\n <definition displayName="' + '<?xml version="1.0" ?>\n' + '<policyDefinitions revision="1.0" schemaVersion="1.0">\n' + ' <policyNamespaces>\n' + ' <target namespace="ADMXWriter.Test.Namespace"' + ' prefix="test_prefix"/>\n' + ' <using namespace="Microsoft.Policies.Windows" prefix="windows"/>\n' + ' </policyNamespaces>\n' + ' <resources minRequiredRevision="1.0"/>\n' + ' <supportedOn>\n' + ' <definitions>\n' + ' <definition displayName="' '$(string.SUPPORTED_TESTOS)" name="SUPPORTED_TESTOS"/>\n' - ' </definitions>\n </supportedOn>\n <categories>\n <category' - ' displayName="$(string.test_category)" name="test_category"/>\n' - ' </categories>\n <policies/>\n</policyDefinitions>') + ' </definitions>\n' + ' </supportedOn>\n' + ' <categories>\n' + ' <category displayName="$(string.test_category)"' + ' name="test_category"/>\n' + ' </categories>\n' + ' <policies/>\n' + '</policyDefinitions>') self.AssertXMLEquals(output, expected_output) - def testPolicyGroup(self): - parent_elem = self._CreateDocumentElement() - self.writer._active_policies_elem = parent_elem + def testEmptyPolicyGroup(self): + empty_policy_group = { + 'name': 'PolicyGroup', + 'policies': [] + } + # Initialize writer to write a policy group. + self.writer.BeginTemplate() + # Write policy group + self.writer.BeginPolicyGroup(empty_policy_group) + self.writer.EndPolicyGroup() + + output = self.GetXMLOfChildren(self._GetPoliciesElement(self.writer._doc)) + expected_output = '' + self.AssertXMLEquals(output, expected_output) + + output = self.GetXMLOfChildren( + self._GetCategoriesElement(self.writer._doc)) + expected_output = ( + '<category displayName="$(string.test_category)"' + ' name="test_category"/>') + self.AssertXMLEquals(output, expected_output) + def testPolicyGroup(self): empty_policy_group = { - 'name': 'PolicyGroup' + 'name': 'PolicyGroup', + 'policies': [ + {'name': 'PolicyStub2', + 'type': 'main'}, + {'name': 'PolicyStub1', + 'type': 'main'}, + ] } + # Initialize writer to write a policy group. + self.writer.BeginTemplate() + # Write policy group self.writer.BeginPolicyGroup(empty_policy_group) self.writer.EndPolicyGroup() - output = self.GetXMLOfChildren(parent_elem) + output = self.GetXMLOfChildren(self._GetPoliciesElement(self.writer._doc)) + expected_output = '' + self.AssertXMLEquals(output, expected_output) + + output = self.GetXMLOfChildren( + self._GetCategoriesElement(self.writer._doc)) expected_output = ( - '<policy class="TestClass" displayName="$(string.PolicyGroup)"' - ' explainText="$(string.PolicyGroup_Explain)" key=' - '"Software\Policies\Test" name="PolicyGroup"' - ' presentation="$(presentation.PolicyGroup)" valueName="PolicyGroup">' - '\n <parentCategory ref="test_category"/>\n' - ' <supportedOn ref="SUPPORTED_TESTOS"/>\n</policy>') + '<category displayName="$(string.test_category)"' + ' name="test_category"/>\n' + '<category displayName="$(string.PolicyGroup_group)"' + ' name="PolicyGroup">\n' + ' <parentCategory ref="test_category"/>\n' + '</category>') self.AssertXMLEquals(output, expected_output) + + def _initWriterForPolicy(self, writer, policy): + '''Initializes the writer to write the given policy next. + ''' + policy_group = { + 'name': 'PolicyGroup', + 'policies': [policy] + } + writer.BeginTemplate() + writer.BeginPolicyGroup(policy_group) + def testMainPolicy(self): main_policy = { 'name': 'DummyMainPolicy', 'type': 'main', } - parent_elem = self._CreateDocumentElement() - self.writer._active_policy_elem = parent_elem + self._initWriterForPolicy(self.writer, main_policy) self.writer.WritePolicy(main_policy) - output = self.GetXMLOfChildren(parent_elem) + output = self.GetXMLOfChildren(self._GetPoliciesElement(self.writer._doc)) expected_output = ( - '<enabledValue>\n <decimal value="1"/>\n</enabledValue>\n' - '<disabledValue>\n <decimal value="0"/>\n</disabledValue>') + '<policy class="TestClass" displayName="$(string.DummyMainPolicy)"' + ' explainText="$(string.DummyMainPolicy_Explain)"' + ' key="Software\\Policies\\Test" name="DummyMainPolicy"' + ' presentation="$(presentation.DummyMainPolicy)"' + ' valueName="DummyMainPolicy">\n' + ' <parentCategory ref="test_category"/>\n' + ' <supportedOn ref="SUPPORTED_TESTOS"/>\n' + ' <enabledValue>\n' + ' <decimal value="1"/>\n' + ' </enabledValue>\n' + ' <disabledValue>\n' + ' <decimal value="0"/>\n' + ' </disabledValue>\n' + '</policy>') + self.AssertXMLEquals(output, expected_output) def testStringPolicy(self): @@ -104,21 +179,24 @@ class AdmxWriterTest(xml_writer_base_unittest.XmlWriterBaseTest): 'name': 'SampleStringPolicy', 'type': 'string', } - parent_elem = self.writer.AddElement(self._CreateDocumentElement(), - 'policy') - self.writer._active_policy_elem = parent_elem + self._initWriterForPolicy(self.writer, string_policy) + self.writer.WritePolicy(string_policy) - output = self.GetXMLOfChildren(parent_elem) + output = self.GetXMLOfChildren(self._GetPoliciesElement(self.writer._doc)) expected_output = ( - '<elements>\n <text id="SampleStringPolicy"' - ' valueName="SampleStringPolicy"/>\n</elements>') + '<policy class="TestClass" displayName="$(string.SampleStringPolicy)"' + ' explainText="$(string.SampleStringPolicy_Explain)"' + ' key="Software\\Policies\\Test" name="SampleStringPolicy"' + ' presentation="$(presentation.SampleStringPolicy)">\n' + ' <parentCategory ref="test_category"/>\n' + ' <supportedOn ref="SUPPORTED_TESTOS"/>\n' + ' <elements>\n' + ' <text id="SampleStringPolicy" valueName="SampleStringPolicy"/>\n' + ' </elements>\n' + '</policy>') self.AssertXMLEquals(output, expected_output) def testEnumPolicy(self): - parent_elem = self.writer.AddElement(self._CreateDocumentElement(), - 'policy') - self.writer._active_policy_elem = parent_elem - enum_policy = { 'name': 'SampleEnumPolicy', 'type': 'enum', @@ -127,15 +205,32 @@ class AdmxWriterTest(xml_writer_base_unittest.XmlWriterBaseTest): {'name': 'item 2', 'value': '1'}, ] } + + self._initWriterForPolicy(self.writer, enum_policy) self.writer.WritePolicy(enum_policy) - output = self.GetXMLOfChildren(parent_elem) + output = self.GetXMLOfChildren(self._GetPoliciesElement(self.writer._doc)) expected_output = ( - '<elements>\n <enum id="SampleEnumPolicy" valueName=' - '"SampleEnumPolicy">\n <item displayName="$(string.item 1)">\n' - ' <value>\n <decimal value="0"/>\n </value>\n' - ' </item>\n <item displayName="$(string.item 2)">\n <value>' - '\n <decimal value="1"/>\n </value>\n </item>' - '\n </enum>\n</elements>') + '<policy class="TestClass" displayName="$(string.SampleEnumPolicy)"' + ' explainText="$(string.SampleEnumPolicy_Explain)"' + ' key="Software\\Policies\\Test" name="SampleEnumPolicy"' + ' presentation="$(presentation.SampleEnumPolicy)">\n' + ' <parentCategory ref="test_category"/>\n' + ' <supportedOn ref="SUPPORTED_TESTOS"/>\n' + ' <elements>\n' + ' <enum id="SampleEnumPolicy" valueName="SampleEnumPolicy">\n' + ' <item displayName="$(string.item 1)">\n' + ' <value>\n' + ' <decimal value="0"/>\n' + ' </value>\n' + ' </item>\n' + ' <item displayName="$(string.item 2)">\n' + ' <value>\n' + ' <decimal value="1"/>\n' + ' </value>\n' + ' </item>\n' + ' </enum>\n' + ' </elements>\n' + '</policy>') self.AssertXMLEquals(output, expected_output) def testListPolicy(self): @@ -143,15 +238,21 @@ class AdmxWriterTest(xml_writer_base_unittest.XmlWriterBaseTest): 'name': 'SampleListPolicy', 'type': 'list', } - parent_elem = self.writer.AddElement(self._CreateDocumentElement(), - 'policy') - self.writer._active_policy_elem = parent_elem + self._initWriterForPolicy(self.writer, list_policy) self.writer.WritePolicy(list_policy) - output = self.GetXMLOfChildren(parent_elem) + output = self.GetXMLOfChildren(self._GetPoliciesElement(self.writer._doc)) expected_output = ( - '<elements>\n <list id="SampleListPolicyDesc"' - ' key="Software\Policies\Test\SampleListPolicy"' - ' valuePrefix=""/>\n</elements>') + '<policy class="TestClass" displayName="$(string.SampleListPolicy)"' + ' explainText="$(string.SampleListPolicy_Explain)"' + ' key="Software\\Policies\\Test" name="SampleListPolicy"' + ' presentation="$(presentation.SampleListPolicy)">\n' + ' <parentCategory ref="test_category"/>\n' + ' <supportedOn ref="SUPPORTED_TESTOS"/>\n' + ' <elements>\n' + ' <list id="SampleListPolicyDesc"' + ' key="Software\Policies\Test\SampleListPolicy" valuePrefix=""/>\n' + ' </elements>\n' + '</policy>') self.AssertXMLEquals(output, expected_output) 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 index f038f2e..ad415d4 100644 --- a/tools/grit/grit/format/policy_templates/writers/xml_formatted_writer.py +++ b/tools/grit/grit/format/policy_templates/writers/xml_formatted_writer.py @@ -43,3 +43,12 @@ class XMLFormattedWriter(template_writer.TemplateWriter): ''' doc = parent.ownerDocument parent.appendChild(doc.createTextNode(text)) + + def AddAttribute(self, parent, name, value): + '''Adds a new attribute to the parent Element. If an attribute with the + given name already exists then it will be replaced. + ''' + doc = parent.ownerDocument + attribute = doc.createAttribute(name) + attribute.value = value + parent.setAttributeNode(attribute) |