OLD | NEW |
| (Empty) |
1 # Copyright 2013 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 from itertools import ifilter | |
6 from operator import itemgetter | |
7 | |
8 import features_utility as features | |
9 from third_party.json_schema_compiler.json_parse import Parse | |
10 | |
11 def _ListifyPermissions(permissions): | |
12 '''Filter out any permissions that do not have a description or with a name | |
13 that ends with Private then sort permissions features by name into a list. | |
14 ''' | |
15 def filter_permissions(perm): | |
16 return 'description' in perm and not perm['name'].endswith('Private') | |
17 | |
18 return sorted( | |
19 ifilter(filter_permissions, permissions.values()), | |
20 key=itemgetter('name')) | |
21 | |
22 def _AddDependencyDescriptions(permissions, api_features): | |
23 '''Use |api_features| to determine the dependencies APIs have on permissions. | |
24 Add descriptions to |permissions| based on those dependencies. | |
25 ''' | |
26 for name, permission in permissions.iteritems(): | |
27 # Don't overwrite the description created by expanding a partial template. | |
28 if 'partial' in permission or not permission['platforms']: | |
29 continue | |
30 | |
31 has_deps = False | |
32 if name in api_features: | |
33 for dependency in api_features[name].get('dependencies', ()): | |
34 if dependency.startswith('permission:'): | |
35 has_deps = True | |
36 | |
37 if has_deps: | |
38 permission['partial'] = 'permissions/generic_description' | |
39 | |
40 class PermissionsDataSource(object): | |
41 '''Load and format permissions features to be used by templates. Requries a | |
42 template data source be set before use. | |
43 ''' | |
44 def __init__(self, | |
45 compiled_fs_factory, | |
46 file_system, | |
47 api_features_path, | |
48 permissions_features_path, | |
49 permissions_json_path): | |
50 self._api_features_path = api_features_path | |
51 self._permissions_features_path = permissions_features_path | |
52 self._permissions_json_path = permissions_json_path | |
53 self._file_system = file_system | |
54 self._cache = compiled_fs_factory.Create( | |
55 self._CreatePermissionsDataSource, PermissionsDataSource) | |
56 | |
57 def SetTemplateDataSource(self, template_data_source_factory): | |
58 '''Initialize a template data source to be used to render partial templates | |
59 into descriptions for permissions. Must be called before .get | |
60 ''' | |
61 self._template_data_source = template_data_source_factory.Create( | |
62 None, {}) | |
63 | |
64 def _CreatePermissionsDataSource(self, _, content): | |
65 '''Combine the contents of |_permissions_json_path| and | |
66 |_permissions_features_path|. Filter into lists, one for extensions and | |
67 one for apps. | |
68 ''' | |
69 api_features = Parse(self._file_system.ReadSingle(self._api_features_path)) | |
70 | |
71 def filter_for_platform(permissions, platform): | |
72 return _ListifyPermissions(features.Filtered(permissions, platform)) | |
73 | |
74 permissions_json = Parse(self._file_system.ReadSingle( | |
75 self._permissions_json_path)) | |
76 permission_features = features.MergedWith( | |
77 features.Parse(Parse(content)), permissions_json) | |
78 | |
79 _AddDependencyDescriptions(permission_features, api_features) | |
80 # Turn partial templates into descriptions, ensure anchors are set. | |
81 for permission in permission_features.values(): | |
82 if not 'anchor' in permission: | |
83 permission['anchor'] = permission['name'] | |
84 if 'partial' in permission: | |
85 permission['description'] = self._template_data_source.get( | |
86 permission['partial']) | |
87 del permission['partial'] | |
88 | |
89 return { | |
90 'declare_apps': filter_for_platform(permission_features, 'app'), | |
91 'declare_extensions': filter_for_platform( | |
92 permission_features, 'extension') | |
93 } | |
94 | |
95 def get(self, key): | |
96 return self._cache.GetFromFile(self._permissions_features_path)[key] | |
OLD | NEW |