Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(109)

Side by Side Diff: grit/format/policy_templates/writers/adml_writer.py

Issue 695613003: Fix duplicated strings in ADMX templates. (Closed) Base URL: https://chromium.googlesource.com/external/grit-i18n.git@master
Patch Set: added tests Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 from xml.dom import minidom 6 from xml.dom import minidom
7 from grit.format.policy_templates.writers import xml_formatted_writer 7 from grit.format.policy_templates.writers import xml_formatted_writer
8 8
9 9
10 def GetWriter(config): 10 def GetWriter(config):
(...skipping 12 matching lines...) Expand all
23 # DOM root node of the generated ADML document. 23 # DOM root node of the generated ADML document.
24 _doc = None 24 _doc = None
25 25
26 # The string-table contains all ADML "string" elements. 26 # The string-table contains all ADML "string" elements.
27 _string_table_elem = None 27 _string_table_elem = None
28 28
29 # The presentation-table is the container for presentation elements, that 29 # The presentation-table is the container for presentation elements, that
30 # describe the presentation of Policy-Groups and Policies. 30 # describe the presentation of Policy-Groups and Policies.
31 _presentation_table_elem = None 31 _presentation_table_elem = None
32 32
33 def _AddString(self, parent, id, text): 33 def _AddString(self, id, text):
34 ''' Adds an ADML "string" element to the passed parent. The following 34 ''' Adds an ADML "string" element to _string_table_elem. The following
35 ADML snippet contains an example: 35 ADML snippet contains an example:
36 36
37 <string id="$(id)">$(text)</string> 37 <string id="$(id)">$(text)</string>
38 38
39 Args: 39 Args:
40 parent: Parent element to which the new "string" element is added.
41 id: ID of the newly created "string" element. 40 id: ID of the newly created "string" element.
42 text: Value of the newly created "string" element. 41 text: Value of the newly created "string" element.
43 ''' 42 '''
44 string_elem = self.AddElement(parent, 'string', {'id': id}) 43 id = id.replace('.', '_')
45 string_elem.appendChild(self._doc.createTextNode(text)) 44 if id in self.strings_seen:
45 assert text == self.strings_seen[id]
46 else:
47 self.strings_seen[id] = text
48 string_elem = self.AddElement(
49 self._string_table_elem, 'string', {'id': id})
50 string_elem.appendChild(self._doc.createTextNode(text))
46 51
47 def WritePolicy(self, policy): 52 def WritePolicy(self, policy):
48 '''Generates the ADML elements for a Policy. 53 '''Generates the ADML elements for a Policy.
49 <stringTable> 54 <stringTable>
50 ... 55 ...
51 <string id="$(policy_group_name)">$(caption)</string> 56 <string id="$(policy_group_name)">$(caption)</string>
52 <string id="$(policy_group_name)_Explain">$(description)</string> 57 <string id="$(policy_group_name)_Explain">$(description)</string>
53 </stringTable> 58 </stringTable>
54 59
55 <presentationTables> 60 <presentationTables>
(...skipping 12 matching lines...) Expand all
68 policy_caption = policy_name 73 policy_caption = policy_name
69 if 'desc' in policy: 74 if 'desc' in policy:
70 policy_description = policy['desc'] 75 policy_description = policy['desc']
71 else: 76 else:
72 policy_description = policy_name 77 policy_description = policy_name
73 if 'label' in policy: 78 if 'label' in policy:
74 policy_label = policy['label'] 79 policy_label = policy['label']
75 else: 80 else:
76 policy_label = policy_name 81 policy_label = policy_name
77 82
78 self._AddString(self._string_table_elem, policy_name, policy_caption) 83 self._AddString(policy_name, policy_caption)
79 self._AddString(self._string_table_elem, policy_name + '_Explain', 84 self._AddString(policy_name + '_Explain', policy_description)
80 policy_description)
81 presentation_elem = self.AddElement( 85 presentation_elem = self.AddElement(
82 self._presentation_table_elem, 'presentation', {'id': policy_name}) 86 self._presentation_table_elem, 'presentation', {'id': policy_name})
83 87
84 if policy_type == 'main': 88 if policy_type == 'main':
85 pass 89 pass
86 elif policy_type in ('string', 'dict'): 90 elif policy_type in ('string', 'dict'):
87 # 'dict' policies are configured as JSON-encoded strings on Windows. 91 # 'dict' policies are configured as JSON-encoded strings on Windows.
88 textbox_elem = self.AddElement(presentation_elem, 'textBox', 92 textbox_elem = self.AddElement(presentation_elem, 'textBox',
89 {'refId': policy_name}) 93 {'refId': policy_name})
90 label_elem = self.AddElement(textbox_elem, 'label') 94 label_elem = self.AddElement(textbox_elem, 'label')
91 label_elem.appendChild(self._doc.createTextNode(policy_label)) 95 label_elem.appendChild(self._doc.createTextNode(policy_label))
92 elif policy_type == 'int': 96 elif policy_type == 'int':
93 textbox_elem = self.AddElement(presentation_elem, 'decimalTextBox', 97 textbox_elem = self.AddElement(presentation_elem, 'decimalTextBox',
94 {'refId': policy_name}) 98 {'refId': policy_name})
95 textbox_elem.appendChild(self._doc.createTextNode(policy_label + ':')) 99 textbox_elem.appendChild(self._doc.createTextNode(policy_label + ':'))
96 elif policy_type in ('int-enum', 'string-enum'): 100 elif policy_type in ('int-enum', 'string-enum'):
97 for item in policy['items']: 101 for item in policy['items']:
98 self._AddString(self._string_table_elem, item['name'], item['caption']) 102 self._AddString(item['name'], item['caption'])
99 dropdownlist_elem = self.AddElement(presentation_elem, 'dropdownList', 103 dropdownlist_elem = self.AddElement(presentation_elem, 'dropdownList',
100 {'refId': policy_name}) 104 {'refId': policy_name})
101 dropdownlist_elem.appendChild(self._doc.createTextNode(policy_label)) 105 dropdownlist_elem.appendChild(self._doc.createTextNode(policy_label))
102 elif policy_type in ('list', 'string-enum-list'): 106 elif policy_type in ('list', 'string-enum-list'):
103 self._AddString(self._string_table_elem, 107 self._AddString(policy_name + 'Desc', policy_caption)
104 policy_name + 'Desc',
105 policy_caption)
106 listbox_elem = self.AddElement(presentation_elem, 'listBox', 108 listbox_elem = self.AddElement(presentation_elem, 'listBox',
107 {'refId': policy_name + 'Desc'}) 109 {'refId': policy_name + 'Desc'})
108 listbox_elem.appendChild(self._doc.createTextNode(policy_label)) 110 listbox_elem.appendChild(self._doc.createTextNode(policy_label))
109 elif policy_type == 'group': 111 elif policy_type == 'group':
110 pass 112 pass
111 elif policy_type == 'external': 113 elif policy_type == 'external':
112 # This type can only be set through cloud policy. 114 # This type can only be set through cloud policy.
113 pass 115 pass
114 else: 116 else:
115 raise Exception('Unknown policy type %s.' % policy_type) 117 raise Exception('Unknown policy type %s.' % policy_type)
116 118
117 def BeginPolicyGroup(self, group): 119 def BeginPolicyGroup(self, group):
118 '''Generates ADML elements for a Policy-Group. For each Policy-Group two 120 '''Generates ADML elements for a Policy-Group. For each Policy-Group two
119 ADML "string" elements are added to the string-table. One contains the 121 ADML "string" elements are added to the string-table. One contains the
120 caption of the Policy-Group and the other a description. A Policy-Group also 122 caption of the Policy-Group and the other a description. A Policy-Group also
121 requires an ADML "presentation" element that must be added to the 123 requires an ADML "presentation" element that must be added to the
122 presentation-table. The "presentation" element is the container for the 124 presentation-table. The "presentation" element is the container for the
123 elements that define the visual presentation of the Policy-Goup's Policies. 125 elements that define the visual presentation of the Policy-Goup's Policies.
124 The following ADML snippet shows an example: 126 The following ADML snippet shows an example:
125 127
126 Args: 128 Args:
127 group: The Policy-Group to generate ADML elements for. 129 group: The Policy-Group to generate ADML elements for.
128 ''' 130 '''
129 # Add ADML "string" elements to the string-table that are required by a 131 # Add ADML "string" elements to the string-table that are required by a
130 # Policy-Group. 132 # Policy-Group.
131 self._AddString(self._string_table_elem, group['name'] + '_group', 133 self._AddString(group['name'] + '_group', group['caption'])
132 group['caption'])
133 134
134 def _AddBaseStrings(self, string_table_elem, build): 135 def _AddBaseStrings(self, build):
135 ''' Adds ADML "string" elements to the string-table that are referenced by 136 ''' Adds ADML "string" elements to the string-table that are referenced by
136 the ADMX file but not related to any specific Policy-Group or Policy. 137 the ADMX file but not related to any specific Policy-Group or Policy.
137 ''' 138 '''
138 self._AddString(string_table_elem, self.config['win_supported_os'], 139 self._AddString(self.config['win_supported_os'],
139 self.messages['win_supported_winxpsp2']['text']) 140 self.messages['win_supported_winxpsp2']['text'])
140 recommended_name = '%s - %s' % \ 141 recommended_name = '%s - %s' % \
141 (self.config['app_name'], self.messages['doc_recommended']['text']) 142 (self.config['app_name'], self.messages['doc_recommended']['text'])
142 if build == 'chrome': 143 if build == 'chrome':
143 self._AddString(string_table_elem, 144 self._AddString(self.config['win_mandatory_category_path'][0],
144 self.config['win_mandatory_category_path'][0],
145 'Google') 145 'Google')
146 self._AddString(string_table_elem, 146 self._AddString(self.config['win_mandatory_category_path'][1],
147 self.config['win_mandatory_category_path'][1],
148 self.config['app_name']) 147 self.config['app_name'])
149 self._AddString(string_table_elem, 148 self._AddString(self.config['win_recommended_category_path'][1],
150 self.config['win_recommended_category_path'][1],
151 recommended_name) 149 recommended_name)
152 elif build == 'chromium': 150 elif build == 'chromium':
153 self._AddString(string_table_elem, 151 self._AddString(self.config['win_mandatory_category_path'][0],
154 self.config['win_mandatory_category_path'][0],
155 self.config['app_name']) 152 self.config['app_name'])
156 self._AddString(string_table_elem, 153 self._AddString(self.config['win_recommended_category_path'][0],
157 self.config['win_recommended_category_path'][0],
158 recommended_name) 154 recommended_name)
159 155
160 def BeginTemplate(self): 156 def BeginTemplate(self):
161 dom_impl = minidom.getDOMImplementation('') 157 dom_impl = minidom.getDOMImplementation('')
162 self._doc = dom_impl.createDocument(None, 'policyDefinitionResources', 158 self._doc = dom_impl.createDocument(None, 'policyDefinitionResources',
163 None) 159 None)
164 policy_definitions_resources_elem = self._doc.documentElement 160 policy_definitions_resources_elem = self._doc.documentElement
165 policy_definitions_resources_elem.attributes['revision'] = '1.0' 161 policy_definitions_resources_elem.attributes['revision'] = '1.0'
166 policy_definitions_resources_elem.attributes['schemaVersion'] = '1.0' 162 policy_definitions_resources_elem.attributes['schemaVersion'] = '1.0'
167 163
168 self.AddElement(policy_definitions_resources_elem, 'displayName') 164 self.AddElement(policy_definitions_resources_elem, 'displayName')
169 self.AddElement(policy_definitions_resources_elem, 'description') 165 self.AddElement(policy_definitions_resources_elem, 'description')
170 resources_elem = self.AddElement(policy_definitions_resources_elem, 166 resources_elem = self.AddElement(policy_definitions_resources_elem,
171 'resources') 167 'resources')
172 self._string_table_elem = self.AddElement(resources_elem, 'stringTable') 168 self._string_table_elem = self.AddElement(resources_elem, 'stringTable')
173 self._AddBaseStrings(self._string_table_elem, self.config['build']) 169 self._AddBaseStrings(self.config['build'])
174 self._presentation_table_elem = self.AddElement(resources_elem, 170 self._presentation_table_elem = self.AddElement(resources_elem,
175 'presentationTable') 171 'presentationTable')
176 172
173 def Init(self):
174 # Map of all strings seen.
175 self.strings_seen = {}
176
177 def GetTemplateText(self): 177 def GetTemplateText(self):
178 # Using "toprettyxml()" confuses the Windows Group Policy Editor 178 # Using "toprettyxml()" confuses the Windows Group Policy Editor
179 # (gpedit.msc) because it interprets whitespace characters in text between 179 # (gpedit.msc) because it interprets whitespace characters in text between
180 # the "string" tags. This prevents gpedit.msc from displaying the category 180 # the "string" tags. This prevents gpedit.msc from displaying the category
181 # names correctly. 181 # names correctly.
182 # TODO(markusheintz): Find a better formatting that works with gpedit. 182 # TODO(markusheintz): Find a better formatting that works with gpedit.
183 return self._doc.toxml() 183 return self._doc.toxml()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698