| 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 _InsertSubDocs(self, manifest_dict): |
| 34 """ Insert all docs in the 'subdoc' category into their 'parent' objcets, |
| 35 determined by the section of the subdocs name before a period. Only nests |
| 36 one level. |
| 37 """ |
| 38 for key in manifest_dict.get('subdoc', []): |
| 39 parent_name, doc_name = key['name'].split('.', 1) |
| 40 key['name'] = doc_name |
| 41 for category in ['required', 'only_one', 'recommended', 'optional']: |
| 42 for entry in manifest_dict[category]: |
| 43 if parent_name == entry['name']: |
| 44 parent = entry |
| 45 |
| 46 assert parent |
| 47 if 'subdoc' in parent: |
| 48 parent['subdoc'].append(key) |
| 49 else: |
| 50 parent['subdoc'] = [key] |
| 51 |
| 52 del manifest_dict['subdoc'] |
| 53 |
| 54 def _CreateManifestData(self, _, content): |
| 55 """ Take the contents of |_manifest_path| and create apps and extensions |
| 56 versions of a manifest example based on the contents of |_features_path|. |
| 57 """ |
| 58 def create_manifest_dict(): |
| 59 d = OrderedDict() |
| 60 for category in ['required', 'only_one', 'recommended', 'optional', 'subdo
c']: |
| 61 d[category] = [] |
| 62 return d |
| 63 |
| 64 apps = create_manifest_dict() |
| 65 extensions = create_manifest_dict() |
| 66 |
| 67 manifest_json = Parse(content) |
| 68 features_json = Parse(self._file_system.ReadSingle( |
| 69 self._features_path)) |
| 70 |
| 71 def add_property(feature, manifest_key, category): |
| 72 """ If |feature|, from features_json, has the correct extension_types, add |
| 73 |manifest_key| to either apps or extensions. |
| 74 """ |
| 75 # Save the subdocs for later by putting them in their own category. |
| 76 category = 'subdoc' if '.' in manifest_key['name'] else category |
| 77 |
| 78 added = False |
| 79 extension_types = feature['extension_types'] |
| 80 if extension_types == 'all' or 'platform_app' in extension_types: |
| 81 apps[category].append(deepcopy(manifest_key)) |
| 82 added = True |
| 83 if extension_types == 'all' or 'extension' in extension_types: |
| 84 extensions[category].append(deepcopy(manifest_key)) |
| 85 added = True |
| 86 return added |
| 87 |
| 88 # Property types are: required, only_one, recommended, and optional. |
| 89 for category in manifest_json: |
| 90 for manifest_key in manifest_json[category]: |
| 91 # If a property is in manifest.json but not _manifest_features, this |
| 92 # will cause an error. |
| 93 feature = features_json[manifest_key['name']] |
| 94 if add_property(feature, manifest_key, category): |
| 95 del features_json[manifest_key['name']] |
| 96 |
| 97 # All of the properties left in features_json are assumed to be optional. |
| 98 for feature in features_json.keys(): |
| 99 item = features_json[feature] |
| 100 # Handles instances where a features entry is a union with a whitelist. |
| 101 if isinstance(item, list): |
| 102 item = item[0] |
| 103 add_property(item, {'name': feature}, 'optional') |
| 104 |
| 105 apps['optional'].sort(key=itemgetter('name')) |
| 106 extensions['optional'].sort(key=itemgetter('name')) |
| 107 |
| 108 self._InsertSubDocs(extensions) |
| 109 self._InsertSubDocs(apps) |
| 110 |
| 111 self._ApplyAppsTransformations(apps) |
| 112 self._ApplyExtensionsTransformations(extensions) |
| 113 |
| 114 return {'apps': apps, 'extensions': extensions} |
| 115 |
| 116 def get(self, key): |
| 117 return self._cache.GetFromFile(self._manifest_path)[key] |
| OLD | NEW |