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 from copy import deepcopy | |
| 6 | |
| 7 class FeaturesModel(object): | |
| 8 '''Load and manipulate the contents of a features file. FeaturesModel is | |
| 9 immutable. | |
| 10 ''' | |
| 11 def __init__(self, features={}): | |
| 12 self._features = features | |
|
not at google - send to devlin
2013/07/31 18:50:20
I meant for the features to be typed, but I forgot
jshumway
2013/08/01 22:27:50
As discussed via email, here is the solution with
| |
| 13 | |
| 14 @staticmethod | |
| 15 def LoadFeaturesJson(features_json): | |
|
not at google - send to devlin
2013/07/31 18:50:20
prefer FromJson here, it looks better.
jshumway
2013/08/01 22:27:50
Done.
| |
| 16 '''Take the raw json of a features file and clean it up. This involves | |
| 17 reducing any keys with lists as values to sinlge dicts, converting | |
| 18 'extension_types' to a list that contains 'app', 'extension', or both, | |
| 19 adding a 'name' field, and ignoring features that have a 'whitelist' | |
| 20 attribute. | |
| 21 ''' | |
| 22 features = {} | |
| 23 | |
| 24 for name, value in features_json.iteritems(): | |
| 25 if isinstance(value, list): | |
|
not at google - send to devlin
2013/07/31 18:50:20
needs some comments.
jshumway
2013/08/01 22:27:50
Done.
| |
| 26 temp = None | |
| 27 for subvalue in value: | |
| 28 if not 'whitelist' in subvalue: | |
| 29 temp = subvalue | |
| 30 break | |
| 31 else: | |
| 32 continue | |
|
not at google - send to devlin
2013/07/31 18:50:20
wow else after a for loop looks weird. can you str
jshumway
2013/08/01 22:27:50
Done.
| |
| 33 value = temp | |
| 34 | |
| 35 if 'whitelist' in value: | |
| 36 continue | |
|
not at google - send to devlin
2013/07/31 18:50:20
wouldn't need this, it would be implicit
jshumway
2013/08/01 22:27:50
This still required for the case where value is no
| |
| 37 features[name] = { 'platform': [] } | |
| 38 | |
| 39 platforms = value.pop('extension_types') | |
| 40 if platforms == 'all' or 'extension' in platforms: | |
| 41 features[name]['platform'].append('extension') | |
| 42 if platforms == 'all' or 'platform_app' in platforms: | |
| 43 features[name]['platform'].append('app') | |
| 44 | |
| 45 features[name]['name'] = name | |
| 46 features[name].update(value) | |
| 47 | |
| 48 return FeaturesModel(features) | |
| 49 | |
| 50 def Filter(self, platform=None): | |
| 51 '''Filter out all features that are not relevant to a specific platform | |
| 52 ('app' or 'extension'). | |
| 53 ''' | |
| 54 features = {} | |
| 55 | |
| 56 for name, feature in self._features.items(): | |
| 57 if not platform or platform in feature['platform']: | |
| 58 features[name] = feature | |
| 59 | |
| 60 return FeaturesModel(features) | |
| 61 | |
| 62 def MergeWith(self, other): | |
| 63 '''Do a semi-recursive merge. Any keys common to both |self| and |other| | |
| 64 will have their dictionary values merged, instead of overwritten. | |
| 65 ''' | |
| 66 features = self._features.copy() | |
| 67 | |
| 68 for key, value in deepcopy(other).iteritems(): | |
| 69 if key in self._features: | |
| 70 features[key].update(value) | |
| 71 else: | |
| 72 features[key] = value | |
| 73 | |
| 74 return FeaturesModel(features) | |
| 75 | |
| 76 def Get(self): | |
| 77 '''Returns a dictionary of the features file. | |
| 78 ''' | |
| 79 return deepcopy(self._features) | |
| OLD | NEW |