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 class Factory(object): |
20 self._json_cache = cache_builder.build(self._LoadJsonAPI) | 20 def __init__(self, cache_builder, base_path, samples_factory): |
21 self._idl_cache = cache_builder.build(self._LoadIdlAPI) | 21 self._permissions_cache = cache_builder.build(self._LoadPermissions) |
22 self._permissions_cache = cache_builder.build(self._LoadPermissions) | 22 self._json_cache = cache_builder.build(self._LoadJsonAPI) |
| 23 self._idl_cache = cache_builder.build(self._LoadIdlAPI) |
| 24 self._samples_factory = samples_factory |
| 25 self._base_path = base_path |
| 26 |
| 27 def Create(self, request): |
| 28 return APIDataSource(self._permissions_cache, |
| 29 self._json_cache, |
| 30 self._idl_cache, |
| 31 self._base_path, |
| 32 self._samples_factory.Create(request)) |
| 33 |
| 34 def _LoadPermissions(self, json_str): |
| 35 return json.loads(json_comment_eater.Nom(json_str)) |
| 36 |
| 37 def _LoadJsonAPI(self, api): |
| 38 return HandlebarDictGenerator(json.loads(json_comment_eater.Nom(api))[0]) |
| 39 |
| 40 def _LoadIdlAPI(self, api): |
| 41 idl = idl_parser.IDLParser().ParseData(api) |
| 42 return HandlebarDictGenerator(idl_schema.IDLSchema(idl).process()[0]) |
| 43 |
| 44 def __init__(self, |
| 45 permissions_cache, |
| 46 json_cache, |
| 47 idl_cache, |
| 48 base_path, |
| 49 samples): |
23 self._base_path = base_path | 50 self._base_path = base_path |
24 | 51 self._permissions_cache = permissions_cache |
25 def _LoadJsonAPI(self, api): | 52 self._json_cache = json_cache |
26 generator = HandlebarDictGenerator( | 53 self._idl_cache = idl_cache |
27 json.loads(json_comment_eater.Nom(api))[0]) | 54 self._samples = samples |
28 return generator.Generate() | |
29 | |
30 def _LoadIdlAPI(self, api): | |
31 idl = idl_parser.IDLParser().ParseData(api) | |
32 generator = HandlebarDictGenerator(idl_schema.IDLSchema(idl).process()[0]) | |
33 return generator.Generate() | |
34 | |
35 def _LoadPermissions(self, perms_json): | |
36 return json.loads(json_comment_eater.Nom(perms_json)) | |
37 | 55 |
38 def _GetFeature(self, path): | 56 def _GetFeature(self, path): |
39 # Remove 'experimental_' from path name to match the keys in | 57 # Remove 'experimental_' from path name to match the keys in |
40 # _permissions_features.json. | 58 # _permissions_features.json. |
41 path = path.replace('experimental_', '') | 59 path = path.replace('experimental_', '') |
42 try: | 60 try: |
43 perms = self._permissions_cache.GetFromFile( | 61 perms = self._permissions_cache.GetFromFile( |
44 self._base_path + '/_permission_features.json') | 62 self._base_path + '/_permission_features.json') |
45 api_perms = perms.get(path, None) | 63 api_perms = perms.get(path, None) |
46 if api_perms['channel'] == 'dev': | 64 if api_perms['channel'] == 'dev': |
47 api_perms['dev'] = True | 65 api_perms['dev'] = True |
48 return api_perms | 66 return api_perms |
49 except Exception: | 67 except Exception: |
50 return None | 68 return None |
51 | 69 |
52 def _AddPermissionsDict(self, api_dict, path): | 70 def _GenerateHandlebarContext(self, api_name, handlebar, path): |
53 return_dict = { 'permissions': self._GetFeature(path) } | 71 return_dict = { 'permissions': self._GetFeature(path) } |
54 return_dict.update(api_dict) | 72 return_dict.update(handlebar.Generate( |
| 73 self._FilterSamples(api_name, self._samples.values()))) |
55 return return_dict | 74 return return_dict |
56 | 75 |
| 76 def _FilterSamples(self, api_name, samples): |
| 77 api_search = '.' + api_name + '.' |
| 78 return [sample for sample in samples |
| 79 if any(api_search in api['name'] for api in sample['api_calls'])] |
| 80 |
57 def __getitem__(self, key): | 81 def __getitem__(self, key): |
58 return self.get(key) | 82 return self.get(key) |
59 | 83 |
60 def get(self, key): | 84 def get(self, key): |
61 path, ext = os.path.splitext(key) | 85 path, ext = os.path.splitext(key) |
62 unix_name = model.UnixName(path) | 86 unix_name = model.UnixName(path) |
63 json_path = unix_name + '.json' | 87 json_path = unix_name + '.json' |
64 idl_path = unix_name + '.idl' | 88 idl_path = unix_name + '.idl' |
65 try: | 89 try: |
66 return self._AddPermissionsDict(self._json_cache.GetFromFile( | 90 return self._GenerateHandlebarContext(key, |
67 self._base_path + '/' + json_path), path) | 91 self._json_cache.GetFromFile(self._base_path + '/' + json_path), |
68 except Exception: | 92 path) |
| 93 except OSError: |
69 try: | 94 try: |
70 return self._AddPermissionsDict(self._idl_cache.GetFromFile( | 95 return self._GenerateHandlebarContext(key, |
71 self._base_path + '/' + idl_path), path) | 96 self._idl_cache.GetFromFile(self._base_path + '/' + idl_path), |
72 except Exception as e: | 97 path) |
73 logging.warn(e) | 98 except OSError as e: |
74 raise | 99 raise |
OLD | NEW |