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