| OLD | NEW |
| 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 | 6 |
| 7 from xml.dom import minidom | 7 from xml.dom import minidom |
| 8 from grit import lazy_re | 8 from grit import lazy_re |
| 9 from grit.format.policy_templates.writers import xml_formatted_writer | 9 from grit.format.policy_templates.writers import xml_formatted_writer |
| 10 | 10 |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 policy: The data structure of a policy. | 227 policy: The data structure of a policy. |
| 228 ''' | 228 ''' |
| 229 examples = self._AddStyledElement(parent, 'dl', ['dd dl']) | 229 examples = self._AddStyledElement(parent, 'dl', ['dd dl']) |
| 230 if self.IsPolicySupportedOnPlatform(policy, 'win'): | 230 if self.IsPolicySupportedOnPlatform(policy, 'win'): |
| 231 self._AddListExampleWindows(examples, policy) | 231 self._AddListExampleWindows(examples, policy) |
| 232 if self.IsPolicySupportedOnPlatform(policy, 'linux'): | 232 if self.IsPolicySupportedOnPlatform(policy, 'linux'): |
| 233 self._AddListExampleLinux(examples, policy) | 233 self._AddListExampleLinux(examples, policy) |
| 234 if self.IsPolicySupportedOnPlatform(policy, 'mac'): | 234 if self.IsPolicySupportedOnPlatform(policy, 'mac'): |
| 235 self._AddListExampleMac(examples, policy) | 235 self._AddListExampleMac(examples, policy) |
| 236 | 236 |
| 237 def _PythonDictionaryToMacDictionary(self, dictionary, indent=''): | 237 def _PythonObjectToPlist(self, obj, indent=''): |
| 238 '''Converts a python dictionary to an equivalent XML plist. | 238 '''Converts a python object to an equivalent XML plist. |
| 239 | 239 |
| 240 Returns a list of lines, with one dictionary entry per line.''' | 240 Returns a list of lines.''' |
| 241 result = [indent + '<dict>'] | 241 obj_type = type(obj) |
| 242 indent += ' ' | 242 if obj_type == bool: |
| 243 for k in sorted(dictionary.keys()): | 243 return [ '%s<%s/>' % (indent, 'true' if obj else 'false') ] |
| 244 v = dictionary[k] | 244 elif obj_type == int: |
| 245 result.append('%s<key>%s</key>' % (indent, k)) | 245 return [ '%s<integer>%s</integer>' % (indent, obj) ] |
| 246 value_type = type(v) | 246 elif obj_type == str: |
| 247 if value_type == bool: | 247 return [ '%s<string>%s</string>' % (indent, obj) ] |
| 248 result.append('%s<%s/>' % (indent, 'true' if v else 'false')) | 248 elif obj_type == list: |
| 249 elif value_type == int: | 249 result = [ '%s<array>' % indent ] |
| 250 result.append('%s<integer>%s</integer>' % (indent, v)) | 250 for item in obj: |
| 251 elif value_type == str: | 251 result += self._PythonObjectToPlist(item, indent + ' ') |
| 252 result.append('%s<string>%s</string>' % (indent, v)) | 252 result.append('%s</array>' % indent) |
| 253 elif value_type == dict: | 253 return result |
| 254 result += self._PythonDictionaryToMacDictionary(v, indent) | 254 elif obj_type == dict: |
| 255 elif value_type == list: | 255 result = [ '%s<dict>' % indent ] |
| 256 array = [] | 256 for key in sorted(obj.keys()): |
| 257 if len(v) != 0: | 257 result.append('%s<key>%s</key>' % (indent + ' ', key)) |
| 258 if type(v[0]) == str: | 258 result += self._PythonObjectToPlist(obj[key], indent + ' ') |
| 259 array = ['%s <string>%s</string>' % (indent, x) for x in v] | 259 result.append('%s</dict>' % indent) |
| 260 elif type(v[0]) == dict: | 260 return result |
| 261 for x in v: | 261 else: |
| 262 array += self._PythonDictionaryToMacDictionary(x, indent + ' ') | 262 raise Exception('Invalid object to convert: %s' % obj) |
| 263 else: | |
| 264 raise Exception('Must be list of string or dict.') | |
| 265 result.append('%s<array>' % indent) | |
| 266 result += array | |
| 267 result.append('%s</array>' % indent) | |
| 268 else: | |
| 269 raise Exception('Invalid example value type %s' % value_type) | |
| 270 result.append(indent[2:] + '</dict>') | |
| 271 return result | |
| 272 | 263 |
| 273 def _AddDictionaryExampleMac(self, parent, policy): | 264 def _AddDictionaryExampleMac(self, parent, policy): |
| 274 '''Adds an example value for Mac of a 'dict' policy to a DOM node. | 265 '''Adds an example value for Mac of a 'dict' policy to a DOM node. |
| 275 | 266 |
| 276 Args: | 267 Args: |
| 277 parent: The DOM node for which the example will be added. | 268 parent: The DOM node for which the example will be added. |
| 278 policy: A policy of type 'dict', for which the Mac example value | 269 policy: A policy of type 'dict', for which the Mac example value |
| 279 is generated. | 270 is generated. |
| 280 ''' | 271 ''' |
| 281 example_value = policy['example_value'] | 272 example_value = policy['example_value'] |
| 282 self.AddElement(parent, 'dt', {}, 'Mac:') | 273 self.AddElement(parent, 'dt', {}, 'Mac:') |
| 283 mac = self._AddStyledElement(parent, 'dd', ['.monospace', '.pre']) | 274 mac = self._AddStyledElement(parent, 'dd', ['.monospace', '.pre']) |
| 284 mac_text = ['<key>%s</key>' % (policy['name'])] | 275 mac_text = ['<key>%s</key>' % (policy['name'])] |
| 285 mac_text += self._PythonDictionaryToMacDictionary(example_value) | 276 mac_text += self._PythonObjectToPlist(example_value) |
| 286 self.AddText(mac, '\n'.join(mac_text)) | 277 self.AddText(mac, '\n'.join(mac_text)) |
| 287 | 278 |
| 288 def _AddDictionaryExampleWindows(self, parent, policy): | 279 def _AddDictionaryExampleWindows(self, parent, policy): |
| 289 '''Adds an example value for Windows of a 'dict' policy to a DOM node. | 280 '''Adds an example value for Windows of a 'dict' policy to a DOM node. |
| 290 | 281 |
| 291 Args: | 282 Args: |
| 292 parent: The DOM node for which the example will be added. | 283 parent: The DOM node for which the example will be added. |
| 293 policy: A policy of type 'dict', for which the Windows example value | 284 policy: A policy of type 'dict', for which the Windows example value |
| 294 is generated. | 285 is generated. |
| 295 ''' | 286 ''' |
| (...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 682 } | 673 } |
| 683 | 674 |
| 684 # A simple regexp to search for URLs. It is enough for now. | 675 # A simple regexp to search for URLs. It is enough for now. |
| 685 self._url_matcher = lazy_re.compile('(http://[^\\s]*[^\\s\\.])') | 676 self._url_matcher = lazy_re.compile('(http://[^\\s]*[^\\s\\.])') |
| 686 | 677 |
| 687 def GetTemplateText(self): | 678 def GetTemplateText(self): |
| 688 # Return the text representation of the main <div> tag. | 679 # Return the text representation of the main <div> tag. |
| 689 return self._main_div.toxml() | 680 return self._main_div.toxml() |
| 690 # To get a complete HTML file, use the following. | 681 # To get a complete HTML file, use the following. |
| 691 # return self._doc.toxml() | 682 # return self._doc.toxml() |
| OLD | NEW |