- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / common / extensions / docs / server2 / features_utility.py
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 '''
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
8 keys:
9   name - a string, the name of the feature
10   platform - a list containing 'apps' or 'extensions', both, or neither.
11
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.
14 '''
15
16 from copy import deepcopy
17
18 def _GetPlatformsForExtensionTypes(extension_types):
19   platforms = []
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')
24   return platforms
25
26 def Parse(features_json):
27   '''Process JSON from a _features.json file, standardizing it into a dictionary
28   of Features.
29   '''
30   features = {}
31
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.
37     '''
38     return 'whitelist' in value and not name.endswith('Private')
39
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]
49
50     if ignore_feature(name, value):
51       continue
52
53     features[name] = { 'platforms': [] }
54
55     extension_types = value.pop('extension_types', None)
56     if extension_types is not None:
57       features[name]['platforms'] = _GetPlatformsForExtensionTypes(
58           extension_types)
59
60     features[name]['name'] = name
61     features[name].update(value)
62
63   return features
64
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
68   dictionary.
69   '''
70   filtered_features = {}
71
72   for name, feature in features.iteritems():
73     if not platform or platform in feature['platforms']:
74       filtered_features[name] = deepcopy(feature)
75
76   return filtered_features
77
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.
83   '''
84   for key, value in other.iteritems():
85     if key in features:
86       features[key].update(value)
87     else:
88       features[key] = value
89
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'] = []
95
96   return features