Chromium Code Reviews| Index: chrome/common/extensions/docs/server2/manifest_features.py |
| diff --git a/chrome/common/extensions/docs/server2/manifest_features.py b/chrome/common/extensions/docs/server2/manifest_features.py |
| index e535592482fec1c804afd4ac82951356528114a5..8a487c6348a410f1f60ea4cf28f76cfa59ef9f1f 100644 |
| --- a/chrome/common/extensions/docs/server2/manifest_features.py |
| +++ b/chrome/common/extensions/docs/server2/manifest_features.py |
| @@ -12,7 +12,11 @@ indicating how an app or extension should define a manifest property. If 'level' |
| is missing, 'optional' is assumed. |
| ''' |
| +import json |
| + |
| +from features import Features |
| import features_utility |
| +from third_party.json_schema_compiler.json_parse import Parse |
| def CreateManifestFeatures(features_json, manifest_json, filter_platform=None): |
| '''Create a manifest features dictionary by normalizing |features_json| and |
| @@ -60,3 +64,116 @@ def ConvertDottedKeysToNested(features): |
| insert_children(features) |
| return features |
| + |
| +def _ListifyAndSortDocs(features, app_name): |
|
not at google - send to devlin
2013/09/23 18:31:30
most of this code is specifically for passing to t
|
| + '''Convert a |features| dictionary, and all 'children' dictionaries, into |
| + lists recursively. Sort lists 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 coerce_example_to_feature(feature): |
| + '''To display json in examples more clearly, convert the example of |
| + |feature| into the feature format, with a name and children, to be rendered |
| + by the templates. Only applicable to examples that are dictionaries. |
| + ''' |
| + if not isinstance(feature.get('example'), dict): |
| + if 'example' in feature: |
| + feature['example'] = json.dumps(feature['example']) |
| + return |
| + # Add any keys/value pairs in the dict as children |
| + for key, value in feature['example'].iteritems(): |
| + if not 'children' in feature: |
| + feature['children'] = {} |
| + feature['children'][key] = { 'name': key, 'example': value } |
| + del feature['example'] |
| + del feature['has_example'] |
| + |
| + def convert_and_sort(features): |
| + for key, value in features.items(): |
| + if 'example' in value: |
| + value['has_example'] = True |
| + example = json.dumps(value['example']) |
| + if example == '{}': |
| + value['example'] = '{...}' |
| + elif example == '[]': |
| + value['example'] = '[...]' |
| + else: |
| + coerce_example_to_feature(value) |
| + if 'children' in value: |
| + features[key]['children'] = convert_and_sort(value['children']) |
| + return sorted(features.values(), key=sort_key) |
| + |
| + # Replace {{title}} in the 'name' manifest property example with |app_name| |
| + if 'name' in features: |
| + name = features['name'] |
| + name['example'] = name['example'].replace('{{title}}', app_name) |
| + |
| + features = convert_and_sort(features) |
| + |
| + return features |
| + |
| +def _AddLevelAnnotations(features): |
| + '''Add level annotations to |features|. |features| and children lists must be |
| + sorted by 'level'. Annotations are added to the first item in a group of |
| + features of the same 'level'. |
| + |
| + The last item in a list has 'is_last' set to True. |
| + ''' |
| + 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, features): |
| + current_level = parent_level |
| + for item in features: |
| + level = item.get('level', 'optional') |
| + if level != current_level: |
| + add_annotation(item, annotations[level]) |
| + current_level = level |
| + if 'children' in item: |
| + annotate(level, item['children']) |
| + if features: |
| + features[-1]['is_last'] = True |
| + |
| + annotate('required', features) |
| + return features |
| + |
| +class ManifestFeatures(Features): |
| + '''Subclass of Features to provide special annotation and ordering as well as |
| + the merging of _manifest_features.json data with manifest.json. |
| + ''' |
| + def __init__(self, |
| + compiled_fs_factory, |
| + manifest_features_path, |
| + file_system, |
| + manifest_json_path): |
| + self._file_system = file_system |
| + self._manifest_json_path = manifest_json_path |
| + super(ManifestFeatures, self).__init__(compiled_fs_factory, |
| + manifest_features_path) |
| + |
| + def Parse(self, features_json): |
| + manifest_json = Parse( |
| + self._file_system.ReadSingle(self._manifest_json_path)) |
| + return CreateManifestFeatures(features_json, manifest_json) |
| + |
| + def FilterByPlatform(self, features, platform): |
| + features = super(ManifestFeatures, self).FilterByPlatform(features, |
| + platform) |
| + return _AddLevelAnnotations( |
| + _ListifyAndSortDocs(ConvertDottedKeysToNested(features), |
| + app_name=platform.capitalize())) |