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 template_writer |
| 8 |
| 9 |
| 10 def GetWriter(config): |
| 11 '''Factory method for creating RegWriter objects. |
| 12 See the constructor of TemplateWriter for description of |
| 13 arguments. |
| 14 ''' |
| 15 return RegWriter(['win'], config) |
| 16 |
| 17 |
| 18 class RegWriter(template_writer.TemplateWriter): |
| 19 '''Class for generating policy example files in .reg format (for Windows). |
| 20 The generated files will define all the supported policies with example |
| 21 values set for them. This class is used by PolicyTemplateGenerator to |
| 22 write .reg files. |
| 23 ''' |
| 24 |
| 25 NEWLINE = '\r\n' |
| 26 |
| 27 def _EscapeRegString(self, string): |
| 28 return string.replace('\\', '\\\\').replace('\"', '\\\"') |
| 29 |
| 30 def _StartBlock(self, suffix): |
| 31 key = 'HKEY_LOCAL_MACHINE\\' + self.config['win_reg_key_name'] |
| 32 if suffix: |
| 33 key = key + '\\' + suffix |
| 34 if key != self._last_key: |
| 35 self._out.append('') |
| 36 self._out.append('[%s]' % key) |
| 37 self._last_key = key |
| 38 |
| 39 def PreprocessPolicies(self, policy_list): |
| 40 return self.FlattenGroupsAndSortPolicies(policy_list, |
| 41 self.GetPolicySortingKey) |
| 42 |
| 43 def GetPolicySortingKey(self, policy): |
| 44 '''Extracts a sorting key from a policy. These keys can be used for |
| 45 list.sort() methods to sort policies. |
| 46 See TemplateWriter.SortPoliciesGroupsFirst for usage. |
| 47 ''' |
| 48 is_list = policy['type'] == 'list' |
| 49 # Lists come after regular policies. |
| 50 return (is_list, policy['name']) |
| 51 |
| 52 def WritePolicy(self, policy): |
| 53 example_value = policy['example_value'] |
| 54 |
| 55 if policy['type'] == 'list': |
| 56 self._StartBlock(policy['name']) |
| 57 i = 1 |
| 58 for item in example_value: |
| 59 escaped_str = self._EscapeRegString(item) |
| 60 self._out.append('"%d"="%s"' % (i, escaped_str)) |
| 61 i = i + 1 |
| 62 else: |
| 63 self._StartBlock(None) |
| 64 if policy['type'] == 'string': |
| 65 escaped_str = self._EscapeRegString(example_value) |
| 66 example_value_str = '"' + escaped_str + '"' |
| 67 elif policy['type'] == 'main': |
| 68 if example_value == True: |
| 69 example_value_str = 'dword:00000001' |
| 70 else: |
| 71 example_value_str = 'dword:00000000' |
| 72 elif policy['type'] in ('int', 'int-enum'): |
| 73 example_value_str = 'dword:%08x' % example_value |
| 74 elif policy['type'] == 'string-enum': |
| 75 example_value_str = '"%s"' % example_value |
| 76 else: |
| 77 raise Exception('unknown policy type %s:' % policy['type']) |
| 78 |
| 79 self._out.append('"%s"=%s' % (policy['name'], example_value_str)) |
| 80 |
| 81 def BeginTemplate(self): |
| 82 pass |
| 83 |
| 84 def EndTemplate(self): |
| 85 pass |
| 86 |
| 87 def Init(self): |
| 88 self._out = ['Windows Registry Editor Version 5.00'] |
| 89 self._last_key = None |
| 90 |
| 91 def GetTemplateText(self): |
| 92 return self.NEWLINE.join(self._out) |
OLD | NEW |