diff options
author | gfeher@chromium.org <gfeher@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-09-28 15:46:01 +0000 |
---|---|---|
committer | gfeher@chromium.org <gfeher@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-09-28 15:46:01 +0000 |
commit | 52d49a6e0731de4a5bc21221b4e200ef132e60b4 (patch) | |
tree | 72e6747bf3241c45ab526d062326e5fcb944c0af /tools/grit | |
parent | f7e9fd67522e66bc9cf3aab6e17a77d35e2b22e2 (diff) | |
download | chromium_src-52d49a6e0731de4a5bc21221b4e200ef132e60b4.zip chromium_src-52d49a6e0731de4a5bc21221b4e200ef132e60b4.tar.gz chromium_src-52d49a6e0731de4a5bc21221b4e200ef132e60b4.tar.bz2 |
Clean up generated policy documentation
Order policies alphabetically for all the generated templates.
Show group descriptions in the generated documentation.
BUG=56358,56654
TEST=DocWriterUnittest.testAddPolicySection,DocWriterUnittest.testAddPolicyRow, PolicyTemplateGenerator.testSorting
Review URL: http://codereview.chromium.org/3431031
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@60789 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools/grit')
4 files changed, 247 insertions, 93 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 8c02d96..10ffa9b 100644 --- a/tools/grit/grit/format/policy_templates/policy_template_generator.py +++ b/tools/grit/grit/format/policy_templates/policy_template_generator.py @@ -20,18 +20,32 @@ class PolicyTemplateGenerator: messages: An identifier to string dictionary of all the localized messages that might appear in the policy template. policy_definitions: The list of defined policies and groups, as - parsed from the polify metafile. + parsed from the policy metafile. Note that this list is passed by + reference and its contents are modified. See chrome/app/policy.policy_templates.json for description and content. ''' # List of all the policies: self._policy_definitions = policy_definitions - # Localized messages to be inserted to the policy_groups structure: + # Localized messages to be inserted to the policy_definitions structure: self._messages = messages + self._SortPolicies(self._policy_definitions) self._AddMessagesToPolicyList(self._policy_definitions) + def _SortPolicies(self, policy_list): + '''Sorts a list of policies in-place alphabetically. The order is the + following: first groups alphabetically (a-z), then other policies + alphabetically (a-z). The order of policies inside groups is unchanged. + + Args: + policy_list: The list of policies to sort. Sub-lists in groups will not + be sorted. + ''' + policy_list.sort( + key=lambda(policy): (policy['type'] != 'group', policy['name'])) + def _AddMessageToItem(self, item_name, item, message_name, default=None): - '''Adds a localized message strings to an item of the policy data structure + '''Adds a localized message string to an item of the policy data structure Args: item_name: The base of the grd name of the item. @@ -42,7 +56,7 @@ class PolicyTemplateGenerator: Raises: Exception() if the message string was not found and no default was - specified.. + specified. ''' # The keys for the item's messages in self._messages: long_message_name = ('IDS_POLICY_%s_%s' % 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 7a07077..fdc0a14 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 @@ -15,10 +15,20 @@ from grit.format.policy_templates.writers import mock_writer from grit.format.policy_templates.writers import template_writer +class MessagesMock: + '''A mock dictionary that contains "all the keys". Used for tests + where the handling of GUI messages is irrelevant. + ''' + def __getitem__(self, key): + return '' + def __contains__(self, key): + return True + + class PolicyTemplateGeneratorUnittest(unittest.TestCase): '''Unit tests for policy_template_generator.py.''' - def do_test(self, messages, policy_groups, writer): + def do_test(self, messages, policy_definitions, writer): '''Executes a test case. Creates and invokes an instance of PolicyTemplateGenerator with @@ -29,16 +39,16 @@ class PolicyTemplateGeneratorUnittest(unittest.TestCase): test output. Args: - strings: The dictionary of localized strings. - policy_groups: The list of policy groups as it would be loaded from - policy_templates.json. + messages: The dictionary of localized messages. + policy_definitions: The list of policies and groups as it would be + loaded from policy_templates.json. writer: A writer used for this test. It is usually derived from mock_writer.MockWriter. ''' writer.tester = self policy_generator = policy_template_generator.PolicyTemplateGenerator( messages, - policy_groups) + policy_definitions) res = policy_generator.GetTemplateText(writer) writer.Test() return res @@ -61,19 +71,11 @@ class PolicyTemplateGeneratorUnittest(unittest.TestCase): def Test(self): self.tester.assertEquals(self.log, 'init;prepare;begin;end;get_text;') - result = self.do_test({}, {}, LocalMockWriter()) + result = self.do_test({}, [], LocalMockWriter()) self.assertEquals(result, 'writer_result_string') def testEmptyGroups(self): # Test that empty policy groups are not passed to the writer. - messages_mock = { - 'IDS_POLICY_GROUP1_CAPTION': '', - 'IDS_POLICY_GROUP1_DESC': '', - 'IDS_POLICY_GROUP2_CAPTION': '', - 'IDS_POLICY_GROUP2_DESC': '', - 'IDS_POLICY_GROUP3_CAPTION': '', - 'IDS_POLICY_GROUP3_DESC': '', - } policies_mock = [ {'name': 'Group1', 'type': 'group', 'policies': []}, {'name': 'Group2', 'type': 'group', 'policies': []}, @@ -88,28 +90,23 @@ class PolicyTemplateGeneratorUnittest(unittest.TestCase): self.log += ']' def Test(self): self.tester.assertEquals(self.log, '') - self.do_test(messages_mock, policies_mock, LocalMockWriter()) + self.do_test(MessagesMock(), policies_mock, LocalMockWriter()) def testGroups(self): # Test that policy groups are passed to the writer in the correct order. - messages_mock = { - 'IDS_POLICY_GROUP1_CAPTION': '', - 'IDS_POLICY_GROUP1_DESC': '', - 'IDS_POLICY_GROUP2_CAPTION': '', - 'IDS_POLICY_GROUP2_DESC': '', - 'IDS_POLICY_GROUP3_CAPTION': '', - 'IDS_POLICY_GROUP3_DESC': '', - 'IDS_POLICY_TAG1_CAPTION': '', - 'IDS_POLICY_TAG1_DESC': '', - 'IDS_POLICY_TAG2_CAPTION': '', - 'IDS_POLICY_TAG2_DESC': '', - 'IDS_POLICY_TAG3_CAPTION': '', - 'IDS_POLICY_TAG3_DESC': '', - } policies_mock = [ - {'name': 'Group1', 'type': 'group', 'policies': [{'name': 'TAG1', 'type': 'mock'}]}, - {'name': 'Group2', 'type': 'group', 'policies': [{'name': 'TAG2', 'type': 'mock'}]}, - {'name': 'Group3', 'type': 'group', 'policies': [{'name': 'TAG3', 'type': 'mock'}]}, + { + 'name': 'Group1', 'type': 'group', + 'policies': [{'name': 'TAG1', 'type': 'mock'}] + }, + { + 'name': 'Group2', 'type': 'group', + 'policies': [{'name': 'TAG2', 'type': 'mock'}] + }, + { + 'name': 'Group3', 'type': 'group', + 'policies': [{'name': 'TAG3', 'type': 'mock'}] + }, ] class LocalMockWriter(mock_writer.MockWriter): def __init__(self): @@ -120,17 +117,17 @@ class PolicyTemplateGeneratorUnittest(unittest.TestCase): self.log += ']' def Test(self): self.tester.assertEquals(self.log, '[TAG1][TAG2][TAG3]') - self.do_test(messages_mock, policies_mock, LocalMockWriter()) + self.do_test(MessagesMock(), policies_mock, LocalMockWriter()) def testGroupTexts(self): - # Test that GUI strings are assigned correctly to policy groups. + # Test that GUI messages are assigned correctly to policy groups. messages_mock = { 'IDS_POLICY_GROUP1_CAPTION': 'string1', 'IDS_POLICY_GROUP1_DESC': 'string2', 'IDS_POLICY_GROUP2_CAPTION': 'string3', 'IDS_POLICY_GROUP2_DESC': 'string4', } - policy_groups_mock = [ + policy_defs_mock = [ {'name': 'Group1', 'type': 'group', 'policies': []}, {'name': 'Group2', 'type': 'group', 'policies': []}, ] @@ -144,23 +141,11 @@ class PolicyTemplateGeneratorUnittest(unittest.TestCase): self.tester.assertEquals(group['desc'], 'string4') else: self.tester.fail() - self.do_test(messages_mock, policy_groups_mock, LocalMockWriter()) + self.do_test(messages_mock, policy_defs_mock, LocalMockWriter()) def testPolicies(self): # Test that policies are passed to the writer in the correct order. - messages_mock = { - 'IDS_POLICY_GROUP1_CAPTION': '', - 'IDS_POLICY_GROUP1_DESC': '', - 'IDS_POLICY_GROUP2_CAPTION': '', - 'IDS_POLICY_GROUP2_DESC': '', - 'IDS_POLICY_GROUP1POLICY1_CAPTION': '', - 'IDS_POLICY_GROUP1POLICY1_DESC': '', - 'IDS_POLICY_GROUP1POLICY2_CAPTION': '', - 'IDS_POLICY_GROUP1POLICY2_DESC': '', - 'IDS_POLICY_GROUP2POLICY3_CAPTION': '', - 'IDS_POLICY_GROUP2POLICY3_DESC': '', - } - policy_groups_mock = [ + policy_defs_mock = [ { 'name': 'Group1', 'type': 'group', @@ -192,10 +177,10 @@ class PolicyTemplateGeneratorUnittest(unittest.TestCase): self.tester.assertEquals( self.policy_list, ['Group1Policy1', 'Group1Policy2', 'Group2Policy3']) - self.do_test(messages_mock, policy_groups_mock, LocalMockWriter()) + self.do_test(MessagesMock(), policy_defs_mock, LocalMockWriter()) def testPolicyTexts(self): - # Test that GUI strings are assigned correctly to policies. + # Test that GUI messages are assigned correctly to policies. messages_mock = { 'IDS_POLICY_POLICY1_CAPTION': 'string1', 'IDS_POLICY_POLICY1_DESC': 'string2', @@ -204,7 +189,7 @@ class PolicyTemplateGeneratorUnittest(unittest.TestCase): 'IDS_POLICY_GROUP1_CAPTION': '', 'IDS_POLICY_GROUP1_DESC': '', } - policy_groups_mock = [ + policy_defs_mock = [ { 'name': 'Group1', 'type': 'group', @@ -224,10 +209,10 @@ class PolicyTemplateGeneratorUnittest(unittest.TestCase): self.tester.assertEquals(policy['desc'], 'string4') else: self.tester.fail() - self.do_test(messages_mock, policy_groups_mock, LocalMockWriter()) + self.do_test(messages_mock, policy_defs_mock, LocalMockWriter()) def testEnumTexts(self): - # Test that GUI strings are assigned correctly to enums + # Test that GUI messages are assigned correctly to enums # (aka dropdown menus). messages_mock = { 'IDS_POLICY_ENUM_ITEM1_CAPTION': 'string1', @@ -237,7 +222,7 @@ class PolicyTemplateGeneratorUnittest(unittest.TestCase): 'IDS_POLICY_POLICY1_DESC': '', } - policy_groups_mock = [{ + policy_defs_mock = [{ 'name': 'Policy1', 'type': 'enum', 'items': [ @@ -252,23 +237,11 @@ class PolicyTemplateGeneratorUnittest(unittest.TestCase): 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()) + self.do_test(messages_mock, policy_defs_mock, LocalMockWriter()) def testPolicyFiltering(self): # Test that policies are filtered correctly based on their annotations. - messages_mock = { - 'IDS_POLICY_GROUP1_CAPTION': '', - 'IDS_POLICY_GROUP1_DESC': '', - 'IDS_POLICY_GROUP2_CAPTION': '', - 'IDS_POLICY_GROUP2_DESC': '', - 'IDS_POLICY_GROUP1POLICY1_CAPTION': '', - 'IDS_POLICY_GROUP1POLICY1_DESC': '', - 'IDS_POLICY_GROUP1POLICY2_CAPTION': '', - 'IDS_POLICY_GROUP1POLICY2_DESC': '', - 'IDS_POLICY_GROUP2POLICY3_CAPTION': '', - 'IDS_POLICY_GROUP2POLICY3_DESC': '', - } - policy_groups_mock = [{ + policy_defs_mock = [{ 'name': 'Group1', 'type': 'group', 'policies': [ @@ -316,15 +289,75 @@ class PolicyTemplateGeneratorUnittest(unittest.TestCase): self.result_list, self.expected_list) self.do_test( - messages_mock, - policy_groups_mock, + MessagesMock(), + policy_defs_mock, LocalMockWriter(['eee'], ['begin_Group2', 'Group2Policy3'])) self.do_test( - messages_mock, - policy_groups_mock, + MessagesMock(), + policy_defs_mock, LocalMockWriter( ['ddd', 'bbb'], ['begin_Group1', 'Group1Policy1', 'Group1Policy2'])) + def testSorting(self): + # Tests that policies are sorted correctly. + policy_defs = [ + {'name': 'zp', 'type': 'string'}, + { + 'type': 'group', + 'name': 'zg', + 'policies': [ + {'name': 'z0', 'type': 'string'}, + {'name': 'a0', 'type': 'string'} + ] + }, + { + 'type': 'group', + 'name': 'ag', + 'policies': [{'name': 'q', 'type': 'string'}], + }, + {'name': 'ap', 'type': 'string'} + ] + sorted_policy_defs = [ + { + 'type': 'group', + 'name': 'ag', + 'policies': [{'name': 'q', 'type': 'string'}], + }, + { + 'type': 'group', + 'name': 'zg', + 'policies': [ + {'name': 'z0', 'type': 'string'}, + {'name': 'a0', 'type': 'string'} + ] + }, + {'name': 'ap', 'type': 'string'}, + {'name': 'zp', 'type': 'string'}, + ] + ptg = policy_template_generator.PolicyTemplateGenerator([], []) + ptg._SortPolicies(policy_defs) + self.assertEquals( + policy_defs, + sorted_policy_defs) + + def testSortingInvoked(self): + # Tests that policy-sorting happens before passing policies to the writer. + policy_defs = [ + {'name': 'zp', 'type': 'string'}, + {'name': 'ap', 'type': 'string'} + ] + class LocalMockWriter(mock_writer.MockWriter): + def __init__(self): + self.result_list = [] + def WritePolicy(self, policy): + self.result_list.append(policy['name']) + def Test(self): + self.tester.assertEquals( + self.result_list, + ['ap', 'zp']) + self.do_test(MessagesMock(), policy_defs, LocalMockWriter()) + + if __name__ == '__main__': unittest.main() 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 cde4227..19feb1a 100644 --- a/tools/grit/grit/format/policy_templates/writers/doc_writer.py +++ b/tools/grit/grit/format/policy_templates/writers/doc_writer.py @@ -100,7 +100,8 @@ class DocWriter(xml_formatted_writer.XMLFormattedWriter): style = ''.join([self._STYLE[x] for x in style_ids]) if style != '': - attrs['style'] = style + # Apply the style specified by style_ids. + attrs['style'] = style + attrs.get('style', '') return self.AddElement(parent, name, attrs, text) def _AddDescription(self, parent, policy): @@ -351,11 +352,21 @@ class DocWriter(xml_formatted_writer.XMLFormattedWriter): policy: The data structure of the policy. ''' tr = self._AddStyledElement(parent, 'tr', ['tr']) - name_td = self._AddStyledElement(tr, 'td', ['td', 'td.left']) - self.AddElement( - name_td, 'a', - {'href': '#' + policy['name']}, policy['name']) - self._AddStyledElement(tr, 'td', ['td', 'td.right'], {}, policy['caption']) + indent = 'padding-left: %dpx;' % (7 + self._indent_level * 14) + if policy['type'] != 'group': + # Normal policies get two columns with name and caption. + name_td = self._AddStyledElement(tr, 'td', ['td', 'td.left'], + {'style': indent}) + self.AddElement(name_td, 'a', + {'href': '#' + policy['name']}, policy['name']) + self._AddStyledElement(tr, 'td', ['td', 'td.right'], {}, + policy['caption']) + else: + # Groups get one column with caption. + name_td = self._AddStyledElement(tr, 'td', ['td', 'td.left'], + {'style': indent, 'colspan': '2'}) + self.AddElement(name_td, 'a', {'href': '#' + policy['name']}, + policy['caption']) def _AddPolicySection(self, parent, policy): '''Adds a section about the policy in the detailed policy listing. @@ -364,15 +375,29 @@ class DocWriter(xml_formatted_writer.XMLFormattedWriter): parent: The DOM node of the <div> of the detailed policy list. policy: The data structure of the policy. ''' - h2 = self.AddElement(parent, 'h2') - self.AddElement(h2, 'a', {'name': policy['name']}) - self.AddText(h2, policy['name']) - self.AddElement(parent, 'span', {}, policy['caption']) - self._AddPolicyNote(parent, policy) + # Set style according to group nesting level. + indent = 'margin-left: %dpx' % (self._indent_level * 28) + if policy['type'] == 'group': + heading = 'h2' + else: + heading = 'h3' + parent2 = self.AddElement(parent, 'div', {'style': indent}) - self._AddPolicyDetails(parent, policy) + h2 = self.AddElement(parent2, heading) + self.AddElement(h2, 'a', {'name': policy['name']}) + if policy['type'] != 'group': + # Normal policies get a full description. + self.AddText(h2, policy['name']) + self.AddElement(parent2, 'span', {}, policy['caption']) + self._AddPolicyNote(parent2, policy) + self._AddPolicyDetails(parent2, policy) + else: + # Groups get a more compact description. + self.AddText(h2, policy['caption']) + self._AddStyledElement(parent2, 'div', ['div.group_desc'], + {}, policy['desc']) self.AddElement( - parent, 'a', {'href': '#top'}, + parent2, 'a', {'href': '#top'}, self._GetLocalizedMessage('back_to_top')) # @@ -383,6 +408,13 @@ class DocWriter(xml_formatted_writer.XMLFormattedWriter): self._AddPolicyRow(self._summary_tbody, policy) self._AddPolicySection(self._details_div, policy) + def BeginPolicyGroup(self, group): + self.WritePolicy(group) + self._indent_level += 1 + + def EndPolicyGroup(self): + self._indent_level -= 1 + def BeginTemplate(self): # Add a <div> for the summary section. summary_div = self.AddElement(self._main_div, 'div') @@ -415,6 +447,7 @@ class DocWriter(xml_formatted_writer.XMLFormattedWriter): self._doc = dom_impl.createDocument(None, 'html', None) body = self.AddElement(self._doc.documentElement, 'body') self._main_div = self.AddElement(body, 'div') + self._indent_level = 0 # Human-readable names of supported platforms. self._PLATFORM_MAP = { @@ -454,7 +487,8 @@ class DocWriter(xml_formatted_writer.XMLFormattedWriter): 'dd dl': 'margin-top: 0px; margin-bottom: 0px;', '.monospace': 'font-family: monospace;', '.pre': 'white-space: pre;', - 'div.note': 'border: 2px solid black; padding: 5px; margin: 5px;' + 'div.note': 'border: 2px solid black; padding: 5px; margin: 5px;', + 'div.group_desc': 'margin-top: 20px; margin-bottom: 20px;', } # A simple regexp to search for URLs. It is enough for now. 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 a394db7..85f1bb8 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 @@ -326,17 +326,90 @@ MockKey\\PolicyName\\1 = "Bar"</dd><dt>Linux:</dt><dd style="style_.mo # Test if policies are correctly added to the summary table. policy = { 'name': 'PolicyName', - 'caption': 'PolicyCaption' + 'caption': 'PolicyCaption', + 'type': 'string', } + self.writer._indent_level = 3 self.writer._AddPolicyRow(self.doc_root, policy) self.assertEquals( self.doc_root.toxml(), '<root><tr style="style_tr;">' - '<td style="style_td;style_td.left;">' + '<td style="style_td;style_td.left;padding-left: 49px;">' '<a href="#PolicyName">PolicyName</a>' '</td>' '<td style="style_td;style_td.right;">PolicyCaption</td>' '</tr></root>') + self.setUp() + policy = { + 'name': 'PolicyName', + 'caption': 'PolicyCaption', + 'type': 'group', + } + self.writer._indent_level = 2 + self.writer._AddPolicyRow(self.doc_root, policy) + self.assertEquals( + self.doc_root.toxml(), + '<root><tr style="style_tr;">' + '<td colspan="2" style="style_td;style_td.left;padding-left: 35px;">' + '<a href="#PolicyName">PolicyCaption</a>' + '</td>' + '</tr></root>') + + def testAddPolicySection(self): + # Test if policy details are correctly added to the document. + policy = { + 'name': 'PolicyName', + 'caption': 'PolicyCaption', + 'desc': 'PolicyDesc', + 'type': 'string', + 'annotations': { + 'platforms': ['win'], + 'products': ['chrome'], + 'features': {'dynamic_refresh': 0}, + 'example_value': False + } + } + self.writer._AddPolicySection(self.doc_root, policy) + self.assertEquals( + self.doc_root.toxml(), + '<root>' + '<div style="margin-left: 0px">' + '<h3><a name="PolicyName"/>PolicyName</h3>' + '<span>PolicyCaption</span>' + '<dl>' + '<dt style="style_dt;">_test_data_type</dt>' + '<dd>String (REG_SZ)</dd>' + '<dt style="style_dt;">_test_win_reg_loc</dt>' + '<dd style="style_.monospace;">MockKey\\PolicyName</dd>' + '<dt style="style_dt;">_test_mac_linux_pref_name</dt>' + '<dd style="style_.monospace;">PolicyName</dd>' + '<dt style="style_dt;">_test_supported_on_platforms</dt>' + '<dd>Windows</dd>' + '<dt style="style_dt;">_test_supported_in_products</dt>' + '<dd>Chrome</dd>' + '<dt style="style_dt;">_test_supported_features</dt>' + '<dd>Dynamic Policy Refresh: _test_not_supported</dd>' + '<dt style="style_dt;">_test_description</dt>' + '<dd>PolicyDesc</dd>' + '<dt style="style_dt;">_test_example_value</dt>' + '<dd>"False"</dd>' + '</dl>' + '<a href="#top">_test_back_to_top</a>' + '</div>' + '</root>') + # Test for groups. + self.setUp() + policy['type'] = 'group' + self.writer._AddPolicySection(self.doc_root, policy) + self.assertEquals( + self.doc_root.toxml(), + '<root>' + '<div style="margin-left: 0px">' + '<h2><a name="PolicyName"/>PolicyCaption</h2>' + '<div style="style_div.group_desc;">PolicyDesc</div>' + '<a href="#top">_test_back_to_top</a>' + '</div>' + '</root>') if __name__ == '__main__': |