Chromium Code Reviews| Index: chrome/common/extensions/docs/server2/manifest_data_source.py |
| diff --git a/chrome/common/extensions/docs/server2/manifest_data_source.py b/chrome/common/extensions/docs/server2/manifest_data_source.py |
| index b01d22655074aafbdc6e242ae61b48fef267d5ce..ae21b230452f455faa78e419064463789c9ddf33 100644 |
| --- a/chrome/common/extensions/docs/server2/manifest_data_source.py |
| +++ b/chrome/common/extensions/docs/server2/manifest_data_source.py |
| @@ -2,12 +2,79 @@ |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| -from collections import OrderedDict |
| from copy import deepcopy |
| -from operator import itemgetter |
| +import json |
| +from features_utility import FeatureFile |
| from third_party.json_schema_compiler.json_parse import Parse |
| +def _ConvertAndSortDocs(properties, platform): |
| + '''Convert docs and subdocs into lists and sort them, first by level, then |
| + by name. |
| + ''' |
| + def sort_key(item): |
| + '''Key function to sort items primarily by level (according to index into |
| + levels) then subsort by name. |
| + ''' |
| + levels = ('required', 'recommended', 'only_one', 'optional') |
| + |
| + return (levels.index(item.get('level', 'optional')), item['name']) |
| + |
| + def convert_and_sort(properties): |
| + for key in properties.keys(): |
| + value = properties[key] |
|
not at google - send to devlin
2013/07/29 23:30:09
for key, value in properties.items() ?
jshumway
2013/07/30 23:15:50
Done.
|
| + if 'example' in value: |
| + value['has_example'] = True |
| + example = json.dumps(value['example']) |
| + if example == '{}': |
| + value['example'] = '{...}' |
| + elif example == '[]': |
| + value['example'] = '[...]' |
| + else: |
| + value['example'] = example |
| + |
| + if 'subdocs' in value: |
| + properties[key]['subdocs'] = convert_and_sort(value['subdocs']) |
| + |
| + return sorted(properties.values(), key=sort_key) |
| + |
| + name = properties['name'] |
| + name['example'] = name['example'].replace('{{title}}', platform.capitalize()) |
| + |
| + return convert_and_sort(properties) |
| + |
| +def _Annotate(properties): |
| + '''Add level annotations to level groups in a manifest property list and |
| + subdoc lists. Requeries a list of properties sorted by level, with subdocs |
| + in sorted lists as well. |
| + ''' |
| + annotations = { |
| + 'required': 'Required', |
| + 'recommended': 'Recommended', |
| + 'only_one': 'Pick one (or none)', |
| + 'optional': 'Optional' |
| + } |
| + |
| + def add_annotation(item, annotation): |
| + if not 'annotations' in item: |
| + item['annotations'] = [] |
| + item['annotations'].insert(0, annotation) |
| + |
| + def annotate(parent_level, properties): |
| + current_level = parent_level |
| + for item in properties: |
| + level = item.get('level', 'optional') |
| + if level != current_level: |
| + add_annotation(item, annotations[level]) |
| + current_level = level |
| + if 'subdocs' in item: |
| + annotate(level, item['subdocs']) |
| + if properties: |
| + properties[-1]['is_last'] = True |
| + |
| + annotate('required', properties) |
| + return properties |
| + |
| class ManifestDataSource(object): |
| '''Provides a template with access to manifest properties specific to apps or |
| extensions. |
| @@ -23,68 +90,28 @@ class ManifestDataSource(object): |
| self._cache = compiled_fs_factory.Create( |
| self._CreateManifestData, ManifestDataSource) |
| - def _ApplyAppsTransformations(self, manifest): |
| - manifest['required'][0]['example'] = 'Application' |
| - manifest['optional'][-1]['is_last'] = True |
| - |
| - def _ApplyExtensionsTransformations(self, manifest): |
| - manifest['optional'][-1]['is_last'] = True |
| - |
| def _CreateManifestData(self, _, content): |
| - '''Take the contents of |_manifest_path| and create apps and extensions |
| - versions of a manifest example based on the contents of |_features_path|. |
| + '''Combine the contents of |_manifest_path| and |_features_path| into a |
| + master dictionary then create filtered lists to be used by templates. |
| ''' |
| - def create_manifest_dict(): |
| - manifest_dict = OrderedDict() |
| - for category in ('required', 'only_one', 'recommended', 'optional'): |
| - manifest_dict[category] = [] |
| - return manifest_dict |
| + manifest_json = Parse(content) |
| + manifest_features = FeatureFile( |
| + Parse(self._file_system.ReadSingle(self._features_path))) |
| + manifest_features.MergeWith(manifest_json) |
| - apps = create_manifest_dict() |
| - extensions = create_manifest_dict() |
| + apps_features = deepcopy(manifest_features).Filter('app').InsertSubDocs() |
| + apps_features = _Annotate(_ConvertAndSortDocs(apps_features, 'app')) |
| - manifest_json = Parse(content) |
| - features_json = Parse(self._file_system.ReadSingle( |
| - self._features_path)) |
| - |
| - def add_property(feature, manifest_key, category): |
| - '''If |feature|, from features_json, has the correct extension_types, add |
| - |manifest_key| to either apps or extensions. |
| - ''' |
| - added = False |
| - extension_types = feature['extension_types'] |
| - if extension_types == 'all' or 'platform_app' in extension_types: |
| - apps[category].append(deepcopy(manifest_key)) |
| - added = True |
| - if extension_types == 'all' or 'extension' in extension_types: |
| - extensions[category].append(deepcopy(manifest_key)) |
| - added = True |
| - return added |
| - |
| - # Property types are: required, only_one, recommended, and optional. |
| - for category in manifest_json: |
| - for manifest_key in manifest_json[category]: |
| - # If a property is in manifest.json but not _manifest_features, this |
| - # will cause an error. |
| - feature = features_json[manifest_key['name']] |
| - if add_property(feature, manifest_key, category): |
| - del features_json[manifest_key['name']] |
| - |
| - # All of the properties left in features_json are assumed to be optional. |
| - for feature in features_json.keys(): |
| - item = features_json[feature] |
| - # Handles instances where a features entry is a union with a whitelist. |
| - if isinstance(item, list): |
| - item = item[0] |
| - add_property(item, {'name': feature}, 'optional') |
| - |
| - apps['optional'].sort(key=itemgetter('name')) |
| - extensions['optional'].sort(key=itemgetter('name')) |
| - |
| - self._ApplyAppsTransformations(apps) |
| - self._ApplyExtensionsTransformations(extensions) |
| - |
| - return {'apps': apps, 'extensions': extensions} |
| + extensions_features = ( |
| + deepcopy(manifest_features).Filter('extension').InsertSubDocs()) |
| + extensions_features = _Annotate( |
| + _ConvertAndSortDocs(extensions_features, 'extension')) |
|
not at google - send to devlin
2013/07/29 23:30:09
if we immutify the FeatureModel class the all this
jshumway
2013/07/30 23:15:50
Done.
|
| + |
| + return { |
| + 'apps': apps_features, |
| + 'extensions': extensions_features, |
| + 'all_features': manifest_features |
| + } |
| def get(self, key): |
| return self._cache.GetFromFile(self._manifest_path)[key] |