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 collections import OrderedDict | |
6 from copy import deepcopy | |
7 from operator import itemgetter | |
8 | |
9 from third_party.json_schema_compiler.json_parse import Parse | |
10 | |
11 def create_dict(): | |
not at google - send to devlin
2013/05/11 22:19:04
if a function is only relevant to a couple of loca
jshumway
2013/05/11 22:50:42
Done.
| |
12 d = OrderedDict() | |
13 for category in ['required', 'only_one', 'recommended', 'optional']: | |
14 d[category] = [] | |
15 return d | |
16 | |
17 class ManifestDataSource(object): | |
18 """ Provides a template with access to manifest properties specific to apps or | |
19 extensions. | |
20 """ | |
21 def __init__(self, compiled_fs_factory, file_system, manifest_path, | |
22 features_path, channel): | |
not at google - send to devlin
2013/05/11 22:19:04
channel not needed anymore
also if things don't f
jshumway
2013/05/11 22:50:42
Done.
| |
23 self._manifest_path = manifest_path | |
24 self._features_path = features_path | |
25 self._file_system = file_system | |
26 self._cache = compiled_fs_factory.Create( | |
27 self._CreateManifestData, ManifestDataSource) | |
28 self._channel = channel | |
29 | |
30 def _ApplyAppsTransformations(self, manifest): | |
31 manifest['required'][0]['example'] = 'Application' | |
32 manifest['optional'][-1]['is_last'] = True | |
33 | |
34 def _ApplyExtensionsTransformations(self, manifest): | |
35 manifest['optional'][-1]['is_last'] = True | |
36 | |
37 def _CreateManifestData(self, _, content): | |
38 """Take the contents of |_manifest_path| and create apps and extensions | |
39 versions of a manifest example based on the contents of |_features_path|. | |
40 """ | |
41 manifest_json = Parse(content) | |
42 features_json = Parse(self._file_system.Read( | |
not at google - send to devlin
2013/05/11 22:19:04
ReadSingle
jshumway
2013/05/11 22:50:42
Done.
| |
43 [self._features_path]).Get()[self._features_path]) | |
44 apps = create_dict() | |
45 extensions = create_dict() | |
46 | |
47 def add_property(feature, manifest_key, category): | |
48 """If |feature|, from features_json, has the correct extension_types, add | |
49 |manifest_key| to either apps or extensions. | |
50 """ | |
51 added = False | |
52 extension_types = feature['extension_types'] | |
53 if extension_types == 'all' or 'platform_app' in extension_types: | |
54 apps[category].append(deepcopy(manifest_key)) | |
55 added = True | |
56 if extension_types == 'all' or 'extension' in extension_types: | |
57 extensions[category].append(deepcopy(manifest_key)) | |
58 added = True | |
59 return added | |
60 | |
61 # Property types are: required, only_one, recommended, and optional. | |
62 for category in manifest_json: | |
63 for manifest_key in manifest_json[category]: | |
64 # If a property is in manifest.json but not _manifest_features, this | |
65 # will cause an error. | |
66 feature = features_json[manifest_key['name']] | |
67 if add_property(feature, manifest_key, category): | |
68 del features_json[manifest_key['name']] | |
69 | |
70 # All of the properties left in features_json are assumed to be optional. | |
71 for feature in features_json.keys(): | |
72 item = features_json[feature] | |
73 # Handles instances where a features entry is a union with a whitelist. | |
74 if isinstance(item, list): | |
75 item = item[0] | |
76 add_property(item, {'name': feature}, 'optional') | |
77 | |
78 apps['optional'].sort(key=itemgetter('name')) | |
79 extensions['optional'].sort(key=itemgetter('name')) | |
80 | |
81 self._ApplyAppsTransformations(apps) | |
82 self._ApplyExtensionsTransformations(extensions) | |
83 | |
84 return {'apps': apps, 'extensions': extensions} | |
85 | |
86 def get(self, key): | |
87 return self._cache.GetFromFile(self._manifest_path)[key] | |
OLD | NEW |