Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(18)

Side by Side Diff: chrome/common/extensions/docs/server2/features_utility.py

Issue 23867003: Docserver: Consolidate features caching and access. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: no version bump Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 # Copyright 2013 The Chromium Authors. All rights reserved. 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 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 ''' 5 '''
6 Utility functions for working with the Feature abstraction. Features are grouped 6 Utility functions for working with the Feature abstraction. Features are grouped
7 into a dictionary by name. Each Feature is guaranteed to have the following two 7 into a dictionary by name. Each Feature is guaranteed to have the following two
8 keys: 8 keys:
9 name - a string, the name of the feature 9 name - a string, the name of the feature
10 platform - a list containing 'app' or 'extension', both, or neither. 10 platform - a list containing 'apps' or 'extensions', both, or neither.
11 11
12 A Feature may have other keys from a _features.json file as well. Features with 12 A Feature may have other keys from a _features.json file as well. Features with
13 a whitelist are ignored as they are only useful to specific apps or extensions. 13 a whitelist are ignored as they are only useful to specific apps or extensions.
14 ''' 14 '''
15 15
16 from copy import deepcopy 16 from copy import deepcopy
17 17
18 def Parse(features_json): 18 def Parse(features_json):
19 '''Process JSON from a _features.json file, standardizing it into a dictionary 19 '''Process JSON from a _features.json file, standardizing it into a dictionary
20 of Features. 20 of Features.
21 ''' 21 '''
22 features = {} 22 features = {}
23 23
24 for name, value in deepcopy(features_json).iteritems(): 24 for name, value in deepcopy(features_json).iteritems():
25 # Some feature names corrispond to a list; force a list down to a single 25 # Some feature names correspond to a list; force a list down to a single
26 # feature by removing entries that have a 'whitelist'. 26 # feature by removing entries that have a 'whitelist'.
27 if isinstance(value, list): 27 if isinstance(value, list):
28 values = [subvalue for subvalue in value if not 'whitelist' in subvalue] 28 values = [subvalue for subvalue in value if not 'whitelist' in subvalue]
29 if values: 29 if values:
30 value = values[0] 30 value = values[0]
31 else: 31 else:
32 continue 32 continue
33 33
34 if 'whitelist' in value: 34 if 'whitelist' in value:
35 continue 35 continue
36 36
37 features[name] = { 'platforms': [] } 37 features[name] = { 'platforms': [] }
38 38
39 platforms = value.pop('extension_types') 39 extension_types = value.pop('extension_types', None)
40 if platforms == 'all' or 'platform_app' in platforms: 40 if extension_types is not None:
41 features[name]['platforms'].append('app') 41 features[name]['platforms'] = GetPlatformsForExtensionTypes(
42 if platforms == 'all' or 'extension' in platforms: 42 extension_types)
43 features[name]['platforms'].append('extension')
44 43
45 features[name]['name'] = name 44 features[name]['name'] = name
46 features[name].update(value) 45 features[name].update(value)
47 46
48 return features 47 return features
49 48
50 def Filtered(features, platform=None): 49 def Filtered(features, platform=None):
51 '''Create a new Features dictionary from |features| that contains only items 50 '''Create a new Features dictionary from |features| that contains only items
52 relevant to |platform|. Items retained are deepcopied. Returns new features 51 relevant to |platform|. Items retained are deepcopied. Returns new features
53 dictionary. 52 dictionary.
(...skipping 18 matching lines...) Expand all
72 else: 71 else:
73 features[key] = value 72 features[key] = value
74 73
75 # Ensure the Feature schema is enforced for all added items. 74 # Ensure the Feature schema is enforced for all added items.
76 if not 'name' in features[key]: 75 if not 'name' in features[key]:
77 features[key]['name'] = key 76 features[key]['name'] = key
78 if not 'platforms' in features[key]: 77 if not 'platforms' in features[key]:
79 features[key]['platforms'] = [] 78 features[key]['platforms'] = []
80 79
81 return features 80 return features
81
82 def GetPlatformsForExtensionTypes(extension_types):
not at google - send to devlin 2013/10/01 16:34:34 this can be private?
Ken Rockot(use gerrit already) 2013/10/01 22:18:16 Done.
83 platforms = []
84 if extension_types == 'all' or 'platform_app' in extension_types:
85 platforms.append('apps')
86 if extension_types == 'all' or 'extension' in extension_types:
87 platforms.append('extensions')
88 return platforms
89
90 def AddPlatformsFromDependencies(feature, features_bundle):
not at google - send to devlin 2013/10/01 16:34:34 as I mentioned earlier - you can put this in featu
Ken Rockot(use gerrit already) 2013/10/01 22:18:16 Done.
91 features_map = {
92 'api': features_bundle.GetAPIFeatures(),
93 'manifest': features_bundle.GetManifestFeatures(),
94 'permission': features_bundle.GetPermissionFeatures()
95 }
96 dependencies = feature.get('dependencies')
97 if dependencies is None:
98 return ['apps', 'extensions']
99 platforms = set()
100 for dependency in dependencies:
101 dep_type, dep_name = dependency.split(':')
102 dependency_features = features_map[dep_type]
103 dependency_feature = dependency_features.get(dep_name)
104 # If the dependency can't be resolved, it is inaccessible and therefore
105 # so is this feature.
106 if dependency_feature is None:
107 return []
108 platforms = platforms.union(dependency_feature['platforms'])
109 feature['platforms'] = list(platforms)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698