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 grit.format.policy_templates.writers import template_writer | 7 from grit.format.policy_templates.writers import template_writer |
8 | 8 |
9 | 9 |
10 NEWLINE = '\r\n' | 10 NEWLINE = '\r\n' |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 TYPE_TO_INPUT = { | 68 TYPE_TO_INPUT = { |
69 'string': 'EDITTEXT', | 69 'string': 'EDITTEXT', |
70 'int': 'NUMERIC', | 70 'int': 'NUMERIC', |
71 'string-enum': 'DROPDOWNLIST', | 71 'string-enum': 'DROPDOWNLIST', |
72 'int-enum': 'DROPDOWNLIST', | 72 'int-enum': 'DROPDOWNLIST', |
73 'list': 'LISTBOX', | 73 'list': 'LISTBOX', |
74 'string-enum-list': 'LISTBOX', | 74 'string-enum-list': 'LISTBOX', |
75 'dict': 'EDITTEXT' | 75 'dict': 'EDITTEXT' |
76 } | 76 } |
77 | 77 |
| 78 def _Escape(self, string): |
| 79 return string.replace('.', '_') |
| 80 |
78 def _AddGuiString(self, name, value): | 81 def _AddGuiString(self, name, value): |
| 82 # The |name| must be escaped. |
| 83 assert name == self._Escape(name) |
79 # Escape newlines in the value. | 84 # Escape newlines in the value. |
80 value = value.replace('\n', '\\n') | 85 value = value.replace('\n', '\\n') |
81 if name in self.strings_seen: | 86 if name in self.strings_seen: |
82 err = ('%s was added as "%s" and now added again as "%s"' % | 87 err = ('%s was added as "%s" and now added again as "%s"' % |
83 (name, self.strings_seen[name], value)) | 88 (name, self.strings_seen[name], value)) |
84 assert value == self.strings_seen[name], err | 89 assert value == self.strings_seen[name], err |
85 else: | 90 else: |
86 self.strings_seen[name] = value | 91 self.strings_seen[name] = value |
87 line = '%s="%s"' % (name, value) | 92 line = '%s="%s"' % (name, value) |
88 self.strings.AddLine(line) | 93 self.strings.AddLine(line) |
89 | 94 |
90 def _WriteSupported(self, builder): | 95 def _WriteSupported(self, builder): |
91 builder.AddLine('#if version >= 4', 1) | 96 builder.AddLine('#if version >= 4', 1) |
92 builder.AddLine('SUPPORTED !!SUPPORTED_WINXPSP2') | 97 builder.AddLine('SUPPORTED !!SUPPORTED_WINXPSP2') |
93 builder.AddLine('#endif', -1) | 98 builder.AddLine('#endif', -1) |
94 | 99 |
95 def _WritePart(self, policy, key_name, builder): | 100 def _WritePart(self, policy, key_name, builder): |
96 '''Writes the PART ... END PART section of a policy. | 101 '''Writes the PART ... END PART section of a policy. |
97 | 102 |
98 Args: | 103 Args: |
99 policy: The policy to write to the output. | 104 policy: The policy to write to the output. |
100 key_name: The registry key backing the policy. | 105 key_name: The registry key backing the policy. |
101 builder: Builder to append lines to. | 106 builder: Builder to append lines to. |
102 ''' | 107 ''' |
103 policy_part_name = policy['name'] + '_Part' | 108 policy_part_name = self._Escape(policy['name'] + '_Part') |
104 self._AddGuiString(policy_part_name, policy['label']) | 109 self._AddGuiString(policy_part_name, policy['label']) |
105 | 110 |
106 # Print the PART ... END PART section: | 111 # Print the PART ... END PART section: |
107 builder.AddLine() | 112 builder.AddLine() |
108 adm_type = self.TYPE_TO_INPUT[policy['type']] | 113 adm_type = self.TYPE_TO_INPUT[policy['type']] |
109 builder.AddLine('PART !!%s %s' % (policy_part_name, adm_type), 1) | 114 builder.AddLine('PART !!%s %s' % (policy_part_name, adm_type), 1) |
110 if policy['type'] in ('list', 'string-enum-list'): | 115 if policy['type'] in ('list', 'string-enum-list'): |
111 # Note that the following line causes FullArmor ADMX Migrator to create | 116 # Note that the following line causes FullArmor ADMX Migrator to create |
112 # corrupt ADMX files. Please use admx_writer to get ADMX files. | 117 # corrupt ADMX files. Please use admx_writer to get ADMX files. |
113 builder.AddLine('KEYNAME "%s\\%s"' % (key_name, policy['name'])) | 118 builder.AddLine('KEYNAME "%s\\%s"' % (key_name, policy['name'])) |
114 builder.AddLine('VALUEPREFIX ""') | 119 builder.AddLine('VALUEPREFIX ""') |
115 else: | 120 else: |
116 builder.AddLine('VALUENAME "%s"' % policy['name']) | 121 builder.AddLine('VALUENAME "%s"' % policy['name']) |
117 if policy['type'] == 'int': | 122 if policy['type'] == 'int': |
118 # The default max for NUMERIC values is 9999 which is too small for us. | 123 # The default max for NUMERIC values is 9999 which is too small for us. |
119 builder.AddLine('MIN 0 MAX 2000000000') | 124 builder.AddLine('MIN 0 MAX 2000000000') |
120 if policy['type'] in ('int-enum', 'string-enum'): | 125 if policy['type'] in ('int-enum', 'string-enum'): |
121 builder.AddLine('ITEMLIST', 1) | 126 builder.AddLine('ITEMLIST', 1) |
122 for item in policy['items']: | 127 for item in policy['items']: |
123 if policy['type'] == 'int-enum': | 128 if policy['type'] == 'int-enum': |
124 value_text = 'NUMERIC ' + str(item['value']) | 129 value_text = 'NUMERIC ' + str(item['value']) |
125 else: | 130 else: |
126 value_text = '"' + item['value'] + '"' | 131 value_text = '"' + item['value'] + '"' |
127 builder.AddLine('NAME !!%s_DropDown VALUE %s' % | 132 string_id = self._Escape(item['name'] + '_DropDown') |
128 (item['name'], value_text)) | 133 builder.AddLine('NAME !!%s VALUE %s' % (string_id, value_text)) |
129 self._AddGuiString(item['name'] + '_DropDown', item['caption']) | 134 self._AddGuiString(string_id, item['caption']) |
130 builder.AddLine('END ITEMLIST', -1) | 135 builder.AddLine('END ITEMLIST', -1) |
131 builder.AddLine('END PART', -1) | 136 builder.AddLine('END PART', -1) |
132 | 137 |
133 def _WritePolicy(self, policy, key_name, builder): | 138 def _WritePolicy(self, policy, key_name, builder): |
134 if policy['type'] == 'external': | 139 if policy['type'] == 'external': |
135 # This type can only be set through cloud policy. | 140 # This type can only be set through cloud policy. |
136 return | 141 return |
137 | 142 |
138 self._AddGuiString(policy['name'] + '_Policy', policy['caption']) | 143 policy_name = self._Escape(policy['name'] + '_Policy') |
139 builder.AddLine('POLICY !!%s_Policy' % policy['name'], 1) | 144 self._AddGuiString(policy_name, policy['caption']) |
| 145 builder.AddLine('POLICY !!%s' % policy_name, 1) |
140 self._WriteSupported(builder) | 146 self._WriteSupported(builder) |
141 policy_explain_name = policy['name'] + '_Explain' | 147 policy_explain_name = self._Escape(policy['name'] + '_Explain') |
142 self._AddGuiString(policy_explain_name, policy['desc']) | 148 self._AddGuiString(policy_explain_name, policy['desc']) |
143 builder.AddLine('EXPLAIN !!' + policy_explain_name) | 149 builder.AddLine('EXPLAIN !!' + policy_explain_name) |
144 | 150 |
145 if policy['type'] == 'main': | 151 if policy['type'] == 'main': |
146 builder.AddLine('VALUENAME "%s"' % policy['name']) | 152 builder.AddLine('VALUENAME "%s"' % policy['name']) |
147 builder.AddLine('VALUEON NUMERIC 1') | 153 builder.AddLine('VALUEON NUMERIC 1') |
148 builder.AddLine('VALUEOFF NUMERIC 0') | 154 builder.AddLine('VALUEOFF NUMERIC 0') |
149 else: | 155 else: |
150 self._WritePart(policy, key_name, builder) | 156 self._WritePart(policy, key_name, builder) |
151 | 157 |
152 builder.AddLine('END POLICY', -1) | 158 builder.AddLine('END POLICY', -1) |
153 builder.AddLine() | 159 builder.AddLine() |
154 | 160 |
155 def WritePolicy(self, policy): | 161 def WritePolicy(self, policy): |
156 if self.CanBeMandatory(policy): | 162 if self.CanBeMandatory(policy): |
157 self._WritePolicy(policy, | 163 self._WritePolicy(policy, |
158 self.config['win_reg_mandatory_key_name'], | 164 self.config['win_reg_mandatory_key_name'], |
159 self.policies) | 165 self.policies) |
160 | 166 |
161 def WriteRecommendedPolicy(self, policy): | 167 def WriteRecommendedPolicy(self, policy): |
162 self._WritePolicy(policy, | 168 self._WritePolicy(policy, |
163 self.config['win_reg_recommended_key_name'], | 169 self.config['win_reg_recommended_key_name'], |
164 self.recommended_policies) | 170 self.recommended_policies) |
165 | 171 |
166 def BeginPolicyGroup(self, group): | 172 def BeginPolicyGroup(self, group): |
167 category_name = group['name'] + '_Category' | 173 category_name = self._Escape(group['name'] + '_Category') |
168 self._AddGuiString(category_name, group['caption']) | 174 self._AddGuiString(category_name, group['caption']) |
169 self.policies.AddLine('CATEGORY !!' + category_name, 1) | 175 self.policies.AddLine('CATEGORY !!' + category_name, 1) |
170 | 176 |
171 def EndPolicyGroup(self): | 177 def EndPolicyGroup(self): |
172 self.policies.AddLine('END CATEGORY', -1) | 178 self.policies.AddLine('END CATEGORY', -1) |
173 self.policies.AddLine('') | 179 self.policies.AddLine('') |
174 | 180 |
175 def BeginRecommendedPolicyGroup(self, group): | 181 def BeginRecommendedPolicyGroup(self, group): |
176 category_name = group['name'] + '_Category' | 182 category_name = self._Escape(group['name'] + '_Category') |
177 self._AddGuiString(category_name, group['caption']) | 183 self._AddGuiString(category_name, group['caption']) |
178 self.recommended_policies.AddLine('CATEGORY !!' + category_name, 1) | 184 self.recommended_policies.AddLine('CATEGORY !!' + category_name, 1) |
179 | 185 |
180 def EndRecommendedPolicyGroup(self): | 186 def EndRecommendedPolicyGroup(self): |
181 self.recommended_policies.AddLine('END CATEGORY', -1) | 187 self.recommended_policies.AddLine('END CATEGORY', -1) |
182 self.recommended_policies.AddLine('') | 188 self.recommended_policies.AddLine('') |
183 | 189 |
184 def _CreateTemplate(self, category_path, key_name, policies): | 190 def _CreateTemplate(self, category_path, key_name, policies): |
185 '''Creates the whole ADM template except for the [Strings] section, and | 191 '''Creates the whole ADM template except for the [Strings] section, and |
186 returns it as an |IndentedStringBuilder|. | 192 returns it as an |IndentedStringBuilder|. |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 self.strings = IndentedStringBuilder() | 255 self.strings = IndentedStringBuilder() |
250 # Map of strings seen, to avoid duplicates. | 256 # Map of strings seen, to avoid duplicates. |
251 self.strings_seen = {} | 257 self.strings_seen = {} |
252 # String buffer for building the policies of the ADM file. | 258 # String buffer for building the policies of the ADM file. |
253 self.policies = IndentedStringBuilder() | 259 self.policies = IndentedStringBuilder() |
254 # String buffer for building the recommended policies of the ADM file. | 260 # String buffer for building the recommended policies of the ADM file. |
255 self.recommended_policies = IndentedStringBuilder() | 261 self.recommended_policies = IndentedStringBuilder() |
256 | 262 |
257 def GetTemplateText(self): | 263 def GetTemplateText(self): |
258 return self.lines.ToString() | 264 return self.lines.ToString() |
OLD | NEW |