OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 |
| 6 from xml.dom import minidom |
| 7 from grit.format.policy_templates.writers import plist_helper |
| 8 from grit.format.policy_templates.writers import xml_formatted_writer |
| 9 |
| 10 |
| 11 def GetWriter(config): |
| 12 '''Factory method for creating PListWriter objects. |
| 13 See the constructor of TemplateWriter for description of |
| 14 arguments. |
| 15 ''' |
| 16 return PListWriter(['mac'], config) |
| 17 |
| 18 |
| 19 class PListWriter(xml_formatted_writer.XMLFormattedWriter): |
| 20 '''Class for generating policy templates in Mac plist format. |
| 21 It is used by PolicyTemplateGenerator to write plist files. |
| 22 ''' |
| 23 |
| 24 STRING_TABLE = 'Localizable.strings' |
| 25 TYPE_TO_INPUT = { |
| 26 'string': 'string', |
| 27 'int': 'integer', |
| 28 'int-enum': 'integer', |
| 29 'string-enum': 'string', |
| 30 'main': 'boolean', |
| 31 'list': 'array', |
| 32 } |
| 33 |
| 34 def _AddKeyValuePair(self, parent, key_string, value_tag): |
| 35 '''Adds a plist key-value pair to a parent XML element. |
| 36 |
| 37 A key-value pair in plist consists of two XML elements next two each other: |
| 38 <key>key_string</key> |
| 39 <value_tag>...</value_tag> |
| 40 |
| 41 Args: |
| 42 key_string: The content of the key tag. |
| 43 value_tag: The name of the value element. |
| 44 |
| 45 Returns: |
| 46 The XML element of the value tag. |
| 47 ''' |
| 48 self.AddElement(parent, 'key', {}, key_string) |
| 49 return self.AddElement(parent, value_tag) |
| 50 |
| 51 def _AddStringKeyValuePair(self, parent, key_string, value_string): |
| 52 '''Adds a plist key-value pair to a parent XML element, where the |
| 53 value element contains a string. The name of the value element will be |
| 54 <string>. |
| 55 |
| 56 Args: |
| 57 key_string: The content of the key tag. |
| 58 value_string: The content of the value tag. |
| 59 ''' |
| 60 self.AddElement(parent, 'key', {}, key_string) |
| 61 self.AddElement(parent, 'string', {}, value_string) |
| 62 |
| 63 def _AddTargets(self, parent): |
| 64 '''Adds the following XML snippet to an XML element: |
| 65 <key>pfm_targets</key> |
| 66 <array> |
| 67 <string>user-managed</string> |
| 68 </array> |
| 69 |
| 70 Args: |
| 71 parent: The parent XML element where the snippet will be added. |
| 72 ''' |
| 73 array = self._AddKeyValuePair(parent, 'pfm_targets', 'array') |
| 74 self.AddElement(array, 'string', {}, 'user-managed') |
| 75 |
| 76 def PreprocessPolicies(self, policy_list): |
| 77 return self.FlattenGroupsAndSortPolicies(policy_list) |
| 78 |
| 79 def WritePolicy(self, policy): |
| 80 policy_name = policy['name'] |
| 81 policy_type = policy['type'] |
| 82 |
| 83 dict = self.AddElement(self._array, 'dict') |
| 84 self._AddStringKeyValuePair(dict, 'pfm_name', policy_name) |
| 85 # Set empty strings for title and description. They will be taken by the |
| 86 # OSX Workgroup Manager from the string table in a Localizable.strings file. |
| 87 # Those files are generated by plist_strings_writer. |
| 88 self._AddStringKeyValuePair(dict, 'pfm_description', '') |
| 89 self._AddStringKeyValuePair(dict, 'pfm_title', '') |
| 90 self._AddTargets(dict) |
| 91 self._AddStringKeyValuePair(dict, 'pfm_type', |
| 92 self.TYPE_TO_INPUT[policy_type]) |
| 93 if policy_type in ('int-enum', 'string-enum'): |
| 94 range_list = self._AddKeyValuePair(dict, 'pfm_range_list', 'array') |
| 95 for item in policy['items']: |
| 96 if policy_type == 'int-enum': |
| 97 element_type = 'integer' |
| 98 else: |
| 99 element_type = 'string' |
| 100 self.AddElement(range_list, element_type, {}, str(item['value'])) |
| 101 |
| 102 def BeginTemplate(self): |
| 103 self._plist.attributes['version'] = '1' |
| 104 dict = self.AddElement(self._plist, 'dict') |
| 105 |
| 106 app_name = plist_helper.GetPlistFriendlyName(self.config['app_name']) |
| 107 self._AddStringKeyValuePair(dict, 'pfm_name', app_name) |
| 108 self._AddStringKeyValuePair(dict, 'pfm_description', '') |
| 109 self._AddStringKeyValuePair(dict, 'pfm_title', '') |
| 110 self._AddStringKeyValuePair(dict, 'pfm_version', '1') |
| 111 self._AddStringKeyValuePair(dict, 'pfm_domain', |
| 112 self.config['mac_bundle_id']) |
| 113 |
| 114 self._array = self._AddKeyValuePair(dict, 'pfm_subkeys', 'array') |
| 115 |
| 116 def Init(self): |
| 117 dom_impl = minidom.getDOMImplementation('') |
| 118 doctype = dom_impl.createDocumentType( |
| 119 'plist', |
| 120 '-//Apple//DTD PLIST 1.0//EN', |
| 121 'http://www.apple.com/DTDs/PropertyList-1.0.dtd') |
| 122 self._doc = dom_impl.createDocument(None, 'plist', doctype) |
| 123 self._plist = self._doc.documentElement |
| 124 |
| 125 def GetTemplateText(self): |
| 126 return self.ToPrettyXml(self._doc) |
OLD | NEW |