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

Side by Side Diff: components/policy/tools/template_writers/writers/admx_writer.py

Issue 2481183002: Generate ADMX template for Chrome OS policies (Closed)
Patch Set: Couple of fixes after testing Created 3 years, 3 months 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 writers import xml_formatted_writer 7 from writers import xml_formatted_writer
8 8
9 9
10 def GetWriter(config): 10 def GetWriter(config):
11 '''Factory method for instanciating the ADMXWriter. Every Writer needs a 11 '''Factory method for instanciating the ADMXWriter. Every Writer needs a
12 GetWriter method because the TemplateFormatter uses this method to 12 GetWriter method because the TemplateFormatter uses this method to
13 instantiate a Writer. 13 instantiate a Writer.
14 ''' 14 '''
15 return ADMXWriter(['win'], config) 15 return ADMXWriter(['win'], config)
16 16
17 17
18 class ADMXWriter(xml_formatted_writer.XMLFormattedWriter): 18 class ADMXWriter(xml_formatted_writer.XMLFormattedWriter):
19 '''Class for generating an ADMX policy template. It is used by the 19 '''Class for generating an ADMX policy template. It is used by the
20 PolicyTemplateGenerator to write the admx file. 20 PolicyTemplateGenerator to write the admx file.
21 ''' 21 '''
22 22
23 # DOM root node of the generated ADMX document. 23 # DOM root node of the generated ADMX document.
24 _doc = None 24 _doc = None
25 25
26 # The ADMX "policies" element that contains the ADMX "policy" elements that 26 # The ADMX "policies" element that contains the ADMX "policy" elements that
27 # are generated. 27 # are generated.
28 _active_policies_elem = None 28 _active_policies_elem = None
29 29
30 def Init(self):
31 # Shortcut to platform-specific ADMX/ADM specific configuration.
32 assert len(self.platforms) == 1
33 self.winconfig = self.config['win_config'][self.platforms[0]]
34
30 def _AdmlString(self, name): 35 def _AdmlString(self, name):
31 '''Creates a reference to the named string in an ADML file. 36 '''Creates a reference to the named string in an ADML file.
32 Args: 37 Args:
33 name: Name of the referenced ADML string. 38 name: Name of the referenced ADML string.
34 ''' 39 '''
35 name = name.replace('.', '_') 40 name = name.replace('.', '_')
36 return '$(string.' + name + ')' 41 return '$(string.' + name + ')'
37 42
38 def _AdmlStringExplain(self, name): 43 def _AdmlStringExplain(self, name):
39 '''Creates a reference to the named explanation string in an ADML file. 44 '''Creates a reference to the named explanation string in an ADML file.
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 } 119 }
115 category_elem = self.AddElement(parent, 'category', attributes) 120 category_elem = self.AddElement(parent, 'category', attributes)
116 if parent_category_name: 121 if parent_category_name:
117 attributes = {'ref': parent_category_name} 122 attributes = {'ref': parent_category_name}
118 self.AddElement(category_elem, 'parentCategory', attributes) 123 self.AddElement(category_elem, 'parentCategory', attributes)
119 124
120 def _AddCategories(self, categories): 125 def _AddCategories(self, categories):
121 '''Generates the ADMX "categories" element and adds it to the categories 126 '''Generates the ADMX "categories" element and adds it to the categories
122 main node. The "categories" element defines the category for the policies 127 main node. The "categories" element defines the category for the policies
123 defined in this ADMX document. Here is an example of an ADMX "categories" 128 defined in this ADMX document. Here is an example of an ADMX "categories"
124 element: 129 element on Windows:
125 130
126 <categories> 131 <categories>
127 <category displayName="$(string.googlechrome)" name="googlechrome"> 132 <category displayName="$(string.googlechrome)" name="googlechrome">
128 <parentCategory ref="Google:Cat_Google"/> 133 <parentCategory ref="Google:Cat_Google"/>
129 </category> 134 </category>
130 </categories> 135 </categories>
131 136
132 Args: 137 Args:
133 categories_path: The categories path e.g. ['google', 'googlechrome']. For 138 categories_path: The categories path e.g. ['google', 'googlechrome']. For
134 each level in the path a "category" element will be generated, unless 139 each level in the path a "category" element will be generated, unless
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 passed parent element. 203 passed parent element.
199 ''' 204 '''
200 name = policy['name'] 205 name = policy['name']
201 items = policy['items'] 206 items = policy['items']
202 attributes = { 207 attributes = {
203 'id': name, 208 'id': name,
204 'valueName': name, 209 'valueName': name,
205 } 210 }
206 enum_elem = self.AddElement(parent, 'enum', attributes) 211 enum_elem = self.AddElement(parent, 'enum', attributes)
207 for item in items: 212 for item in items:
208 attributes = {'displayName': self._AdmlString(item['name'])} 213 attributes = {'displayName': self._AdmlString(name + "_" + item['name'])}
209 item_elem = self.AddElement(enum_elem, 'item', attributes) 214 item_elem = self.AddElement(enum_elem, 'item', attributes)
210 value_elem = self.AddElement(item_elem, 'value') 215 value_elem = self.AddElement(item_elem, 'value')
211 value_string = str(item['value']) 216 value_string = str(item['value'])
212 if policy['type'] == 'int-enum': 217 if policy['type'] == 'int-enum':
213 self.AddElement(value_elem, 'decimal', {'value': value_string}) 218 self.AddElement(value_elem, 'decimal', {'value': value_string})
214 else: 219 else:
215 self.AddElement(value_elem, 'string', {}, value_string) 220 self.AddElement(value_elem, 'string', {}, value_string)
216 221
217 def _AddListPolicy(self, parent, key, name): 222 def _AddListPolicy(self, parent, key, name):
218 '''Generates ADMX XML elements for a List-Policy and adds them to the 223 '''Generates ADMX XML elements for a List-Policy and adds them to the
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 ''' 270 '''
266 policies_elem = self._active_policies_elem 271 policies_elem = self._active_policies_elem
267 policy_type = policy['type'] 272 policy_type = policy['type']
268 policy_name = policy['name'] 273 policy_name = policy['name']
269 if policy_type == 'external': 274 if policy_type == 'external':
270 # This type can only be set through cloud policy. 275 # This type can only be set through cloud policy.
271 return 276 return
272 277
273 attributes = { 278 attributes = {
274 'name': name, 279 'name': name,
275 'class': self.config['win_group_policy_class'], 280 'class': self.GetClass(policy),
276 'displayName': self._AdmlString(policy_name), 281 'displayName': self._AdmlString(policy_name),
277 'explainText': self._AdmlStringExplain(policy_name), 282 'explainText': self._AdmlStringExplain(policy_name),
278 'presentation': self._AdmlPresentation(policy_name), 283 'presentation': self._AdmlPresentation(policy_name),
279 'key': key, 284 'key': key,
280 } 285 }
281 # Store the current "policy" AMDX element in self for later use by the 286 # Store the current "policy" AMDX element in self for later use by the
282 # WritePolicy method. 287 # WritePolicy method.
283 policy_elem = self.AddElement(policies_elem, 'policy', 288 policy_elem = self.AddElement(policies_elem, 'policy',
284 attributes) 289 attributes)
285 self.AddElement(policy_elem, 'parentCategory', 290 self.AddElement(policy_elem, 'parentCategory',
(...skipping 18 matching lines...) Expand all
304 self._AddListPolicy(parent, key, policy_name) 309 self._AddListPolicy(parent, key, policy_name)
305 elif policy_type == 'group': 310 elif policy_type == 'group':
306 pass 311 pass
307 else: 312 else:
308 raise Exception('Unknown policy type %s.' % policy_type) 313 raise Exception('Unknown policy type %s.' % policy_type)
309 314
310 def WritePolicy(self, policy): 315 def WritePolicy(self, policy):
311 if self.CanBeMandatory(policy): 316 if self.CanBeMandatory(policy):
312 self._WritePolicy(policy, 317 self._WritePolicy(policy,
313 policy['name'], 318 policy['name'],
314 self.config['win_reg_mandatory_key_name'], 319 self.winconfig['reg_mandatory_key_name'],
315 self._active_mandatory_policy_group_name) 320 self._active_mandatory_policy_group_name)
316 321
317 def WriteRecommendedPolicy(self, policy): 322 def WriteRecommendedPolicy(self, policy):
318 self._WritePolicy(policy, 323 self._WritePolicy(policy,
319 policy['name'] + '_recommended', 324 policy['name'] + '_recommended',
320 self.config['win_reg_recommended_key_name'], 325 self.winconfig['reg_recommended_key_name'],
321 self._active_recommended_policy_group_name) 326 self._active_recommended_policy_group_name)
322 327
323 def _BeginPolicyGroup(self, group, name, parent): 328 def _BeginPolicyGroup(self, group, name, parent):
324 '''Generates ADMX elements for a Policy-Group. 329 '''Generates ADMX elements for a Policy-Group.
325 ''' 330 '''
326 attributes = { 331 attributes = {
327 'name': name, 332 'name': name,
328 'displayName': self._AdmlString(group['name'] + '_group'), 333 'displayName': self._AdmlString(group['name'] + '_group'),
329 } 334 }
330 category_elem = self.AddElement(self._categories_elem, 335 category_elem = self.AddElement(self._categories_elem,
331 'category', 336 'category',
332 attributes) 337 attributes)
333 attributes = { 338 attributes = {
334 'ref': parent 339 'ref': parent
335 } 340 }
336 self.AddElement(category_elem, 'parentCategory', attributes) 341 self.AddElement(category_elem, 'parentCategory', attributes)
337 342
338 def BeginPolicyGroup(self, group): 343 def BeginPolicyGroup(self, group):
339 self._BeginPolicyGroup(group, 344 self._BeginPolicyGroup(group,
340 group['name'], 345 group['name'],
341 self.config['win_mandatory_category_path'][-1]) 346 self.winconfig['mandatory_category_path'][-1])
342 self._active_mandatory_policy_group_name = group['name'] 347 self._active_mandatory_policy_group_name = group['name']
343 348
344 def EndPolicyGroup(self): 349 def EndPolicyGroup(self):
345 self._active_mandatory_policy_group_name = \ 350 self._active_mandatory_policy_group_name = \
346 self.config['win_mandatory_category_path'][-1] 351 self.winconfig['mandatory_category_path'][-1]
347 352
348 def BeginRecommendedPolicyGroup(self, group): 353 def BeginRecommendedPolicyGroup(self, group):
349 self._BeginPolicyGroup(group, 354 self._BeginPolicyGroup(group,
350 group['name'] + '_recommended', 355 group['name'] + '_recommended',
351 self.config['win_recommended_category_path'][-1]) 356 self.winconfig['recommended_category_path'][-1])
352 self._active_recommended_policy_group_name = group['name'] + '_recommended' 357 self._active_recommended_policy_group_name = group['name'] + '_recommended'
353 358
354 def EndRecommendedPolicyGroup(self): 359 def EndRecommendedPolicyGroup(self):
355 self._active_recommended_policy_group_name = \ 360 self._active_recommended_policy_group_name = \
356 self.config['win_recommended_category_path'][-1] 361 self.winconfig['recommended_category_path'][-1]
357 362
358 def BeginTemplate(self): 363 def BeginTemplate(self):
359 '''Generates the skeleton of the ADMX template. An ADMX template contains 364 '''Generates the skeleton of the ADMX template. An ADMX template contains
360 an ADMX "PolicyDefinitions" element with four child nodes: "policies" 365 an ADMX "PolicyDefinitions" element with four child nodes: "policies"
361 "policyNamspaces", "resources", "supportedOn" and "categories" 366 "policyNamspaces", "resources", "supportedOn" and "categories"
362 ''' 367 '''
363 dom_impl = minidom.getDOMImplementation('') 368 dom_impl = minidom.getDOMImplementation('')
364 self._doc = dom_impl.createDocument(None, 'policyDefinitions', None) 369 self._doc = dom_impl.createDocument(None, 'policyDefinitions', None)
365 if self._GetChromiumVersionString() is not None: 370 if self._GetChromiumVersionString() is not None:
366 self.AddComment(self._doc.documentElement, self.config['build'] + \ 371 self.AddComment(self._doc.documentElement, self.config['build'] + \
367 ' version: ' + self._GetChromiumVersionString()) 372 ' version: ' + self._GetChromiumVersionString())
368 policy_definitions_elem = self._doc.documentElement 373 policy_definitions_elem = self._doc.documentElement
369 374
370 policy_definitions_elem.attributes['revision'] = '1.0' 375 policy_definitions_elem.attributes['revision'] = '1.0'
371 policy_definitions_elem.attributes['schemaVersion'] = '1.0' 376 policy_definitions_elem.attributes['schemaVersion'] = '1.0'
372 377
373 self._AddPolicyNamespaces(policy_definitions_elem, 378 self._AddPolicyNamespaces(policy_definitions_elem,
374 self.config['admx_prefix'], 379 self.config['admx_prefix'],
375 self.config['admx_namespace']) 380 self.winconfig['namespace'])
376 self.AddElement(policy_definitions_elem, 'resources', 381 self.AddElement(policy_definitions_elem, 'resources',
377 {'minRequiredRevision' : '1.0'}) 382 {'minRequiredRevision' : '1.0'})
378 self._AddSupportedOn(policy_definitions_elem, 383 self._AddSupportedOn(policy_definitions_elem,
379 self.config['win_supported_os']) 384 self.config['win_supported_os'])
380 self._categories_elem = self.AddElement(policy_definitions_elem, 385 self._categories_elem = self.AddElement(policy_definitions_elem,
381 'categories') 386 'categories')
382 self._AddCategories(self.config['win_mandatory_category_path']) 387 self._AddCategories(self.winconfig['mandatory_category_path'])
383 self._AddCategories(self.config['win_recommended_category_path']) 388 self._AddCategories(self.winconfig['recommended_category_path'])
384 self._active_policies_elem = self.AddElement(policy_definitions_elem, 389 self._active_policies_elem = self.AddElement(policy_definitions_elem,
385 'policies') 390 'policies')
386 self._active_mandatory_policy_group_name = \ 391 self._active_mandatory_policy_group_name = \
387 self.config['win_mandatory_category_path'][-1] 392 self.winconfig['mandatory_category_path'][-1]
388 self._active_recommended_policy_group_name = \ 393 self._active_recommended_policy_group_name = \
389 self.config['win_recommended_category_path'][-1] 394 self.winconfig['recommended_category_path'][-1]
390 395
391 def GetTemplateText(self): 396 def GetTemplateText(self):
392 return self.ToPrettyXml(self._doc) 397 return self.ToPrettyXml(self._doc)
398
399 def GetClass(self, policy):
400 return 'Both'
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698