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 import copy | 7 import copy |
8 import types | |
9 | 8 |
10 | 9 |
11 class PolicyTemplateGenerator: | 10 class PolicyTemplateGenerator: |
12 '''Generates template text for a particular platform. | 11 '''Generates template text for a particular platform. |
13 | 12 |
14 This class is used to traverse a JSON structure from a .json template | 13 This class is used to traverse a JSON structure from a .json template |
15 definition metafile and merge GUI message string definitions that come | 14 definition metafile and merge GUI message string definitions that come |
16 from a .grd resource tree onto it. After this, it can be used to output | 15 from a .grd resource tree onto it. After this, it can be used to output |
17 this data to policy template files using TemplateWriter objects. | 16 this data to policy template files using TemplateWriter objects. |
18 ''' | 17 ''' |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 platforms = [platform] | 96 platforms = [platform] |
98 since_version, until_version = version_part.split('-') | 97 since_version, until_version = version_part.split('-') |
99 result.append({ | 98 result.append({ |
100 'product': product, | 99 'product': product, |
101 'platforms': platforms, | 100 'platforms': platforms, |
102 'since_version': since_version, | 101 'since_version': since_version, |
103 'until_version': until_version | 102 'until_version': until_version |
104 }) | 103 }) |
105 return result | 104 return result |
106 | 105 |
107 def _PrintPolicyValue(self, item): | |
108 '''Produces a string representation for a policy value. Taking care to print | |
109 dictionaries in a sorted order.''' | |
110 if type(item) == types.StringType: | |
111 str_val = "'%s'" % item | |
112 elif isinstance(item, dict): | |
113 str_val = "{"; | |
114 for it in sorted(item.iterkeys()): | |
115 str_val += "\'%s\': %s, " % (it, self._PrintPolicyValue(item[it])) | |
116 str_val = str_val.rstrip(", ") + "}"; | |
117 else: | |
118 str_val = str(item) | |
119 return str_val; | |
120 | |
121 def _ProcessPolicy(self, policy): | 106 def _ProcessPolicy(self, policy): |
122 '''Processes localized message strings in a policy or a group. | 107 '''Processes localized message strings in a policy or a group. |
123 Also breaks up the content of 'supported_on' attribute into a list. | 108 Also breaks up the content of 'supported_on' attribute into a list. |
124 | 109 |
125 Args: | 110 Args: |
126 policy: The data structure of the policy or group, that will get message | 111 policy: The data structure of the policy or group, that will get message |
127 strings here. | 112 strings here. |
128 ''' | 113 ''' |
129 policy['desc'] = self._ImportMessage(policy['desc']) | 114 policy['desc'] = self._ImportMessage(policy['desc']) |
130 policy['caption'] = self._ImportMessage(policy['caption']) | 115 policy['caption'] = self._ImportMessage(policy['caption']) |
131 if 'label' in policy: | 116 if 'label' in policy: |
132 policy['label'] = self._ImportMessage(policy['label']) | 117 policy['label'] = self._ImportMessage(policy['label']) |
133 | 118 |
134 if policy['type'] == 'group': | 119 if policy['type'] == 'group': |
135 self._ProcessPolicyList(policy['policies']) | 120 self._ProcessPolicyList(policy['policies']) |
136 elif policy['type'] in ('string-enum', 'int-enum'): | 121 elif policy['type'] in ('string-enum', 'int-enum'): |
137 # Iterate through all the items of an enum-type policy, and add captions. | 122 # Iterate through all the items of an enum-type policy, and add captions. |
138 for item in policy['items']: | 123 for item in policy['items']: |
139 item['caption'] = self._ImportMessage(item['caption']) | 124 item['caption'] = self._ImportMessage(item['caption']) |
140 elif policy['type'] == 'dict' and 'example_value' in policy: | |
141 policy['example_value'] = self._PrintPolicyValue(policy['example_value']) | |
142 if policy['type'] != 'group': | 125 if policy['type'] != 'group': |
143 if not 'label' in policy: | 126 if not 'label' in policy: |
144 # If 'label' is not specified, then it defaults to 'caption': | 127 # If 'label' is not specified, then it defaults to 'caption': |
145 policy['label'] = policy['caption'] | 128 policy['label'] = policy['caption'] |
146 policy['supported_on'] = self._ProcessSupportedOn(policy['supported_on']) | 129 policy['supported_on'] = self._ProcessSupportedOn(policy['supported_on']) |
147 | 130 |
148 def _ProcessPolicyList(self, policy_list): | 131 def _ProcessPolicyList(self, policy_list): |
149 '''Adds localized message strings to each item in a list of policies and | 132 '''Adds localized message strings to each item in a list of policies and |
150 groups. Also breaks up the content of 'supported_on' attributes into lists | 133 groups. Also breaks up the content of 'supported_on' attributes into lists |
151 of dictionaries. | 134 of dictionaries. |
(...skipping 10 matching lines...) Expand all Loading... |
162 to the constructor, using a given TemplateWriter. | 145 to the constructor, using a given TemplateWriter. |
163 | 146 |
164 Args: | 147 Args: |
165 template_writer: An object implementing TemplateWriter. Its methods | 148 template_writer: An object implementing TemplateWriter. Its methods |
166 are called here for each item of self._policy_groups. | 149 are called here for each item of self._policy_groups. |
167 | 150 |
168 Returns: | 151 Returns: |
169 The text of the generated template. | 152 The text of the generated template. |
170 ''' | 153 ''' |
171 return template_writer.WriteTemplate(self._policy_data) | 154 return template_writer.WriteTemplate(self._policy_data) |
OLD | NEW |