OLD | NEW |
---|---|
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 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 import json | 5 import json |
6 import logging | 6 import logging |
7 import os | 7 import os |
8 | 8 |
9 from handlebar_dict_generator import HandlebarDictGenerator | 9 from handlebar_dict_generator import HandlebarDictGenerator |
10 import third_party.json_schema_compiler.json_comment_eater as json_comment_eater | 10 import third_party.json_schema_compiler.json_comment_eater as json_comment_eater |
11 import third_party.json_schema_compiler.model as model | 11 import third_party.json_schema_compiler.model as model |
12 import third_party.json_schema_compiler.idl_schema as idl_schema | 12 import third_party.json_schema_compiler.idl_schema as idl_schema |
13 import third_party.json_schema_compiler.idl_parser as idl_parser | 13 import third_party.json_schema_compiler.idl_parser as idl_parser |
14 | 14 |
15 class APIDataSource(object): | 15 class APIDataSource(object): |
16 """This class fetches and loads JSON APIs from the FileSystem passed in with | 16 """This class fetches and loads JSON APIs from the FileSystem passed in with |
17 |cache_builder|, so the APIs can be plugged into templates. | 17 |cache_builder|, so the APIs can be plugged into templates. |
18 """ | 18 """ |
19 def __init__(self, cache_builder, base_path): | 19 def __init__(self, cache_builder, base_path): |
20 self._json_cache = cache_builder.build(self._LoadJsonAPI) | 20 self._json_cache = cache_builder.build(self._LoadJsonAPI) |
21 self._idl_cache = cache_builder.build(self._LoadIdlAPI) | 21 self._idl_cache = cache_builder.build(self._LoadIdlAPI) |
22 self._permissions_cache = cache_builder.build(self._LoadPermissions) | |
22 self._base_path = base_path | 23 self._base_path = base_path |
23 | 24 |
24 def _LoadJsonAPI(self, api): | 25 def _LoadJsonAPI(self, api): |
25 generator = HandlebarDictGenerator( | 26 generator = HandlebarDictGenerator( |
26 json.loads(json_comment_eater.Nom(api))[0]) | 27 json.loads(json_comment_eater.Nom(api))[0]) |
27 return generator.Generate() | 28 return generator.Generate() |
28 | 29 |
29 def _LoadIdlAPI(self, api): | 30 def _LoadIdlAPI(self, api): |
30 idl = idl_parser.IDLParser().ParseData(api) | 31 idl = idl_parser.IDLParser().ParseData(api) |
31 generator = HandlebarDictGenerator(idl_schema.IDLSchema(idl).process()[0]) | 32 generator = HandlebarDictGenerator(idl_schema.IDLSchema(idl).process()[0]) |
32 return generator.Generate() | 33 return generator.Generate() |
33 | 34 |
35 def _LoadPermissions(self, perms_json): | |
36 return json.loads(json_comment_eater.Nom(perms_json)) | |
37 | |
38 def _GetFeature(self, path): | |
39 path = path.replace('experimental_', '') | |
not at google - send to devlin
2012/07/30 19:24:33
why delete the "experimental" references? add comm
cduvall
2012/07/30 19:37:37
Done.
| |
40 try: | |
41 perms = self._permissions_cache.GetFromFile( | |
42 self._base_path + '/_permission_features.json') | |
43 api_perms = perms.get(path, None) | |
44 if api_perms['channel'] == 'dev': | |
45 api_perms['dev'] = True | |
46 return api_perms | |
47 except Exception: | |
48 return None | |
49 | |
50 def _AddPermissionsDict(self, api_dict, path): | |
51 return_dict = { 'permissions': self._GetFeature(path) } | |
52 return_dict.update(api_dict) | |
53 return return_dict | |
54 | |
34 def __getitem__(self, key): | 55 def __getitem__(self, key): |
35 return self.get(key) | 56 return self.get(key) |
36 | 57 |
37 def get(self, key): | 58 def get(self, key): |
38 path, ext = os.path.splitext(key) | 59 path, ext = os.path.splitext(key) |
39 unix_name = model.UnixName(path) | 60 unix_name = model.UnixName(path) |
40 json_path = unix_name + '.json' | 61 json_path = unix_name + '.json' |
41 idl_path = unix_name + '.idl' | 62 idl_path = unix_name + '.idl' |
42 try: | 63 try: |
43 return self._json_cache.GetFromFile(self._base_path + '/' + json_path) | 64 return self._AddPermissionsDict(self._json_cache.GetFromFile( |
65 self._base_path + '/' + json_path), path) | |
44 except Exception: | 66 except Exception: |
45 try: | 67 try: |
46 return self._idl_cache.GetFromFile(self._base_path + '/' + idl_path) | 68 return self._AddPermissionsDict(self._idl_cache.GetFromFile( |
69 self._base_path + '/' + idl_path), path) | |
47 except Exception as e: | 70 except Exception as e: |
48 logging.warn(e) | 71 logging.warn(e) |
49 return None | 72 return None |
OLD | NEW |