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 grit.format.policy_templates.writers import template_writer |
| 7 |
| 8 |
| 9 NEWLINE = '\r\n' |
| 10 |
| 11 |
| 12 def GetWriter(config): |
| 13 '''Factory method for creating AdmWriter objects. |
| 14 See the constructor of TemplateWriter for description of |
| 15 arguments. |
| 16 ''' |
| 17 return AdmWriter(['win'], config) |
| 18 |
| 19 |
| 20 class IndentedStringBuilder: |
| 21 '''Utility class for building text with indented lines.''' |
| 22 |
| 23 def __init__(self): |
| 24 self.lines = [] |
| 25 self.indent = '' |
| 26 |
| 27 def AddLine(self, string='', indent_diff=0): |
| 28 '''Appends a string with indentation and a linebreak to |self.lines|. |
| 29 |
| 30 Args: |
| 31 string: The string to print. |
| 32 indent_diff: the difference of indentation of the printed line, |
| 33 compared to the next/previous printed line. Increment occurs |
| 34 after printing the line, while decrement occurs before that. |
| 35 ''' |
| 36 indent_diff *= 2 |
| 37 if indent_diff < 0: |
| 38 self.indent = self.indent[(-indent_diff):] |
| 39 if string != '': |
| 40 self.lines.append(self.indent + string) |
| 41 else: |
| 42 self.lines.append('') |
| 43 if indent_diff > 0: |
| 44 self.indent += ''.ljust(indent_diff) |
| 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 |
| 81 def _WriteSupported(self): |
| 82 self.policies.AddLine('#if version >= 4', 1) |
| 83 self.policies.AddLine('SUPPORTED !!SUPPORTED_WINXPSP2') |
| 84 self.policies.AddLine('#endif', -1) |
| 85 |
| 86 def _WritePart(self, policy): |
| 87 '''Writes the PART ... END PART section of a policy. |
| 88 |
| 89 Args: |
| 90 policy: The policy to write to the output. |
| 91 ''' |
| 92 policy_part_name = policy['name'] + '_Part' |
| 93 self._AddGuiString(policy_part_name, policy['label']) |
| 94 |
| 95 # Print the PART ... END PART section: |
| 96 self.policies.AddLine() |
| 97 adm_type = self.TYPE_TO_INPUT[policy['type']] |
| 98 self.policies.AddLine('PART !!%s %s' % (policy_part_name, adm_type), 1) |
| 99 if policy['type'] == 'list': |
| 100 # Note that the following line causes FullArmor ADMX Migrator to create |
| 101 # corrupt ADMX files. Please use admx_writer to get ADMX files. |
| 102 self.policies.AddLine('KEYNAME "%s\\%s"' % |
| 103 (self.config['win_reg_key_name'], policy['name'])) |
| 104 self.policies.AddLine('VALUEPREFIX ""') |
| 105 else: |
| 106 self.policies.AddLine('VALUENAME "%s"' % policy['name']) |
| 107 if policy['type'] in ('int-enum', 'string-enum'): |
| 108 self.policies.AddLine('ITEMLIST', 1) |
| 109 for item in policy['items']: |
| 110 if policy['type'] == 'int-enum': |
| 111 value_text = 'NUMERIC ' + str(item['value']) |
| 112 else: |
| 113 value_text = '"' + item['value'] + '"' |
| 114 self.policies.AddLine('NAME !!%s_DropDown VALUE %s' % |
| 115 (item['name'], value_text)) |
| 116 self._AddGuiString(item['name'] + '_DropDown', item['caption']) |
| 117 self.policies.AddLine('END ITEMLIST', -1) |
| 118 self.policies.AddLine('END PART', -1) |
| 119 |
| 120 def WritePolicy(self, policy): |
| 121 self._AddGuiString(policy['name'] + '_Policy', policy['caption']) |
| 122 self.policies.AddLine('POLICY !!%s_Policy' % policy['name'], 1) |
| 123 self._WriteSupported() |
| 124 policy_explain_name = policy['name'] + '_Explain' |
| 125 self._AddGuiString(policy_explain_name, policy['desc']) |
| 126 self.policies.AddLine('EXPLAIN !!' + policy_explain_name) |
| 127 |
| 128 if policy['type'] == 'main': |
| 129 self.policies.AddLine('VALUENAME "%s"' % policy['name']) |
| 130 self.policies.AddLine('VALUEON NUMERIC 1') |
| 131 self.policies.AddLine('VALUEOFF NUMERIC 0') |
| 132 else: |
| 133 self._WritePart(policy) |
| 134 |
| 135 self.policies.AddLine('END POLICY', -1) |
| 136 self.policies.AddLine() |
| 137 |
| 138 def BeginPolicyGroup(self, group): |
| 139 self._open_category = len(group['policies']) > 1 |
| 140 # Open a category for the policies if there is more than one in the |
| 141 # group. |
| 142 if self._open_category: |
| 143 category_name = group['name'] + '_Category' |
| 144 self._AddGuiString(category_name, group['caption']) |
| 145 self.policies.AddLine('CATEGORY !!' + category_name, 1) |
| 146 |
| 147 def EndPolicyGroup(self): |
| 148 if self._open_category: |
| 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 |
| 183 |
| 184 def BeginTemplate(self): |
| 185 category_path = self.config['win_category_path'] |
| 186 self._AddGuiString(self.config['win_supported_os'], |
| 187 self.messages['win_supported_winxpsp2']['text']) |
| 188 if self.config['build'] == 'chrome': |
| 189 self._AddGuiString(category_path[0], 'Google') |
| 190 self._AddGuiString(category_path[1], self.config['app_name']) |
| 191 elif self.config['build'] == 'chromium': |
| 192 self._AddGuiString(category_path[0], self.config['app_name']) |
| 193 # All the policies will be written into self.policies. |
| 194 # The final template text will be assembled into self.lines by |
| 195 # self.EndTemplate(). |
| 196 |
| 197 def EndTemplate(self): |
| 198 # Copy policies into self.lines. |
| 199 policy_class = self.config['win_group_policy_class'].upper() |
| 200 if policy_class in ('BOTH', 'MACHINE'): |
| 201 self.lines.AddLines(self._CreateTemplateForClass('MACHINE', |
| 202 self.policies)) |
| 203 if policy_class in ('BOTH', 'USER'): |
| 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) |
| 209 |
| 210 def Init(self): |
| 211 # String buffer for building the whole ADM file. |
| 212 self.lines = IndentedStringBuilder() |
| 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() |
| 217 |
| 218 def GetTemplateText(self): |
| 219 return self.lines.ToString() |
OLD | NEW |