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

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

Issue 1438403002: Remove contents of grit's git-svn-mirror repository. (Closed) Base URL: https://chromium.googlesource.com/external/grit-i18n@master
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
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):
95 '''Checks if |policy| is supported on |platform|.
96
97 Args:
98 policy: The dictionary of the policy.
99 platform: The platform to check; one of 'win', 'mac', 'linux' or
100 'chrome_os'.
101 '''
102 is_supported = lambda x: platform in x['platforms']
103 return any(filter(is_supported, policy['supported_on']))
104
105 def _GetChromiumVersionString(self):
106 '''Returns the Chromium version string stored in the environment variable
107 version (if it is set).
108
109 Returns: The Chromium version string or None if it has not been set.'''
110
111 if 'version' in self.config:
112 return self.config['version']
113
114 def _GetPoliciesForWriter(self, group):
115 '''Filters the list of policies in the passed group that are supported by
116 the writer.
117
118 Args:
119 group: The dictionary of the policy group.
120
121 Returns: The list of policies of the policy group that are compatible
122 with the writer.
123 '''
124 if not 'policies' in group:
125 return []
126 result = []
127 for policy in group['policies']:
128 if self.IsPolicySupported(policy):
129 result.append(policy)
130 return result
131
132 def Init(self):
133 '''Initializes the writer. If the WriteTemplate method is overridden, then
134 this method must be called as first step of each template generation
135 process.
136 '''
137 pass
138
139 def WriteTemplate(self, template):
140 '''Writes the given template definition.
141
142 Args:
143 template: Template definition to write.
144
145 Returns:
146 Generated output for the passed template definition.
147 '''
148 self.messages = template['messages']
149 self.Init()
150 template['policy_definitions'] = \
151 self.PreprocessPolicies(template['policy_definitions'])
152 self.BeginTemplate()
153 for policy in template['policy_definitions']:
154 if policy['type'] == 'group':
155 child_policies = self._GetPoliciesForWriter(policy)
156 child_recommended_policies = filter(self.CanBeRecommended,
157 child_policies)
158 if child_policies:
159 # Only write nonempty groups.
160 self.BeginPolicyGroup(policy)
161 for child_policy in child_policies:
162 # Nesting of groups is currently not supported.
163 self.WritePolicy(child_policy)
164 self.EndPolicyGroup()
165 if child_recommended_policies:
166 self.BeginRecommendedPolicyGroup(policy)
167 for child_policy in child_recommended_policies:
168 self.WriteRecommendedPolicy(child_policy)
169 self.EndRecommendedPolicyGroup()
170 elif self.IsPolicySupported(policy):
171 self.WritePolicy(policy)
172 if self.CanBeRecommended(policy):
173 self.WriteRecommendedPolicy(policy)
174 self.EndTemplate()
175
176 return self.GetTemplateText()
177
178 def PreprocessPolicies(self, policy_list):
179 '''Preprocesses a list of policies according to a given writer's needs.
180 Preprocessing steps include sorting policies and stripping unneeded
181 information such as groups (for writers that ignore them).
182 Subclasses are encouraged to override this method, overriding
183 implementations may call one of the provided specialized implementations.
184 The default behaviour is to use SortPoliciesGroupsFirst().
185
186 Args:
187 policy_list: A list containing the policies to sort.
188
189 Returns:
190 The sorted policy list.
191 '''
192 return self.SortPoliciesGroupsFirst(policy_list)
193
194 def WritePolicy(self, policy):
195 '''Appends the template text corresponding to a policy into the
196 internal buffer.
197
198 Args:
199 policy: The policy as it is found in the JSON file.
200 '''
201 raise NotImplementedError()
202
203 def WriteComment(self, comment):
204 '''Appends the comment to the internal buffer.
205
206 comment: The comment to be added.
207 '''
208 raise NotImplementedError()
209
210 def WriteRecommendedPolicy(self, policy):
211 '''Appends the template text corresponding to a recommended policy into the
212 internal buffer.
213
214 Args:
215 policy: The recommended policy as it is found in the JSON file.
216 '''
217 # TODO
218 #raise NotImplementedError()
219 pass
220
221 def BeginPolicyGroup(self, group):
222 '''Appends the template text corresponding to the beginning of a
223 policy group into the internal buffer.
224
225 Args:
226 group: The policy group as it is found in the JSON file.
227 '''
228 pass
229
230 def EndPolicyGroup(self):
231 '''Appends the template text corresponding to the end of a
232 policy group into the internal buffer.
233 '''
234 pass
235
236 def BeginRecommendedPolicyGroup(self, group):
237 '''Appends the template text corresponding to the beginning of a recommended
238 policy group into the internal buffer.
239
240 Args:
241 group: The recommended policy group as it is found in the JSON file.
242 '''
243 pass
244
245 def EndRecommendedPolicyGroup(self):
246 '''Appends the template text corresponding to the end of a recommended
247 policy group into the internal buffer.
248 '''
249 pass
250
251 def BeginTemplate(self):
252 '''Appends the text corresponding to the beginning of the whole
253 template into the internal buffer.
254 '''
255 raise NotImplementedError()
256
257 def EndTemplate(self):
258 '''Appends the text corresponding to the end of the whole
259 template into the internal buffer.
260 '''
261 pass
262
263 def GetTemplateText(self):
264 '''Gets the content of the internal template buffer.
265
266 Returns:
267 The generated template from the the internal buffer as a string.
268 '''
269 raise NotImplementedError()
270
271 def SortPoliciesGroupsFirst(self, policy_list):
272 '''Sorts a list of policies alphabetically. The order is the
273 following: first groups alphabetically by caption, then other policies
274 alphabetically by name. The order of policies inside groups is unchanged.
275
276 Args:
277 policy_list: The list of policies to sort. Sub-lists in groups will not
278 be sorted.
279 '''
280 policy_list.sort(key=self.GetPolicySortingKeyGroupsFirst)
281 return policy_list
282
283 def FlattenGroupsAndSortPolicies(self, policy_list, sorting_key=None):
284 '''Sorts a list of policies according to |sorting_key|, defaulting
285 to alphabetical sorting if no key is given. If |policy_list| contains
286 policies with type="group", it is flattened first, i.e. any groups' contents
287 are inserted into the list as first-class elements and the groups are then
288 removed.
289 '''
290 new_list = []
291 for policy in policy_list:
292 if policy['type'] == 'group':
293 for grouped_policy in policy['policies']:
294 new_list.append(grouped_policy)
295 else:
296 new_list.append(policy)
297 if sorting_key == None:
298 sorting_key = self.GetPolicySortingKeyName
299 new_list.sort(key=sorting_key)
300 return new_list
301
302 def GetPolicySortingKeyName(self, policy):
303 return policy['name']
304
305 def GetPolicySortingKeyGroupsFirst(self, policy):
306 '''Extracts a sorting key from a policy. These keys can be used for
307 list.sort() methods to sort policies.
308 See TemplateWriter.SortPolicies for usage.
309 '''
310 is_group = policy['type'] == 'group'
311 if is_group:
312 # Groups are sorted by caption.
313 str_key = policy['caption']
314 else:
315 # Regular policies are sorted by name.
316 str_key = policy['name']
317 # Groups come before regular policies.
318 return (not is_group, str_key)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698