Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 def FeaturesFromJson(features_json): | |
|
not at google - send to devlin
2013/08/08 16:31:19
let's go with Parse here and Merge below, then cal
jshumway
2013/08/14 23:38:39
Done.
| |
| 6 '''Standardize the raw features file json |features_json| into a more regular | |
| 7 format. Return a dictionary of features, keyed by name. Each feature is | |
| 8 guaranteed to have a 'name' attribute and a 'platforms' attribute that is a | |
| 9 list of platforms the feature is relevant to. Valid platforms are 'app' or | |
| 10 'extension'. Other optional keys may be present in each feature. | |
| 11 | |
| 12 Features with a 'whitelist' are only relevant to apps or extensions on that | |
| 13 whitelist and are filtered out. | |
| 14 ''' | |
| 15 features = {} | |
| 16 | |
| 17 for name, value in features_json.iteritems(): | |
| 18 # Some feature names corrispond to a list; force a list down to a single | |
| 19 # feature by removing entries that have a 'whitelist'. | |
| 20 if isinstance(value, list): | |
| 21 values = [subvalue for subvalue in value if not 'whitelist' in subvalue] | |
| 22 if values: | |
| 23 value = values[0] | |
| 24 else: | |
| 25 continue | |
| 26 | |
| 27 if 'whitelist' in value: | |
| 28 continue | |
| 29 | |
| 30 features[name] = { 'platforms': [] } | |
| 31 | |
| 32 platforms = value.pop('extension_types') | |
| 33 if platforms == 'all' or 'platform_app' in platforms: | |
| 34 features[name]['platforms'].append('app') | |
| 35 if platforms == 'all' or 'extension' in platforms: | |
| 36 features[name]['platforms'].append('extension') | |
| 37 | |
| 38 features[name]['name'] = name | |
| 39 features[name].update(value) | |
| 40 | |
| 41 return features | |
| 42 | |
| 43 def Filter(features, platform=None): | |
| 44 '''Remove items from standardized |features| that are not relevant to | |
| 45 |platform|. Returns the mutated features. | |
| 46 ''' | |
|
not at google - send to devlin
2013/08/08 16:31:19
How about this idiom: python has "sort" for in-pla
jshumway
2013/08/14 23:38:39
Done.
| |
| 47 for name, feature in features.items(): | |
| 48 if platform and not platform in feature['platforms']: | |
| 49 del features[name] | |
| 50 | |
| 51 return features | |
| 52 | |
| 53 def MergeFeatures(features, other): | |
| 54 '''Merge additional information into |features| from |other|. Merge features | |
| 55 with the same name with a standard dictionary update, instead of overwritting | |
| 56 features present in both dictionaries. Mutates and returns features. | |
| 57 ''' | |
| 58 for key, value in other.iteritems(): | |
| 59 if key in features: | |
| 60 features[key].update(value) | |
| 61 else: | |
| 62 features[key] = value | |
| 63 | |
| 64 return features | |
| OLD | NEW |