OLD | NEW |
1 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 | 5 |
6 from grit.format.policy_templates.writers import template_writer | 6 from grit.format.policy_templates.writers import template_writer |
7 | 7 |
8 | 8 |
| 9 NEWLINE = '\r\n' |
| 10 |
| 11 |
9 def GetWriter(config): | 12 def GetWriter(config): |
10 '''Factory method for creating AdmWriter objects. | 13 '''Factory method for creating AdmWriter objects. |
11 See the constructor of TemplateWriter for description of | 14 See the constructor of TemplateWriter for description of |
12 arguments. | 15 arguments. |
13 ''' | 16 ''' |
14 return AdmWriter(['win'], config) | 17 return AdmWriter(['win'], config) |
15 | 18 |
16 | 19 |
17 class AdmWriter(template_writer.TemplateWriter): | 20 class IndentedStringBuilder: |
18 '''Class for generating policy templates in Windows ADM format. | 21 '''Utility class for building text with indented lines.''' |
19 It is used by PolicyTemplateGenerator to write ADM files. | |
20 ''' | |
21 | 22 |
22 TYPE_TO_INPUT = { | 23 def __init__(self): |
23 'string': 'EDITTEXT', | 24 self.lines = [] |
24 'int': 'NUMERIC', | 25 self.indent = '' |
25 'string-enum': 'DROPDOWNLIST', | |
26 'int-enum': 'DROPDOWNLIST', | |
27 'list': 'LISTBOX'} | |
28 NEWLINE = '\r\n' | |
29 | 26 |
30 def _AddGuiString(self, name, value): | 27 def AddLine(self, string='', indent_diff=0): |
31 # Escape newlines in the value. | 28 '''Appends a string with indentation and a linebreak to |self.lines|. |
32 value = value.replace('\n', '\\n') | |
33 line = '%s="%s"' % (name, value) | |
34 self.str_list.append(line) | |
35 | |
36 def _PrintLine(self, string='', indent_diff=0): | |
37 '''Prints a string with indents and a linebreak to the output. | |
38 | 29 |
39 Args: | 30 Args: |
40 string: The string to print. | 31 string: The string to print. |
41 indent_diff: the difference of indentation of the printed line, | 32 indent_diff: the difference of indentation of the printed line, |
42 compared to the next/previous printed line. Increment occurs | 33 compared to the next/previous printed line. Increment occurs |
43 after printing the line, while decrement occurs before that. | 34 after printing the line, while decrement occurs before that. |
44 ''' | 35 ''' |
45 indent_diff *= 2 | 36 indent_diff *= 2 |
46 if indent_diff < 0: | 37 if indent_diff < 0: |
47 self.indent = self.indent[(-indent_diff):] | 38 self.indent = self.indent[(-indent_diff):] |
48 if string != '': | 39 if string != '': |
49 self.policy_list.append(self.indent + string) | 40 self.lines.append(self.indent + string) |
50 else: | 41 else: |
51 self.policy_list.append('') | 42 self.lines.append('') |
52 if indent_diff > 0: | 43 if indent_diff > 0: |
53 self.indent += ''.ljust(indent_diff) | 44 self.indent += ''.ljust(indent_diff) |
54 | 45 |
| 46 def AddLines(self, other): |
| 47 '''Appends the content of another |IndentedStringBuilder| to |self.lines|. |
| 48 Indentation of the added lines will be the sum of |self.indent| and |
| 49 their original indentation. |
| 50 |
| 51 Args: |
| 52 other: The buffer from which lines are copied. |
| 53 ''' |
| 54 for line in other.lines: |
| 55 self.AddLine(line) |
| 56 |
| 57 def ToString(self): |
| 58 '''Returns |self.lines| as text string.''' |
| 59 return NEWLINE.join(self.lines) |
| 60 |
| 61 |
| 62 class AdmWriter(template_writer.TemplateWriter): |
| 63 '''Class for generating policy templates in Windows ADM format. |
| 64 It is used by PolicyTemplateGenerator to write ADM files. |
| 65 ''' |
| 66 |
| 67 TYPE_TO_INPUT = { |
| 68 'string': 'EDITTEXT', |
| 69 'int': 'NUMERIC', |
| 70 'string-enum': 'DROPDOWNLIST', |
| 71 'int-enum': 'DROPDOWNLIST', |
| 72 'list': 'LISTBOX' |
| 73 } |
| 74 |
| 75 def _AddGuiString(self, name, value): |
| 76 # Escape newlines in the value. |
| 77 value = value.replace('\n', '\\n') |
| 78 line = '%s="%s"' % (name, value) |
| 79 self.strings.AddLine(line) |
| 80 |
55 def _WriteSupported(self): | 81 def _WriteSupported(self): |
56 self._PrintLine('#if version >= 4', 1) | 82 self.policies.AddLine('#if version >= 4', 1) |
57 self._PrintLine('SUPPORTED !!SUPPORTED_WINXPSP2') | 83 self.policies.AddLine('SUPPORTED !!SUPPORTED_WINXPSP2') |
58 self._PrintLine('#endif', -1) | 84 self.policies.AddLine('#endif', -1) |
59 | 85 |
60 def _WritePart(self, policy): | 86 def _WritePart(self, policy): |
61 '''Writes the PART ... END PART section of a policy. | 87 '''Writes the PART ... END PART section of a policy. |
62 | 88 |
63 Args: | 89 Args: |
64 policy: The policy to write to the output. | 90 policy: The policy to write to the output. |
65 ''' | 91 ''' |
66 policy_part_name = policy['name'] + '_Part' | 92 policy_part_name = policy['name'] + '_Part' |
67 self._AddGuiString(policy_part_name, policy['label']) | 93 self._AddGuiString(policy_part_name, policy['label']) |
68 | 94 |
69 # Print the PART ... END PART section: | 95 # Print the PART ... END PART section: |
70 self._PrintLine() | 96 self.policies.AddLine() |
71 adm_type = self.TYPE_TO_INPUT[policy['type']] | 97 adm_type = self.TYPE_TO_INPUT[policy['type']] |
72 self._PrintLine('PART !!%s %s' % (policy_part_name, adm_type), 1) | 98 self.policies.AddLine('PART !!%s %s' % (policy_part_name, adm_type), 1) |
73 if policy['type'] == 'list': | 99 if policy['type'] == 'list': |
74 # Note that the following line causes FullArmor ADMX Migrator to create | 100 # Note that the following line causes FullArmor ADMX Migrator to create |
75 # corrupt ADMX files. Please use admx_writer to get ADMX files. | 101 # corrupt ADMX files. Please use admx_writer to get ADMX files. |
76 self._PrintLine('KEYNAME "%s\\%s"' % | 102 self.policies.AddLine('KEYNAME "%s\\%s"' % |
77 (self.config['win_reg_key_name'], policy['name'])) | 103 (self.config['win_reg_key_name'], policy['name'])) |
78 self._PrintLine('VALUEPREFIX ""') | 104 self.policies.AddLine('VALUEPREFIX ""') |
79 else: | 105 else: |
80 self._PrintLine('VALUENAME "%s"' % policy['name']) | 106 self.policies.AddLine('VALUENAME "%s"' % policy['name']) |
81 if policy['type'] in ('int-enum', 'string-enum'): | 107 if policy['type'] in ('int-enum', 'string-enum'): |
82 self._PrintLine('ITEMLIST', 1) | 108 self.policies.AddLine('ITEMLIST', 1) |
83 for item in policy['items']: | 109 for item in policy['items']: |
84 if policy['type'] == 'int-enum': | 110 if policy['type'] == 'int-enum': |
85 value_text = 'NUMERIC ' + str(item['value']) | 111 value_text = 'NUMERIC ' + str(item['value']) |
86 else: | 112 else: |
87 value_text = '"' + item['value'] + '"' | 113 value_text = '"' + item['value'] + '"' |
88 self._PrintLine('NAME !!%s_DropDown VALUE %s' % | 114 self.policies.AddLine('NAME !!%s_DropDown VALUE %s' % |
89 (item['name'], value_text)) | 115 (item['name'], value_text)) |
90 self._AddGuiString(item['name'] + '_DropDown', item['caption']) | 116 self._AddGuiString(item['name'] + '_DropDown', item['caption']) |
91 self._PrintLine('END ITEMLIST', -1) | 117 self.policies.AddLine('END ITEMLIST', -1) |
92 self._PrintLine('END PART', -1) | 118 self.policies.AddLine('END PART', -1) |
93 | 119 |
94 def WritePolicy(self, policy): | 120 def WritePolicy(self, policy): |
95 self._AddGuiString(policy['name'] + '_Policy', policy['caption']) | 121 self._AddGuiString(policy['name'] + '_Policy', policy['caption']) |
96 self._PrintLine('POLICY !!%s_Policy' % policy['name'], 1) | 122 self.policies.AddLine('POLICY !!%s_Policy' % policy['name'], 1) |
97 self._WriteSupported() | 123 self._WriteSupported() |
98 policy_explain_name = policy['name'] + '_Explain' | 124 policy_explain_name = policy['name'] + '_Explain' |
99 self._AddGuiString(policy_explain_name, policy['desc']) | 125 self._AddGuiString(policy_explain_name, policy['desc']) |
100 self._PrintLine('EXPLAIN !!' + policy_explain_name) | 126 self.policies.AddLine('EXPLAIN !!' + policy_explain_name) |
101 | 127 |
102 if policy['type'] == 'main': | 128 if policy['type'] == 'main': |
103 self._PrintLine('VALUENAME "%s"' % policy['name']) | 129 self.policies.AddLine('VALUENAME "%s"' % policy['name']) |
104 self._PrintLine('VALUEON NUMERIC 1') | 130 self.policies.AddLine('VALUEON NUMERIC 1') |
105 self._PrintLine('VALUEOFF NUMERIC 0') | 131 self.policies.AddLine('VALUEOFF NUMERIC 0') |
106 else: | 132 else: |
107 self._WritePart(policy) | 133 self._WritePart(policy) |
108 | 134 |
109 self._PrintLine('END POLICY', -1) | 135 self.policies.AddLine('END POLICY', -1) |
110 self._PrintLine() | 136 self.policies.AddLine() |
111 | 137 |
112 def BeginPolicyGroup(self, group): | 138 def BeginPolicyGroup(self, group): |
113 self._open_category = len(group['policies']) > 1 | 139 self._open_category = len(group['policies']) > 1 |
114 # Open a category for the policies if there is more than one in the | 140 # Open a category for the policies if there is more than one in the |
115 # group. | 141 # group. |
116 if self._open_category: | 142 if self._open_category: |
117 category_name = group['name'] + '_Category' | 143 category_name = group['name'] + '_Category' |
118 self._AddGuiString(category_name, group['caption']) | 144 self._AddGuiString(category_name, group['caption']) |
119 self._PrintLine('CATEGORY !!' + category_name, 1) | 145 self.policies.AddLine('CATEGORY !!' + category_name, 1) |
120 | 146 |
121 def EndPolicyGroup(self): | 147 def EndPolicyGroup(self): |
122 if self._open_category: | 148 if self._open_category: |
123 self._PrintLine('END CATEGORY', -1) | 149 self.policies.AddLine('END CATEGORY', -1) |
| 150 self.policies.AddLine('') |
| 151 |
| 152 def _CreateTemplateForClass(self, policy_class, policies): |
| 153 '''Creates the whole ADM template except for the [Strings] section, and |
| 154 returns it as an |IndentedStringBuilder|. |
| 155 |
| 156 Args: |
| 157 policy_class: USER or MACHINE |
| 158 policies: ADM code for all the policies in an |IndentedStringBuilder|. |
| 159 ''' |
| 160 lines = IndentedStringBuilder() |
| 161 category_path = self.config['win_category_path'] |
| 162 |
| 163 lines.AddLine('CLASS ' + policy_class, 1) |
| 164 if self.config['build'] == 'chrome': |
| 165 lines.AddLine('CATEGORY !!' + category_path[0], 1) |
| 166 lines.AddLine('CATEGORY !!' + category_path[1], 1) |
| 167 elif self.config['build'] == 'chromium': |
| 168 lines.AddLine('CATEGORY !!' + category_path[0], 1) |
| 169 lines.AddLine('KEYNAME "%s"' % self.config['win_reg_key_name']) |
| 170 lines.AddLine() |
| 171 |
| 172 lines.AddLines(policies) |
| 173 |
| 174 if self.config['build'] == 'chrome': |
| 175 lines.AddLine('END CATEGORY', -1) |
| 176 lines.AddLine('END CATEGORY', -1) |
| 177 lines.AddLine('', -1) |
| 178 elif self.config['build'] == 'chromium': |
| 179 lines.AddLine('END CATEGORY', -1) |
| 180 lines.AddLine('', -1) |
| 181 |
| 182 return lines |
124 | 183 |
125 def BeginTemplate(self): | 184 def BeginTemplate(self): |
126 category_path = self.config['win_category_path'] | 185 category_path = self.config['win_category_path'] |
127 self._AddGuiString(self.config['win_supported_os'], | 186 self._AddGuiString(self.config['win_supported_os'], |
128 self.messages['win_supported_winxpsp2']['text']) | 187 self.messages['win_supported_winxpsp2']['text']) |
129 self._PrintLine( | |
130 'CLASS ' + self.config['win_group_policy_class'].upper(), | |
131 1) | |
132 if self.config['build'] == 'chrome': | 188 if self.config['build'] == 'chrome': |
133 self._AddGuiString(category_path[0], 'Google') | 189 self._AddGuiString(category_path[0], 'Google') |
134 self._AddGuiString(category_path[1], self.config['app_name']) | 190 self._AddGuiString(category_path[1], self.config['app_name']) |
135 self._PrintLine('CATEGORY !!' + category_path[0], 1) | |
136 self._PrintLine('CATEGORY !!' + category_path[1], 1) | |
137 elif self.config['build'] == 'chromium': | 191 elif self.config['build'] == 'chromium': |
138 self._AddGuiString(category_path[0], self.config['app_name']) | 192 self._AddGuiString(category_path[0], self.config['app_name']) |
139 self._PrintLine('CATEGORY !!' + category_path[0], 1) | 193 # All the policies will be written into self.policies. |
140 self._PrintLine('KEYNAME "%s"' % self.config['win_reg_key_name']) | 194 # The final template text will be assembled into self.lines by |
141 self._PrintLine() | 195 # self.EndTemplate(). |
142 | 196 |
143 def EndTemplate(self): | 197 def EndTemplate(self): |
144 if self.config['build'] == 'chrome': | 198 # Copy policies into self.lines. |
145 self._PrintLine('END CATEGORY', -1) | 199 policy_class = self.config['win_group_policy_class'].upper() |
146 self._PrintLine('END CATEGORY', -1) | 200 if policy_class in ('BOTH', 'MACHINE'): |
147 self._PrintLine('', -1) | 201 self.lines.AddLines(self._CreateTemplateForClass('MACHINE', |
148 elif self.config['build'] == 'chromium': | 202 self.policies)) |
149 self._PrintLine('END CATEGORY', -1) | 203 if policy_class in ('BOTH', 'USER'): |
150 self._PrintLine('', -1) | 204 self.lines.AddLines(self._CreateTemplateForClass('USER', |
| 205 self.policies)) |
| 206 # Copy user strings into self.lines. |
| 207 self.lines.AddLine('[Strings]') |
| 208 self.lines.AddLines(self.strings) |
151 | 209 |
152 def Init(self): | 210 def Init(self): |
153 self.policy_list = [] | 211 # String buffer for building the whole ADM file. |
154 self.str_list = ['[Strings]'] | 212 self.lines = IndentedStringBuilder() |
155 self.indent = '' | 213 # String buffer for building the strings section of the ADM file. |
| 214 self.strings = IndentedStringBuilder() |
| 215 # String buffer for building the policies of the ADM file. |
| 216 self.policies = IndentedStringBuilder() |
156 | 217 |
157 def GetTemplateText(self): | 218 def GetTemplateText(self): |
158 lines = self.policy_list + self.str_list | 219 return self.lines.ToString() |
159 return self.NEWLINE.join(lines) | |
OLD | NEW |