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