| OLD | NEW |
| 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 ''' | 5 ''' |
| 6 Utility functions for working with the Feature abstraction. Features are grouped | 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 | 7 into a dictionary by name. Each Feature is guaranteed to have the following two |
| 8 keys: | 8 keys: |
| 9 name - a string, the name of the feature | 9 name - a string, the name of the feature |
| 10 platform - a list containing 'app' or 'extension', both, or neither. | 10 platform - a list containing 'apps' or 'extensions', both, or neither. |
| 11 | 11 |
| 12 A Feature may have other keys from a _features.json file as well. Features with | 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. | 13 a whitelist are ignored as they are only useful to specific apps or extensions. |
| 14 ''' | 14 ''' |
| 15 | 15 |
| 16 from copy import deepcopy | 16 from copy import deepcopy |
| 17 | 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 |
| 18 def Parse(features_json): | 26 def Parse(features_json): |
| 19 '''Process JSON from a _features.json file, standardizing it into a dictionary | 27 '''Process JSON from a _features.json file, standardizing it into a dictionary |
| 20 of Features. | 28 of Features. |
| 21 ''' | 29 ''' |
| 22 features = {} | 30 features = {} |
| 23 | 31 |
| 24 for name, value in deepcopy(features_json).iteritems(): | 32 for name, value in deepcopy(features_json).iteritems(): |
| 25 # Some feature names corrispond to a list; force a list down to a single | 33 # Some feature names correspond to a list; force a list down to a single |
| 26 # feature by removing entries that have a 'whitelist'. | 34 # feature by removing entries that have a 'whitelist'. |
| 27 if isinstance(value, list): | 35 if isinstance(value, list): |
| 28 values = [subvalue for subvalue in value if not 'whitelist' in subvalue] | 36 values = [subvalue for subvalue in value if not 'whitelist' in subvalue] |
| 29 if values: | 37 if values: |
| 30 value = values[0] | 38 value = values[0] |
| 31 else: | 39 else: |
| 32 continue | 40 continue |
| 33 | 41 |
| 34 if 'whitelist' in value: | 42 if 'whitelist' in value: |
| 35 continue | 43 continue |
| 36 | 44 |
| 37 features[name] = { 'platforms': [] } | 45 features[name] = { 'platforms': [] } |
| 38 | 46 |
| 39 platforms = value.pop('extension_types') | 47 extension_types = value.pop('extension_types', None) |
| 40 if platforms == 'all' or 'platform_app' in platforms: | 48 if extension_types is not None: |
| 41 features[name]['platforms'].append('app') | 49 features[name]['platforms'] = _GetPlatformsForExtensionTypes( |
| 42 if platforms == 'all' or 'extension' in platforms: | 50 extension_types) |
| 43 features[name]['platforms'].append('extension') | |
| 44 | 51 |
| 45 features[name]['name'] = name | 52 features[name]['name'] = name |
| 46 features[name].update(value) | 53 features[name].update(value) |
| 47 | 54 |
| 48 return features | 55 return features |
| 49 | 56 |
| 50 def Filtered(features, platform=None): | 57 def Filtered(features, platform=None): |
| 51 '''Create a new Features dictionary from |features| that contains only items | 58 '''Create a new Features dictionary from |features| that contains only items |
| 52 relevant to |platform|. Items retained are deepcopied. Returns new features | 59 relevant to |platform|. Items retained are deepcopied. Returns new features |
| 53 dictionary. | 60 dictionary. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 72 else: | 79 else: |
| 73 features[key] = value | 80 features[key] = value |
| 74 | 81 |
| 75 # Ensure the Feature schema is enforced for all added items. | 82 # Ensure the Feature schema is enforced for all added items. |
| 76 if not 'name' in features[key]: | 83 if not 'name' in features[key]: |
| 77 features[key]['name'] = key | 84 features[key]['name'] = key |
| 78 if not 'platforms' in features[key]: | 85 if not 'platforms' in features[key]: |
| 79 features[key]['platforms'] = [] | 86 features[key]['platforms'] = [] |
| 80 | 87 |
| 81 return features | 88 return features |
| OLD | NEW |