Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
| 2 # Copyright (c) 2015 The Chromium Authors. All rights reserved. | |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 | |
| 7 from grit.format.policy_templates.writers import xml_formatted_writer | |
| 8 from xml.dom import minidom | |
| 9 from xml.sax import saxutils as xml_escape | |
| 10 | |
| 11 | |
| 12 def GetWriter(config): | |
| 13 '''Factory method for creating AndroidPolicyWriter objects. | |
|
Nico
2015/03/02 19:02:06
Style guide says use """ for doc strings (but ' fo
knn
2015/03/02 19:54:35
Acknowledged. Will have to update other files in t
| |
| 14 See the constructor of TemplateWriter for description of | |
| 15 arguments. | |
| 16 ''' | |
| 17 return AndroidPolicyWriter(['android'], config) | |
| 18 | |
|
Nico
2015/03/02 19:02:06
nit: python style guide says two newlines between
knn
2015/03/02 19:54:35
Done.
| |
| 19 def _EscapeResource(resource): | |
| 20 '''Escape the resource for usage in an Android resource XML file. | |
| 21 This includes standard XML escaping as well as those specific to Android. | |
| 22 ''' | |
| 23 if type(resource) == int: | |
| 24 return str(resource) | |
| 25 return xml_escape.escape(resource, {"'": "\\'", '"': '\\"', '\\': '\\\\'}) | |
| 26 | |
| 27 class AndroidPolicyWriter(xml_formatted_writer.XMLFormattedWriter): | |
| 28 '''Outputs localized Android Resource XML files. | |
| 29 The policy strings are localized and exposed as string resources for | |
| 30 consumption through Android's App restriction Schema. | |
| 31 ''' | |
| 32 | |
| 33 # DOM root node of the generated ADML document. | |
| 34 _doc = None | |
|
Nico
2015/03/02 19:02:06
Instance variables are usually set in __init__ in
knn
2015/03/02 19:54:35
Other files in this directory are using this conve
| |
| 35 # The resources node contains all resource 'string' and 'string-array' | |
| 36 # elements. | |
| 37 _resources = None | |
| 38 | |
| 39 def AddStringResource(self, name, string): | |
| 40 '''Add a string resource of the given name. | |
| 41 ''' | |
| 42 string_node = self._doc.createElement('string') | |
| 43 string_node.setAttribute('name', name) | |
| 44 string_node.appendChild(self._doc.createTextNode(_EscapeResource(string))) | |
| 45 self._resources.appendChild(string_node) | |
| 46 | |
| 47 def AddStringArrayResource(self, name, string_items): | |
| 48 '''Add a string-array resource of the given name and | |
| 49 elements from string_items. | |
| 50 ''' | |
| 51 string_array_node = self._doc.createElement('string-array') | |
| 52 string_array_node.setAttribute('name', name) | |
| 53 self._resources.appendChild(string_array_node) | |
| 54 for item in string_items: | |
| 55 string_node = self._doc.createElement('item') | |
| 56 string_node.appendChild(self._doc.createTextNode(_EscapeResource(item))) | |
| 57 string_array_node.appendChild(string_node) | |
| 58 | |
| 59 def PreprocessPolicies(self, policy_list): | |
| 60 return self.FlattenGroupsAndSortPolicies(policy_list) | |
| 61 | |
| 62 def CanBeRecommended(self, policy): | |
| 63 return False | |
| 64 | |
| 65 def IsDeprecatedPolicySupported(self, policy): | |
| 66 return True | |
| 67 | |
| 68 def IsFuturePolicySupported(self, policy): | |
| 69 return True | |
| 70 | |
| 71 def WritePolicy(self, policy): | |
| 72 name = policy['name'] | |
| 73 self.AddStringResource(name + 'Title', policy['caption']) | |
| 74 | |
| 75 # Get the first line of the policy description. | |
| 76 description = policy['desc'].split('\n', 1)[0] | |
| 77 self.AddStringResource(name + 'Desc', description) | |
| 78 | |
| 79 items = policy.get('items') | |
| 80 if items is not None: | |
| 81 entries = [ item['caption'] for item in items ] | |
| 82 values = [ item['value'] for item in items ] | |
| 83 self.AddStringArrayResource(name + 'Entries', entries) | |
| 84 self.AddStringArrayResource(name + 'Values', values) | |
| 85 | |
| 86 def BeginTemplate(self): | |
| 87 comment_text = 'DO NOT MODIFY THIS FILE DIRECTLY!\n' \ | |
| 88 'IT IS GENERATED FROM policy_templates.json.' | |
| 89 comment_node = self._doc.createComment(comment_text) | |
| 90 self._doc.insertBefore(comment_node, self._resources) | |
| 91 | |
| 92 def Init(self): | |
|
Nico
2015/03/02 19:02:06
What's wrong with doing this in __init__?
(Looks
knn
2015/03/02 19:54:35
Acknowledged. Will update other files as well.
| |
| 93 impl = minidom.getDOMImplementation() | |
| 94 self._doc = impl.createDocument(None, 'resources', None) | |
| 95 self._resources = self._doc.documentElement | |
| 96 | |
| 97 def GetTemplateText(self): | |
| 98 return self.ToPrettyXml(self._doc) | |
| OLD | NEW |