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