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.
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
9 name - a string, the name of the feature
10 platform - a list containing 'apps' or 'extensions', both, or neither.
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.
16 from copy import deepcopy
18 def _GetPlatformsForExtensionTypes(extension_types):
20 if extension_types == 'all' or 'platform_app' in extension_types:
21 platforms.append('apps')
22 if extension_types == 'all' or 'extension' in extension_types:
23 platforms.append('extensions')
26 def Parse(features_json):
27 '''Process JSON from a _features.json file, standardizing it into a dictionary
32 def ignore_feature(name, value):
33 '''Returns true if this feature should be ignored. This is defined by the
34 presence of a 'whitelist' property for non-private APIs. Private APIs
35 shouldn't have whitelisted features ignored since they're inherently
36 private. Logic elsewhere makes sure not to list private APIs.
38 return 'whitelist' in value and not name.endswith('Private')
40 for name, value in deepcopy(features_json).iteritems():
41 # Some feature names correspond to a list, typically because they're
42 # whitelisted in stable for certain extensions and available in dev for
43 # everybody else. Force a list down to a single feature by attempting to
44 # remove the entries that don't affect the typical usage of an API.
45 if isinstance(value, list):
46 available_values = [subvalue for subvalue in value
47 if not ignore_feature(name, subvalue)]
48 value = available_values[0] if available_values else value[0]
50 if ignore_feature(name, value):
53 features[name] = { 'platforms': [] }
55 extension_types = value.pop('extension_types', None)
56 if extension_types is not None:
57 features[name]['platforms'] = _GetPlatformsForExtensionTypes(
60 features[name]['name'] = name
61 features[name].update(value)
65 def Filtered(features, platform=None):
66 '''Create a new Features dictionary from |features| that contains only items
67 relevant to |platform|. Items retained are deepcopied. Returns new features
70 filtered_features = {}
72 for name, feature in features.iteritems():
73 if not platform or platform in feature['platforms']:
74 filtered_features[name] = deepcopy(feature)
76 return filtered_features
78 def MergedWith(features, other):
79 '''Merge |features| with an additional dictionary to create a new features
80 dictionary. If a feature is common to both |features| and |other|, then it is
81 merged using the standard dictionary update instead of being overwritten.
82 Returns the new Features dictionary.
84 for key, value in other.iteritems():
86 features[key].update(value)
90 # Ensure the Feature schema is enforced for all added items.
91 if not 'name' in features[key]:
92 features[key]['name'] = key
93 if not 'platforms' in features[key]:
94 features[key]['platforms'] = []