diff options
author | markusheintz@chromium.org <markusheintz@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-09-23 11:09:33 +0000 |
---|---|---|
committer | markusheintz@chromium.org <markusheintz@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-09-23 11:09:33 +0000 |
commit | 2c35a4a3ad123f965c76391eb4e664fd9fd74452 (patch) | |
tree | 814737e7bb3aa8507a0f4ad2074fa0f259372eb7 /tools/grit | |
parent | 2e21b5e7458998a0d01bc6f3797c0e3e64e8294e (diff) | |
download | chromium_src-2c35a4a3ad123f965c76391eb4e664fd9fd74452.zip chromium_src-2c35a4a3ad123f965c76391eb4e664fd9fd74452.tar.gz chromium_src-2c35a4a3ad123f965c76391eb4e664fd9fd74452.tar.bz2 |
Moving "Writer" related code for processing the policy tree from policy_generator.py to the template_writer.py.
This is the first CL in a couple of refactoring steps.
BUG=56512
TEST=Current tests need to pass
Review URL: http://codereview.chromium.org/3496001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@60276 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools/grit')
13 files changed, 72 insertions, 117 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 fd23a52..bcf5b8f 100644 --- a/tools/grit/grit/format/policy_templates/policy_template_generator.py +++ b/tools/grit/grit/format/policy_templates/policy_template_generator.py @@ -114,24 +114,6 @@ class PolicyTemplateGenerator: # a policy, like in PListStringsWriter and DocWriter. policy['parent'] = group - 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. @@ -143,15 +125,4 @@ class PolicyTemplateGenerator: Returns: The text of the generated template. ''' - template_writer.Prepare() - template_writer.BeginTemplate() - for group in self._policy_groups: - 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.EndTemplate() - return template_writer.GetTemplateText() + return template_writer.WriteTemplate(self._policy_groups) 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 aab761b..f2f6c66 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 @@ -49,7 +49,7 @@ class PolicyTemplateGeneratorUnittest(unittest.TestCase): class LocalMockWriter(mock_writer.MockWriter): def __init__(self): self.log = 'init;' - def Prepare(self): + def Init(self): self.log += 'prepare;' def BeginTemplate(self): self.log += 'begin;' 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 df3620c..bea1e38 100644 --- a/tools/grit/grit/format/policy_templates/writers/adm_writer.py +++ b/tools/grit/grit/format/policy_templates/writers/adm_writer.py @@ -168,7 +168,7 @@ class AdmWriter(template_writer.TemplateWriter): self._PrintLine('END CATEGORY', -1) self._PrintLine('', -1) - def Prepare(self): + def Init(self): self.policy_list = [] self.str_list = ['[Strings]'] self.indent = '' 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 cc29fdb..b18ac39 100644 --- a/tools/grit/grit/format/policy_templates/writers/adml_writer.py +++ b/tools/grit/grit/format/policy_templates/writers/adml_writer.py @@ -207,9 +207,6 @@ class ADMLWriter(xml_formatted_writer.XMLFormattedWriter): self._active_presentation_elem = self.AddElement( self._presentation_table_elem, 'presentation', {'id': id}) - def EndPolicyGroup(self): - pass - def _AddBaseStrings(self, string_table_elem, build): ''' Adds ADML "string" elements to the string-table that are referenced by the ADMX file but not related to any specific Policy-Group or Policy. @@ -242,12 +239,6 @@ class ADMLWriter(xml_formatted_writer.XMLFormattedWriter): self._presentation_table_elem = self.AddElement(resources_elem, 'presentationTable') - def EndTemplate(self): - pass - - def Prepare(self): - pass - def GetTemplateText(self): # Using "toprettyxml()" confuses the Windows Group Policy Editor # (gpedit.msc) because it interprets whitespace characters in text between 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 c21ab65..6c2a9c2 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 @@ -32,7 +32,7 @@ class AdmlWriterTest(xml_writer_base_unittest.XmlWriterBaseTest): 'IDS_POLICY_WIN_SUPPORTED_WINXPSP2': 'Supported on Test OS or higher' } self.writer = adml_writer.GetWriter(config, messages) - self.writer.Prepare() + self.writer.Init() def _InitWriterForAddingPolicyGroups(self, writer): '''Initialize the writer for adding policy groups. This method must be 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 e266944..11a2007 100644 --- a/tools/grit/grit/format/policy_templates/writers/admx_writer.py +++ b/tools/grit/grit/format/policy_templates/writers/admx_writer.py @@ -348,9 +348,6 @@ class ADMXWriter(xml_formatted_writer.XMLFormattedWriter): self.AddElement(self._active_policy_elem, 'supportedOn', {'ref': self.config['win_supported_os']}) - def EndPolicyGroup(self): - return - def BeginTemplate(self): '''Generates the skeleton of the ADMX template. An ADMX template contains an ADMX "PolicyDefinitions" element with four child nodes: "policies" @@ -375,11 +372,5 @@ class ADMXWriter(xml_formatted_writer.XMLFormattedWriter): self._active_policies_elem = self.AddElement(policy_definitions_elem, 'policies') - def EndTemplate(self): - pass - - def Prepare(self): - pass - def GetTemplateText(self): return self._doc.toprettyxml(indent=' ') 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 634d0b5..dcf58b3 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 @@ -41,7 +41,7 @@ class AdmxWriterTest(xml_writer_base_unittest.XmlWriterBaseTest): # Grit messages. messages = {} self.writer = admx_writer.GetWriter(config, messages) - self.writer.Prepare() + self.writer.Init() def testEmpty(self): self.writer.BeginTemplate() diff --git a/tools/grit/grit/format/policy_templates/writers/doc_writer.py b/tools/grit/grit/format/policy_templates/writers/doc_writer.py index b632937..ad28ace 100644 --- a/tools/grit/grit/format/policy_templates/writers/doc_writer.py +++ b/tools/grit/grit/format/policy_templates/writers/doc_writer.py @@ -405,12 +405,6 @@ class DocWriter(xml_formatted_writer.XMLFormattedWriter): self._AddPolicyRow(self._summary_tbody, policy) self._AddPolicySection(self._details_div, policy) - def BeginPolicyGroup(self, group): - pass - - def EndPolicyGroup(self): - pass - def BeginTemplate(self): # Add a <div> for the summary section. summary_div = self.AddElement(self._main_div, 'div') @@ -438,10 +432,7 @@ class DocWriter(xml_formatted_writer.XMLFormattedWriter): # Add a <div> for the detailed policy listing. self._details_div = self.AddElement(self._main_div, 'div') - def EndTemplate(self): - pass - - def Prepare(self): + def Init(self): dom_impl = minidom.getDOMImplementation('') self._doc = dom_impl.createDocument(None, 'html', None) body = self.AddElement(self._doc.documentElement, 'body') diff --git a/tools/grit/grit/format/policy_templates/writers/doc_writer_unittest.py b/tools/grit/grit/format/policy_templates/writers/doc_writer_unittest.py index bbb2292..2166916 100644 --- a/tools/grit/grit/format/policy_templates/writers/doc_writer_unittest.py +++ b/tools/grit/grit/format/policy_templates/writers/doc_writer_unittest.py @@ -66,7 +66,7 @@ class DocWriterUnittest(writer_unittest_common.WriterUnittestCommon): 'win_reg_key_name': 'MockKey', }, messages=MockMessageDictionary()) - self.writer.Prepare() + self.writer.Init() # It is not worth testing the exact content of style attributes. # Therefore we override them here with shorter texts. 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 97b9e47..f402d68 100644 --- a/tools/grit/grit/format/policy_templates/writers/mock_writer.py +++ b/tools/grit/grit/format/policy_templates/writers/mock_writer.py @@ -16,21 +16,9 @@ class MockWriter(TemplateWriter): def WritePolicy(self, policy): pass - def BeginPolicyGroup(self, group): - pass - - def EndPolicyGroup(self): - pass - def BeginTemplate(self): pass - def EndTemplate(self): - pass - - def Prepare(self): - pass - def GetTemplateText(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 a42bb13..47b1816 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 @@ -73,22 +73,13 @@ class PListStringsWriter(template_writer.TemplateWriter): self._AddToStringTable(policy['name'], caption, desc) - def BeginPolicyGroup(self, group): - pass - - def EndPolicyGroup(self): - pass - def BeginTemplate(self): self._AddToStringTable( self.config['app_name'], self.config['app_name'], self.messages['IDS_POLICY_MAC_CHROME_PREFERENCES']) - def EndTemplate(self): - pass - - def Prepare(self): + def Init(self): # A buffer for the lines of the string table being generated. self._out = [] 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 37deb94..790e21a 100644 --- a/tools/grit/grit/format/policy_templates/writers/plist_writer.py +++ b/tools/grit/grit/format/policy_templates/writers/plist_writer.py @@ -89,12 +89,6 @@ class PListWriter(xml_formatted_writer.XMLFormattedWriter): for item in policy['items']: self.AddElement(range_list, 'integer', {}, item['value']) - def BeginPolicyGroup(self, group): - pass - - def EndPolicyGroup(self): - pass - def BeginTemplate(self): self._plist.attributes['version'] = '1' dict = self.AddElement(self._plist, 'dict') @@ -108,10 +102,7 @@ class PListWriter(xml_formatted_writer.XMLFormattedWriter): self._array = self._AddKeyValuePair(dict, 'pfm_subkeys', 'array') - def EndTemplate(self): - pass - - def Prepare(self): + def Init(self): dom_impl = minidom.getDOMImplementation('') doctype = dom_impl.createDocumentType( 'plist', 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 ccc730a..fd31c90 100644 --- a/tools/grit/grit/format/policy_templates/writers/template_writer.py +++ b/tools/grit/grit/format/policy_templates/writers/template_writer.py @@ -30,11 +30,67 @@ class TemplateWriter(object): self.config = config self.messages = messages - def Prepare(self): - '''Initializes the internal buffer where the template will be - stored. + def IsPolicySupported(self, policy): + '''Checks if the given policy is supported by the writer. + + Args: + policy: The dictionary of the policy. + + Returns: + True if the writer chooses to include 'policy' in its output. ''' - raise NotImplementedError() + for platform in self.platforms: + if platform in policy['annotations']['platforms']: + return True + return False + + def _GetPoliciesForWriter(self, group): + '''Filters the list of policies in the passed group that are supported by + the writer. + + Args: + 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 self.IsPolicySupported(policy): + result.append(policy) + return result + + def Init(self): + '''Initializes the writer. If the WriteTemplate method is overridden, then + this method must be called as first step of each template generation + process. + ''' + pass + + + def WriteTemplate(self, template): + '''Writes the given template definition. + + Args: + template: Template definition to write. + + Returns: + Generated output for the passed template definition. + ''' + self.Init() + self.BeginTemplate() + for group in template: + policies = self._GetPoliciesForWriter(group) + if policies: + # Only write nonempty groups. + self.BeginPolicyGroup(group) + for policy in policies: + self.WritePolicy(policy) + self.EndPolicyGroup() + self.EndTemplate() + return self.GetTemplateText() def WritePolicy(self, policy): '''Appends the template text corresponding to a policy into the @@ -52,13 +108,13 @@ class TemplateWriter(object): Args: group: The policy group as it is found in the JSON file. ''' - raise NotImplementedError() + pass def EndPolicyGroup(self): '''Appends the template text corresponding to the end of a policy group into the internal buffer. ''' - raise NotImplementedError() + pass def BeginTemplate(self): '''Appends the text corresponding to the beginning of the whole @@ -70,8 +126,7 @@ class TemplateWriter(object): '''Appends the text corresponding to the end of the whole template into the internal buffer. ''' - raise NotImplementedError() - + pass def GetTemplateText(self): '''Gets the content of the internal template buffer. @@ -80,17 +135,3 @@ 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 |