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