Chromium Code Reviews| Index: grit/format/policy_templates/writers/android_policy_writer.py |
| diff --git a/grit/format/policy_templates/writers/android_policy_writer.py b/grit/format/policy_templates/writers/android_policy_writer.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4e4bf5137614c5f679727a2f669c1a940755c8dc |
| --- /dev/null |
| +++ b/grit/format/policy_templates/writers/android_policy_writer.py |
| @@ -0,0 +1,98 @@ |
| +#!/usr/bin/env python |
| +# Copyright (c) 2015 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| + |
| +from grit.format.policy_templates.writers import xml_formatted_writer |
| +from xml.dom import minidom |
| +from xml.sax import saxutils as xml_escape |
| + |
| + |
| +def GetWriter(config): |
| + '''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
|
| + See the constructor of TemplateWriter for description of |
| + arguments. |
| + ''' |
| + return AndroidPolicyWriter(['android'], config) |
| + |
|
Nico
2015/03/02 19:02:06
nit: python style guide says two newlines between
knn
2015/03/02 19:54:35
Done.
|
| +def _EscapeResource(resource): |
| + '''Escape the resource for usage in an Android resource XML file. |
| + This includes standard XML escaping as well as those specific to Android. |
| + ''' |
| + if type(resource) == int: |
| + return str(resource) |
| + return xml_escape.escape(resource, {"'": "\\'", '"': '\\"', '\\': '\\\\'}) |
| + |
| +class AndroidPolicyWriter(xml_formatted_writer.XMLFormattedWriter): |
| + '''Outputs localized Android Resource XML files. |
| + The policy strings are localized and exposed as string resources for |
| + consumption through Android's App restriction Schema. |
| + ''' |
| + |
| + # DOM root node of the generated ADML document. |
| + _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
|
| + # The resources node contains all resource 'string' and 'string-array' |
| + # elements. |
| + _resources = None |
| + |
| + def AddStringResource(self, name, string): |
| + '''Add a string resource of the given name. |
| + ''' |
| + string_node = self._doc.createElement('string') |
| + string_node.setAttribute('name', name) |
| + string_node.appendChild(self._doc.createTextNode(_EscapeResource(string))) |
| + self._resources.appendChild(string_node) |
| + |
| + def AddStringArrayResource(self, name, string_items): |
| + '''Add a string-array resource of the given name and |
| + elements from string_items. |
| + ''' |
| + string_array_node = self._doc.createElement('string-array') |
| + string_array_node.setAttribute('name', name) |
| + self._resources.appendChild(string_array_node) |
| + for item in string_items: |
| + string_node = self._doc.createElement('item') |
| + string_node.appendChild(self._doc.createTextNode(_EscapeResource(item))) |
| + string_array_node.appendChild(string_node) |
| + |
| + def PreprocessPolicies(self, policy_list): |
| + return self.FlattenGroupsAndSortPolicies(policy_list) |
| + |
| + def CanBeRecommended(self, policy): |
| + return False |
| + |
| + def IsDeprecatedPolicySupported(self, policy): |
| + return True |
| + |
| + def IsFuturePolicySupported(self, policy): |
| + return True |
| + |
| + def WritePolicy(self, policy): |
| + name = policy['name'] |
| + self.AddStringResource(name + 'Title', policy['caption']) |
| + |
| + # Get the first line of the policy description. |
| + description = policy['desc'].split('\n', 1)[0] |
| + self.AddStringResource(name + 'Desc', description) |
| + |
| + items = policy.get('items') |
| + if items is not None: |
| + entries = [ item['caption'] for item in items ] |
| + values = [ item['value'] for item in items ] |
| + self.AddStringArrayResource(name + 'Entries', entries) |
| + self.AddStringArrayResource(name + 'Values', values) |
| + |
| + def BeginTemplate(self): |
| + comment_text = 'DO NOT MODIFY THIS FILE DIRECTLY!\n' \ |
| + 'IT IS GENERATED FROM policy_templates.json.' |
| + comment_node = self._doc.createComment(comment_text) |
| + self._doc.insertBefore(comment_node, self._resources) |
| + |
| + 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.
|
| + impl = minidom.getDOMImplementation() |
| + self._doc = impl.createDocument(None, 'resources', None) |
| + self._resources = self._doc.documentElement |
| + |
| + def GetTemplateText(self): |
| + return self.ToPrettyXml(self._doc) |