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

Unified Diff: grit/format/policy_templates/writers/android_policy_writer.py

Issue 865563002: Add a Policy template writer that generates Android resources which can be exposed through Android'… (Closed) Base URL: https://chromium.googlesource.com/external/grit-i18n.git@master
Patch Set: Rebase on r186 Created 5 years, 10 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 side-by-side diff with in-line comments
Download patch
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..5d8cbef38f991b2964d15d36ce6f6e3b45133072
--- /dev/null
+++ b/grit/format/policy_templates/writers/android_policy_writer.py
@@ -0,0 +1,100 @@
+#!/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.
+ See the constructor of TemplateWriter for description of
+ arguments.
+ '''
+ return AndroidPolicyWriter(['android'], config)
+
+
+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 XML document.
+ _doc = None
+ # 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):
+ impl = minidom.getDOMImplementation()
+ self._doc = impl.createDocument(None, 'resources', None)
+ self._resources = self._doc.documentElement
+
+ def GetTemplateText(self):
+ return self.ToPrettyXml(self._doc)
« no previous file with comments | « grit/format/policy_templates/PRESUBMIT.py ('k') | grit/format/policy_templates/writers/android_policy_writer_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698