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 = deepcopy(features) | |
| 13 | |
| 14 def LoadFeaturesJson(self, features_json): | |
|
not at google - send to devlin
2013/07/31 00:36:39
make this a static method like Parse on FeaturesMo
jshumway
2013/07/31 18:10:28
Done.
| |
| 15 '''Take the raw json of a features file and clean it up. This involves | |
| 16 reducing any keys with lists as values to sinlge dicts, converting | |
| 17 'extension_types' to a list that contains 'app', 'extension', or both, | |
| 18 adding a 'name' field, and ignoring features that have a 'whitelist' | |
| 19 attribute. | |
| 20 ''' | |
| 21 features = deepcopy(self._features) | |
| 22 | |
| 23 for name, value in features_json.iteritems(): | |
| 24 if isinstance(value, list): | |
| 25 temp = None | |
| 26 for subvalue in value: | |
| 27 if not 'whitelist' in subvalue: | |
| 28 temp = subvalue | |
| 29 break | |
| 30 else: | |
| 31 continue | |
| 32 value = temp | |
| 33 | |
| 34 if 'whitelist' in value: | |
| 35 continue | |
| 36 features[name] = { 'platform': [] } | |
| 37 | |
| 38 platforms = value.pop('extension_types') | |
| 39 if platforms == 'all' or 'extension' in platforms: | |
| 40 features[name]['platform'].append('extension') | |
| 41 if platforms == 'all' or 'platform_app' in platforms: | |
| 42 features[name]['platform'].append('app') | |
| 43 | |
| 44 features[name]['name'] = name | |
| 45 features[name].update(value) | |
| 46 | |
| 47 return FeaturesModel(features) | |
| 48 | |
| 49 def Filter(self, platform=None): | |
| 50 '''Remove all entries that are not relevant to |platform|. | |
| 51 ''' | |
| 52 features = deepcopy(self._features) | |
| 53 | |
| 54 for name, feature in features.items(): | |
| 55 if platform and not platform in feature['platform']: | |
| 56 del features[name] | |
|
not at google - send to devlin
2013/07/31 00:36:39
it would be more efficient to do this constructive
jshumway
2013/07/31 18:10:28
Done.
| |
| 57 | |
| 58 return FeaturesModel(features) | |
| 59 | |
| 60 def MergeWith(self, other): | |
| 61 '''Do a semi-recursive merge. Any keys common to both |self| and |other| | |
| 62 will have their dictionary values merged, instead of overwritten. | |
| 63 ''' | |
| 64 features = deepcopy(self._features) | |
| 65 | |
| 66 for key, value in other.iteritems(): | |
| 67 if key in self._features: | |
| 68 features[key].update(value) | |
| 69 else: | |
| 70 features[key] = value | |
| 71 | |
| 72 return FeaturesModel(features) | |
|
not at google - send to devlin
2013/07/31 00:36:39
likewise
all that really needs to be different be
jshumway
2013/07/31 18:10:28
Done.
| |
| 73 | |
| 74 def RestructureChildren(self): | |
|
not at google - send to devlin
2013/07/31 00:36:39
I think that RestructureChidlren actually sounds l
jshumway
2013/07/31 18:10:28
See below.
| |
| 75 '''Features whose names are of the form 'parent.child' are moved to be part | |
| 76 of the 'parent' dictionary under the key 'children'. Names are changed to | |
| 77 the 'child' section of the original name. | |
| 78 ''' | |
| 79 features = deepcopy(self._features) | |
|
not at google - send to devlin
2013/07/31 00:36:39
I guess deepcopy here is ok, though it is unnecess
jshumway
2013/07/31 18:10:28
Okay, I moved this method into manifest data sourc
| |
| 80 | |
| 81 def add_child(features, parent, child_name, value): | |
| 82 value['name'] = child_name | |
| 83 if not 'children' in features[parent]: | |
| 84 features[parent]['children'] = {} | |
| 85 features[parent]['children'][child_name] = value | |
| 86 | |
| 87 def insert_children(features): | |
| 88 for name in features.keys(): | |
| 89 if '.' in name: | |
| 90 value = features.pop(name) | |
| 91 parent, child_name = name.split('.', 1) | |
| 92 add_child(features, parent, child_name, value) | |
| 93 | |
| 94 for value in features.values(): | |
| 95 if 'children' in value: | |
| 96 insert_children(value['children']) | |
| 97 | |
| 98 insert_children(features) | |
| 99 return features | |
| 100 | |
| 101 def Get(self): | |
| 102 '''Returns a dictionary of the features file. | |
| 103 ''' | |
| 104 return deepcopy(self._features) | |
| OLD | NEW |