Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(145)

Side by Side Diff: chrome/common/extensions/docs/server2/features_model.py

Issue 16410002: Docserver manifest follow up (rewrite) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@gen-manifest-try-2
Patch Set: replace dictionaries with Feature class Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 # Copyright 2013 The Chromium Authors. All rights reserved. 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 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 from collections import MutableMapping
5 from copy import deepcopy 6 from copy import deepcopy
6 7
8 class Feature(MutableMapping):
9 def __init__(self, inital_dict=None):
10 inital_dict = inital_dict or dict()
11 if not 'platform' in inital_dict:
12 inital_dict['platform'] = []
13 inital_dict = inital_dict
14 self._dict = inital_dict
15
16 def __getitem__(self, key):
17 return self._dict[key]
18
19 def __setitem__(self, key, value):
20 self._dict[key] = value
21
22 def __delitem__(self, key):
23 del self._dict[key]
24
25 def __iter__(self):
26 return iter(self._dict)
27
28 def __len__(self):
29 return len(self._dict)
not at google - send to devlin 2013/08/05 22:23:49 Ok so now this just looks like a super magical dic
30
31 def __eq__(self, other):
not at google - send to devlin 2013/08/05 22:23:49 cos python is crap you need to define __ne__ as we
32 if isinstance(other, Feature):
33 return self._dict.__eq__(other._dict)
34 else:
35 return self._dict.__eq__(other)
36 return False
37
38 def __repr__(self):
39 return '<Feature(%s)>' % self._dict.__repr__()
not at google - send to devlin 2013/08/05 22:23:49 also override __str__
40
41 @property
42 def name(self):
43 return self._dict['name']
44
45 @name.setter
46 def name(self, value):
47 self._dict['name'] = value
not at google - send to devlin 2013/08/05 22:23:49 features are supposed to be immutable; why do you
48
49 @property
50 def platform(self):
not at google - send to devlin 2013/08/05 22:23:49 should this be "platforms"?
51 return self._dict['platform']
52
53 @platform.setter
54 def platform(self, value):
55 self._dict['platform'] = value
not at google - send to devlin 2013/08/05 22:23:49 likewise
56
57 def add_platform(self, value):
58 self._dict['platform'].append(value)
not at google - send to devlin 2013/08/05 22:23:49 likewise
59
7 class FeaturesModel(object): 60 class FeaturesModel(object):
8 '''Load and manipulate the contents of a features file. FeaturesModel is 61 '''Load and manipulate the contents of a features file. FeaturesModel is
9 immutable. All features are guaranteed to have 'name' and 'platform' 62 immutable. All features are guaranteed to have 'name' and 'platform'
10 properties. 63 properties.
11 ''' 64 '''
12 def __init__(self, features={}): 65 def __init__(self, features={}):
13 self._features = features 66 self._features = features
14 67
15 @staticmethod 68 @staticmethod
16 def FromJson(features_json): 69 def FromJson(features_json):
(...skipping 11 matching lines...) Expand all
28 if isinstance(value, list): 81 if isinstance(value, list):
29 values = [subvalue for subvalue in value if not 'whitelist' in subvalue] 82 values = [subvalue for subvalue in value if not 'whitelist' in subvalue]
30 if values: 83 if values:
31 value = values[0] 84 value = values[0]
32 else: 85 else:
33 continue 86 continue
34 87
35 if 'whitelist' in value: 88 if 'whitelist' in value:
36 continue 89 continue
37 90
38 features[name] = { 'platform': [] } 91 features[name] = Feature()
92 features[name].name = name
39 93
40 platforms = value.pop('extension_types') 94 platforms = value.pop('extension_types')
41 if platforms == 'all' or 'extension' in platforms: 95 if platforms == 'all' or 'extension' in platforms:
42 features[name]['platform'].append('extension') 96 features[name].add_platform('extension')
43 if platforms == 'all' or 'platform_app' in platforms: 97 if platforms == 'all' or 'platform_app' in platforms:
44 features[name]['platform'].append('app') 98 features[name].add_platform('app')
not at google - send to devlin 2013/08/05 22:23:49 these should be passed into feature's constructor
45
46 features[name]['name'] = name
47 features[name].update(value)
48 99
49 return FeaturesModel(features) 100 return FeaturesModel(features)
50 101
51 def Filter(self, platform=None): 102 def Filter(self, platform=None):
52 '''Filter out all features that are not relevant to a specific platform 103 '''Filter out all features that are not relevant to a specific platform
53 ('app' or 'extension'). 104 ('app' or 'extension').
54 ''' 105 '''
55 features = {} 106 features = {}
56 107
57 for name, feature in self._features.items(): 108 for name, feature in self._features.items():
58 if not platform or platform in feature['platform']: 109 if not platform or platform in feature.platform:
59 features[name] = feature 110 features[name] = feature
60 111
61 return FeaturesModel(features) 112 return FeaturesModel(features)
62 113
63 def MergeWith(self, other): 114 def MergeWith(self, other):
64 '''Do a semi-recursive merge. Any keys common to both |self| and |other| 115 '''Do a semi-recursive merge. Any keys common to both |self| and |other|
65 will have their dictionary values merged, instead of overwritten. 116 will have their dictionary values merged, instead of overwritten.
66 ''' 117 '''
67 features = self._features.copy() 118 features = self._features.copy()
68 119
69 for key, value in deepcopy(other).iteritems(): 120 for key, value in deepcopy(other).iteritems():
70 if key in self._features: 121 if key in self._features:
71 features[key].update(value) 122 features[key].update(value)
72 else: 123 else:
73 features[key] = value 124 features[key] = value
74 125
75 return FeaturesModel(features) 126 return FeaturesModel(features)
76 127
77 def Get(self): 128 def Get(self):
78 '''Returns a dictionary of the features file. 129 '''Returns a dictionary of the features file.
79 ''' 130 '''
80 return deepcopy(self._features) 131 return deepcopy(self._features)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698