Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: grit/format/policy_templates/writers/template_writer.py

Issue 1442863002: Remove contents of grit's SVN repository. (Closed) Base URL: http://grit-i18n.googlecode.com/svn/trunk/
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 #!/usr/bin/env python
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
4 # found in the LICENSE file.
5
6
7 class TemplateWriter(object):
8 '''Abstract base class for writing policy templates in various formats.
9 The methods of this class will be called by PolicyTemplateGenerator.
10 '''
11
12 def __init__(self, platforms, config):
13 '''Initializes a TemplateWriter object.
14
15 Args:
16 platforms: List of platforms for which this writer can write policies.
17 config: A dictionary of information required to generate the template.
18 It contains some key-value pairs, including the following examples:
19 'build': 'chrome' or 'chromium'
20 'branding': 'Google Chrome' or 'Chromium'
21 'mac_bundle_id': The Mac bundle id of Chrome. (Only set when building
22 for Mac.)
23 messages: List of all the message strings from the grd file. Most of them
24 are also present in the policy data structures that are passed to
25 methods. That is the preferred way of accessing them, this should only
26 be used in exceptional cases. An example for its use is the
27 IDS_POLICY_WIN_SUPPORTED_WINXPSP2 message in ADM files, because that
28 cannot be associated with any policy or group.
29 '''
30 self.platforms = platforms
31 self.config = config
32
33 def IsDeprecatedPolicySupported(self, policy):
34 '''Checks if the given deprecated policy is supported by the writer.
35
36 Args:
37 policy: The dictionary of the policy.
38
39 Returns:
40 True if the writer chooses to include the deprecated 'policy' in its
41 output.
42 '''
43 return False
44
45 def IsFuturePolicySupported(self, policy):
46 '''Checks if the given future policy is supported by the writer.
47
48 Args:
49 policy: The dictionary of the policy.
50
51 Returns:
52 True if the writer chooses to include the deprecated 'policy' in its
53 output.
54 '''
55 return False
56
57 def IsPolicySupported(self, policy):
58 '''Checks if the given policy is supported by the writer.
59 In other words, the set of platforms supported by the writer
60 has a common subset with the set of platforms that support
61 the policy.
62
63 Args:
64 policy: The dictionary of the policy.
65
66 Returns:
67 True if the writer chooses to include 'policy' in its output.
68 '''
69 if ('deprecated' in policy and policy['deprecated'] is True and
70 not self.IsDeprecatedPolicySupported(policy)):
71 return False
72
73 if ('future' in policy and policy['future'] is True and
74 not self.IsFuturePolicySupported(policy)):
75 return False
76
77 if '*' in self.platforms:
78 # Currently chrome_os is only catched here.
79 return True
80 for supported_on in policy['supported_on']:
81 for supported_on_platform in supported_on['platforms']:
82 if supported_on_platform in self.platforms:
83 return True
84 return False
85
86 def CanBeRecommended(self, policy):
87 '''Checks if the given policy can be recommended.'''
88 return policy.get('features', {}).get('can_be_recommended', False)
89
90 def CanBeMandatory(self, policy):
91 '''Checks if the given policy can be mandatory.'''
92 return policy.get('features', {}).get('can_be_mandatory', True)
93
94 def IsPolicySupportedOnPlatform(self, policy, platform, product=None):
95 '''Checks if |policy| is supported on |product| for |platform|. If not
96 specified, only the platform support is checked.
97
98 Args:
99 policy: The dictionary of the policy.
100 platform: The platform to check; one of 'win', 'mac', 'linux' or
101 'chrome_os'.
102 product: Optional product to check; one of 'chrome', 'chrome_frame',
103 'chrome_os', 'webview'
104 '''
105 is_supported = lambda x: (platform in x['platforms'] and
106 (not product or product in x['product']))
107
108 return any(filter(is_supported, policy['supported_on']))
109
110 def _GetChromiumVersionString(self):
111 '''Returns the Chromium version string stored in the environment variable
112 version (if it is set).
113
114 Returns: The Chromium version string or None if it has not been set.'''
115
116 if 'version' in self.config:
117 return self.config['version']
118
119 def _GetPoliciesForWriter(self, group):
120 '''Filters the list of policies in the passed group that are supported by
121 the writer.
122
123 Args:
124 group: The dictionary of the policy group.
125
126 Returns: The list of policies of the policy group that are compatible
127 with the writer.
128 '''
129 if not 'policies' in group:
130 return []
131 result = []
132 for policy in group['policies']:
133 if self.IsPolicySupported(policy):
134 result.append(policy)
135 return result
136
137 def Init(self):
138 '''Initializes the writer. If the WriteTemplate method is overridden, then
139 this method must be called as first step of each template generation
140 process.
141 '''
142 pass
143
144 def WriteTemplate(self, template):
145 '''Writes the given template definition.
146
147 Args:
148 template: Template definition to write.
149
150 Returns:
151 Generated output for the passed template definition.
152 '''
153 self.messages = template['messages']
154 self.Init()
155 template['policy_definitions'] = \
156 self.PreprocessPolicies(template['policy_definitions'])
157 self.BeginTemplate()
158 for policy in template['policy_definitions']:
159 if policy['type'] == 'group':
160 child_policies = self._GetPoliciesForWriter(policy)
161 child_recommended_policies = filter(self.CanBeRecommended,
162 child_policies)
163 if child_policies:
164 # Only write nonempty groups.
165 self.BeginPolicyGroup(policy)
166 for child_policy in child_policies:
167 # Nesting of groups is currently not supported.
168 self.WritePolicy(child_policy)
169 self.EndPolicyGroup()
170 if child_recommended_policies:
171 self.BeginRecommendedPolicyGroup(policy)
172 for child_policy in child_recommended_policies:
173 self.WriteRecommendedPolicy(child_policy)
174 self.EndRecommendedPolicyGroup()
175 elif self.IsPolicySupported(policy):
176 self.WritePolicy(policy)
177 if self.CanBeRecommended(policy):
178 self.WriteRecommendedPolicy(policy)
179 self.EndTemplate()
180
181 return self.GetTemplateText()
182
183 def PreprocessPolicies(self, policy_list):
184 '''Preprocesses a list of policies according to a given writer's needs.
185 Preprocessing steps include sorting policies and stripping unneeded
186 information such as groups (for writers that ignore them).
187 Subclasses are encouraged to override this method, overriding
188 implementations may call one of the provided specialized implementations.
189 The default behaviour is to use SortPoliciesGroupsFirst().
190
191 Args:
192 policy_list: A list containing the policies to sort.
193
194 Returns:
195 The sorted policy list.
196 '''
197 return self.SortPoliciesGroupsFirst(policy_list)
198
199 def WritePolicy(self, policy):
200 '''Appends the template text corresponding to a policy into the
201 internal buffer.
202
203 Args:
204 policy: The policy as it is found in the JSON file.
205 '''
206 raise NotImplementedError()
207
208 def WriteComment(self, comment):
209 '''Appends the comment to the internal buffer.
210
211 comment: The comment to be added.
212 '''
213 raise NotImplementedError()
214
215 def WriteRecommendedPolicy(self, policy):
216 '''Appends the template text corresponding to a recommended policy into the
217 internal buffer.
218
219 Args:
220 policy: The recommended policy as it is found in the JSON file.
221 '''
222 # TODO
223 #raise NotImplementedError()
224 pass
225
226 def BeginPolicyGroup(self, group):
227 '''Appends the template text corresponding to the beginning of a
228 policy group into the internal buffer.
229
230 Args:
231 group: The policy group as it is found in the JSON file.
232 '''
233 pass
234
235 def EndPolicyGroup(self):
236 '''Appends the template text corresponding to the end of a
237 policy group into the internal buffer.
238 '''
239 pass
240
241 def BeginRecommendedPolicyGroup(self, group):
242 '''Appends the template text corresponding to the beginning of a recommended
243 policy group into the internal buffer.
244
245 Args:
246 group: The recommended policy group as it is found in the JSON file.
247 '''
248 pass
249
250 def EndRecommendedPolicyGroup(self):
251 '''Appends the template text corresponding to the end of a recommended
252 policy group into the internal buffer.
253 '''
254 pass
255
256 def BeginTemplate(self):
257 '''Appends the text corresponding to the beginning of the whole
258 template into the internal buffer.
259 '''
260 raise NotImplementedError()
261
262 def EndTemplate(self):
263 '''Appends the text corresponding to the end of the whole
264 template into the internal buffer.
265 '''
266 pass
267
268 def GetTemplateText(self):
269 '''Gets the content of the internal template buffer.
270
271 Returns:
272 The generated template from the the internal buffer as a string.
273 '''
274 raise NotImplementedError()
275
276 def SortPoliciesGroupsFirst(self, policy_list):
277 '''Sorts a list of policies alphabetically. The order is the
278 following: first groups alphabetically by caption, then other policies
279 alphabetically by name. The order of policies inside groups is unchanged.
280
281 Args:
282 policy_list: The list of policies to sort. Sub-lists in groups will not
283 be sorted.
284 '''
285 policy_list.sort(key=self.GetPolicySortingKeyGroupsFirst)
286 return policy_list
287
288 def FlattenGroupsAndSortPolicies(self, policy_list, sorting_key=None):
289 '''Sorts a list of policies according to |sorting_key|, defaulting
290 to alphabetical sorting if no key is given. If |policy_list| contains
291 policies with type="group", it is flattened first, i.e. any groups' contents
292 are inserted into the list as first-class elements and the groups are then
293 removed.
294 '''
295 new_list = []
296 for policy in policy_list:
297 if policy['type'] == 'group':
298 for grouped_policy in policy['policies']:
299 new_list.append(grouped_policy)
300 else:
301 new_list.append(policy)
302 if sorting_key == None:
303 sorting_key = self.GetPolicySortingKeyName
304 new_list.sort(key=sorting_key)
305 return new_list
306
307 def GetPolicySortingKeyName(self, policy):
308 return policy['name']
309
310 def GetPolicySortingKeyGroupsFirst(self, policy):
311 '''Extracts a sorting key from a policy. These keys can be used for
312 list.sort() methods to sort policies.
313 See TemplateWriter.SortPolicies for usage.
314 '''
315 is_group = policy['type'] == 'group'
316 if is_group:
317 # Groups are sorted by caption.
318 str_key = policy['caption']
319 else:
320 # Regular policies are sorted by name.
321 str_key = policy['name']
322 # Groups come before regular policies.
323 return (not is_group, str_key)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698