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
4 # found in the LICENSE file.
9 from grit.format.policy_templates.writers import template_writer
12 def GetWriter(config):
13 '''Factory method for creating RegWriter objects.
14 See the constructor of TemplateWriter for description of
17 return RegWriter(['win'], config)
20 class RegWriter(template_writer.TemplateWriter):
21 '''Class for generating policy example files in .reg format (for Windows).
22 The generated files will define all the supported policies with example
23 values set for them. This class is used by PolicyTemplateGenerator to
29 def _EscapeRegString(self, string):
30 return string.replace('\\', '\\\\').replace('\"', '\\\"')
32 def _StartBlock(self, key, suffix, list):
33 key = 'HKEY_LOCAL_MACHINE\\' + key
35 key = key + '\\' + suffix
36 if key != self._last_key.get(id(list), None):
38 list.append('[%s]' % key)
39 self._last_key[id(list)] = key
41 def PreprocessPolicies(self, policy_list):
42 return self.FlattenGroupsAndSortPolicies(policy_list,
43 self.GetPolicySortingKey)
45 def GetPolicySortingKey(self, policy):
46 '''Extracts a sorting key from a policy. These keys can be used for
47 list.sort() methods to sort policies.
48 See TemplateWriter.SortPoliciesGroupsFirst for usage.
50 is_list = policy['type'] in ('list', 'string-enum-list')
51 # Lists come after regular policies.
52 return (is_list, policy['name'])
54 def _WritePolicy(self, policy, key, list):
55 example_value = policy['example_value']
57 if policy['type'] == 'external':
58 # This type can only be set through cloud policy.
60 elif policy['type'] in ('list', 'string-enum-list'):
61 self._StartBlock(key, policy['name'], list)
63 for item in example_value:
64 escaped_str = self._EscapeRegString(item)
65 list.append('"%d"="%s"' % (i, escaped_str))
68 self._StartBlock(key, None, list)
69 if policy['type'] in ('string', 'string-enum', 'dict'):
70 example_value_str = json.dumps(example_value, sort_keys=True)
71 if policy['type'] == 'dict':
72 example_value_str = '"%s"' % example_value_str
73 elif policy['type'] == 'main':
74 if example_value == True:
75 example_value_str = 'dword:00000001'
77 example_value_str = 'dword:00000000'
78 elif policy['type'] in ('int', 'int-enum'):
79 example_value_str = 'dword:%08x' % example_value
81 raise Exception('unknown policy type %s:' % policy['type'])
83 list.append('"%s"=%s' % (policy['name'], example_value_str))
85 def WritePolicy(self, policy):
86 if self.CanBeMandatory(policy):
87 self._WritePolicy(policy,
88 self.config['win_reg_mandatory_key_name'],
91 def WriteRecommendedPolicy(self, policy):
92 self._WritePolicy(policy,
93 self.config['win_reg_recommended_key_name'],
96 def BeginTemplate(self):
99 def EndTemplate(self):
104 self._recommended = []
107 def GetTemplateText(self):
108 prefix = ['Windows Registry Editor Version 5.00']
109 all = prefix + self._mandatory + self._recommended
110 return self.NEWLINE.join(all)