OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
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. | |
5 | |
6 | |
7 import json | |
8 | |
9 from grit.format.policy_templates.writers import template_writer | |
10 | |
11 | |
12 def GetWriter(config): | |
13 '''Factory method for creating RegWriter objects. | |
14 See the constructor of TemplateWriter for description of | |
15 arguments. | |
16 ''' | |
17 return RegWriter(['win'], config) | |
18 | |
19 | |
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 | |
24 write .reg files. | |
25 ''' | |
26 | |
27 NEWLINE = '\r\n' | |
28 | |
29 def _EscapeRegString(self, string): | |
30 return string.replace('\\', '\\\\').replace('\"', '\\\"') | |
31 | |
32 def _StartBlock(self, key, suffix, list): | |
33 key = 'HKEY_LOCAL_MACHINE\\' + key | |
34 if suffix: | |
35 key = key + '\\' + suffix | |
36 if key != self._last_key.get(id(list), None): | |
37 list.append('') | |
38 list.append('[%s]' % key) | |
39 self._last_key[id(list)] = key | |
40 | |
41 def PreprocessPolicies(self, policy_list): | |
42 return self.FlattenGroupsAndSortPolicies(policy_list, | |
43 self.GetPolicySortingKey) | |
44 | |
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. | |
49 ''' | |
50 is_list = policy['type'] in ('list', 'string-enum-list') | |
51 # Lists come after regular policies. | |
52 return (is_list, policy['name']) | |
53 | |
54 def _WritePolicy(self, policy, key, list): | |
55 example_value = policy['example_value'] | |
56 | |
57 if policy['type'] == 'external': | |
58 # This type can only be set through cloud policy. | |
59 return | |
60 elif policy['type'] in ('list', 'string-enum-list'): | |
61 self._StartBlock(key, policy['name'], list) | |
62 i = 1 | |
63 for item in example_value: | |
64 escaped_str = self._EscapeRegString(item) | |
65 list.append('"%d"="%s"' % (i, escaped_str)) | |
66 i = i + 1 | |
67 else: | |
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' | |
76 else: | |
77 example_value_str = 'dword:00000000' | |
78 elif policy['type'] in ('int', 'int-enum'): | |
79 example_value_str = 'dword:%08x' % example_value | |
80 else: | |
81 raise Exception('unknown policy type %s:' % policy['type']) | |
82 | |
83 list.append('"%s"=%s' % (policy['name'], example_value_str)) | |
84 | |
85 def WriteComment(self, comment): | |
86 self._prefix.append('; ' + comment) | |
87 | |
88 def WritePolicy(self, policy): | |
89 if self.CanBeMandatory(policy): | |
90 self._WritePolicy(policy, | |
91 self.config['win_reg_mandatory_key_name'], | |
92 self._mandatory) | |
93 | |
94 def WriteRecommendedPolicy(self, policy): | |
95 self._WritePolicy(policy, | |
96 self.config['win_reg_recommended_key_name'], | |
97 self._recommended) | |
98 | |
99 def BeginTemplate(self): | |
100 pass | |
101 | |
102 def EndTemplate(self): | |
103 pass | |
104 | |
105 def Init(self): | |
106 self._mandatory = [] | |
107 self._recommended = [] | |
108 self._last_key = {} | |
109 self._prefix = [] | |
110 | |
111 def GetTemplateText(self): | |
112 self._prefix.append('Windows Registry Editor Version 5.00') | |
113 if self._GetChromiumVersionString() is not None: | |
114 self.WriteComment(self.config['build'] + ' version: ' + \ | |
115 self._GetChromiumVersionString()) | |
116 all = self._prefix + self._mandatory + self._recommended | |
117 return self.NEWLINE.join(all) | |
OLD | NEW |