Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
|
not at google - send to devlin
2013/05/10 04:14:14
no (c)
jshumway
2013/05/11 02:37:08
Done.
| |
| 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 collections import defaultdict | |
| 6 import json | |
|
not at google - send to devlin
2013/05/10 04:14:14
pls use third_party.json_schema_compiler.json_pars
jshumway
2013/05/11 02:37:08
It is a cool function name, changed.
| |
| 7 | |
| 8 from third_party.json_schema_compiler.json_comment_eater import Nom | |
| 9 | |
| 10 class ManifestDataSource(object): | |
| 11 """ Provides a template with access to manifest properties specific to apps or | |
| 12 extensions. | |
| 13 """ | |
| 14 def __init__(self, compiled_fs_factory, manifest_path, features_path, channel) : | |
|
not at google - send to devlin
2013/05/10 04:14:14
line is too long
jshumway
2013/05/11 02:37:08
Done.
| |
| 15 # Path to manifest.json | |
| 16 self._manifest_path = manifest_path | |
| 17 # Path to _manifest_features.json | |
| 18 self._features_path = features_path | |
| 19 # Used to get raw json files. | |
| 20 self._compiled_fs = compiled_fs_factory.Create( | |
| 21 lambda _, contents: json.loads(Nom(contents)), ManifestDataSource) | |
| 22 # Used to cache the final apps and extensions manifest json objects. | |
|
not at google - send to devlin
2013/05/10 04:14:14
None of these comments add much IMO.
Three commen
jshumway
2013/05/11 02:37:08
Done.
| |
| 23 self._cache = compiled_fs_factory.Create( | |
| 24 self._CreateManifestData, ManifestDataSource) | |
| 25 | |
|
not at google - send to devlin
2013/05/10 04:14:14
no \n
jshumway
2013/05/11 02:37:08
Done.
| |
| 26 self._channel = channel | |
| 27 | |
| 28 def _ApplyAppsTransformations(self, manifest): | |
| 29 manifest['required'][0]['example'] = 'My Application' | |
|
not at google - send to devlin
2013/05/10 04:14:14
just App
jshumway
2013/05/11 02:37:08
Done.
| |
| 30 manifest['optional'][-1]['is_last'] = True | |
| 31 | |
| 32 def _ApplyExtensionsTransformations(self, manifest): | |
| 33 manifest['optional'][-1]['is_last'] = True | |
| 34 | |
| 35 def _LoadManifestData(self, content): | |
| 36 """Helper function to make testing easier. | |
| 37 """ | |
|
not at google - send to devlin
2013/05/10 04:14:14
how does this make testing easier? what does it ha
jshumway
2013/05/11 02:37:08
Removed it.
| |
| 38 return json.loads(Nom(content)) | |
| 39 | |
| 40 def _CreateManifestData(self, _, content): | |
| 41 """Take the contents of |_manifest_path| and create apps and extensions | |
| 42 versions of a manifest example based on the contents of |_features_path|. | |
| 43 """ | |
| 44 manifest_json = self._LoadManifestData(content) | |
| 45 features_json = self._compiled_fs.GetFromFile(self._features_path) | |
|
not at google - send to devlin
2013/05/10 04:14:14
... that said about the caches, I don't think this
jshumway
2013/05/11 02:37:08
Done.
| |
| 46 apps = defaultdict(list) | |
| 47 extensions = defaultdict(list) | |
| 48 | |
| 49 channels = {"stable": 0, "beta": 1, "dev": 2, "trunk": 3} | |
|
not at google - send to devlin
2013/05/10 04:14:14
try to use ' not " for strings. Less RSI.
jshumway
2013/05/11 02:37:08
My mistake
| |
| 50 app_types = frozenset(['packaged_app', 'hosted_app', 'platform_app']) | |
|
not at google - send to devlin
2013/05/10 04:14:14
Just platform_app.
I guess this logic will become
jshumway
2013/05/11 02:37:08
Changed.
| |
| 51 | |
| 52 def add_property(feature, mproperty, type_='optional'): | |
| 53 """If |feature|, from features_json, has the correct extension_types, add | |
| 54 |mproperty| to either apps or extensions. | |
| 55 """ | |
| 56 added = False | |
| 57 | |
| 58 if feature['extension_types'] == 'all': | |
| 59 apps[type_].append(mproperty) | |
| 60 extensions[type_].append(mproperty) | |
| 61 return True | |
| 62 # The following two conditions are not mutally exclusive. | |
| 63 if app_types.intersection(feature['extension_types']): | |
| 64 apps[type_].append(mproperty) | |
| 65 added = True | |
| 66 if 'extension' in feature['extension_types']: | |
| 67 extensions[type_].append(mproperty) | |
| 68 added = True | |
| 69 return added | |
|
not at google - send to devlin
2013/05/10 04:14:14
Also as I mentioned in IRC - we will need to group
| |
| 70 | |
| 71 # Property types are: required, only_one, recommended, and optional. | |
| 72 for property_type in manifest_json: | |
| 73 for mproperty in manifest_json[property_type]: | |
|
not at google - send to devlin
2013/05/10 04:14:14
mproperty -> manifest_key?
jshumway
2013/05/11 02:37:08
Done.
| |
| 74 # If a property is in manifest.json but not _manifest_features, this | |
| 75 # will cause an error. | |
| 76 feature = features_json[mproperty['name']] | |
| 77 # Only add properties that are available in a certain channel. | |
| 78 if channels[feature['channel']] <= channels[self._channel]: | |
|
not at google - send to devlin
2013/05/10 04:14:14
interesting. This is something we'll need to think
jshumway
2013/05/11 02:37:08
Done.
| |
| 79 if add_property(feature, mproperty, property_type): | |
| 80 del features_json[mproperty['name']] | |
|
not at google - send to devlin
2013/05/10 04:14:14
makes me nervous, better to take a copy of it befo
jshumway
2013/05/11 02:37:08
Done.
| |
| 81 | |
| 82 # All of the properties left in features_json are assumed to be optional. | |
| 83 for feature in sorted(features_json.keys()): | |
|
not at google - send to devlin
2013/05/10 04:14:14
as I mentioned - it looks a bit odd the way it is
jshumway
2013/05/11 02:37:08
Done.
| |
| 84 item = features_json[feature] | |
| 85 # In two cases, a features_json entry is a list for no obvious reason. | |
|
not at google - send to devlin
2013/05/10 04:14:14
it represents union :) one is usually for whitelis
jshumway
2013/05/11 02:37:08
I changed the comment.
| |
| 86 if isinstance(item, list): | |
| 87 item = item[0] | |
| 88 if channels[item['channel']] <= channels[self._channel]: | |
| 89 add_property(item, {'name': feature}) | |
| 90 | |
| 91 self._ApplyAppsTransformations(apps) | |
| 92 self._ApplyExtensionsTransformations(extensions) | |
| 93 | |
| 94 return {'apps': apps, 'extensions': extensions} | |
| 95 | |
| 96 def get(self, key): | |
| 97 return self._cache.GetFromFile(self._manifest_path)[key] | |
| OLD | NEW |