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 _LazySamplesGetter(object): | |
16 def __init__(self, api_name, samples): | |
17 self._api_search = '_' + api_name + '_' | |
18 self._samples = samples | |
19 | |
20 def _FilterSamples(self, key, samples): | |
21 logging.info(self._api_search) | |
not at google - send to devlin
2012/08/09 07:52:36
remove logging?
cduvall
2012/08/09 19:20:08
Done.
| |
22 samples_list = [] | |
23 for sample in samples.get(key): | |
24 api_calls_unix = [model.UnixName(call['name']) | |
25 for call in sample['api_calls']] | |
26 for call in api_calls_unix: | |
27 if self._api_search in call: | |
28 samples_list.append(sample) | |
not at google - send to devlin
2012/08/09 07:52:36
All this seems like it could be a method on Sample
cduvall
2012/08/09 19:20:08
Done.
| |
29 break | |
30 return samples_list | |
31 | |
32 def get(self, key): | |
33 # Try to get the samples, but it's not a big deal if we can't. It's more | |
34 # important to render the API page. | |
not at google - send to devlin
2012/08/09 07:52:36
I don't follow this comment nor why you need to ca
cduvall
2012/08/09 19:20:08
Done.
| |
35 try: | |
36 return self._FilterSamples(key, self._samples) | |
37 except Exception as e: | |
38 logging.warn(e) | |
39 | |
15 class APIDataSource(object): | 40 class APIDataSource(object): |
16 """This class fetches and loads JSON APIs from the FileSystem passed in with | 41 """This class fetches and loads JSON APIs from the FileSystem passed in with |
17 |cache_builder|, so the APIs can be plugged into templates. | 42 |cache_builder|, so the APIs can be plugged into templates. |
18 """ | 43 """ |
19 class Factory(object): | 44 class Factory(object): |
20 def __init__(self, cache_builder, base_path, samples_factory): | 45 def __init__(self, cache_builder, base_path, samples_factory): |
21 self._permissions_cache = cache_builder.build(self._LoadPermissions) | 46 self._permissions_cache = cache_builder.build(self._LoadPermissions) |
22 self._json_cache = cache_builder.build(self._LoadJsonAPI) | 47 self._json_cache = cache_builder.build(self._LoadJsonAPI) |
23 self._idl_cache = cache_builder.build(self._LoadIdlAPI) | 48 self._idl_cache = cache_builder.build(self._LoadIdlAPI) |
24 self._samples_factory = samples_factory | 49 self._samples_factory = samples_factory |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
60 try: | 85 try: |
61 perms = self._permissions_cache.GetFromFile( | 86 perms = self._permissions_cache.GetFromFile( |
62 self._base_path + '/_permission_features.json') | 87 self._base_path + '/_permission_features.json') |
63 api_perms = perms.get(path, None) | 88 api_perms = perms.get(path, None) |
64 if api_perms['channel'] == 'dev': | 89 if api_perms['channel'] == 'dev': |
65 api_perms['dev'] = True | 90 api_perms['dev'] = True |
66 return api_perms | 91 return api_perms |
67 except Exception: | 92 except Exception: |
68 return None | 93 return None |
69 | 94 |
70 def _GenerateHandlebarContext(self, api_name, handlebar, path): | 95 def _GenerateHandlebarContext(self, handlebar, path): |
71 return_dict = { 'permissions': self._GetFeature(path) } | 96 return_dict = { |
72 return_dict.update(handlebar.Generate( | 97 'permissions': self._GetFeature(path), |
73 self._FilterSamples(api_name, self._samples.values()))) | 98 'samples': _LazySamplesGetter(path, self._samples) |
99 } | |
100 return_dict.update(handlebar.Generate()) | |
74 return return_dict | 101 return return_dict |
75 | 102 |
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 | |
81 def __getitem__(self, key): | 103 def __getitem__(self, key): |
82 return self.get(key) | 104 return self.get(key) |
83 | 105 |
84 def get(self, key): | 106 def get(self, key): |
85 path, ext = os.path.splitext(key) | 107 path, ext = os.path.splitext(key) |
86 unix_name = model.UnixName(path) | 108 unix_name = model.UnixName(path) |
87 json_path = unix_name + '.json' | 109 json_path = unix_name + '.json' |
88 idl_path = unix_name + '.idl' | 110 idl_path = unix_name + '.idl' |
89 try: | 111 try: |
90 return self._GenerateHandlebarContext(key, | 112 return self._GenerateHandlebarContext( |
91 self._json_cache.GetFromFile(self._base_path + '/' + json_path), | 113 self._json_cache.GetFromFile(self._base_path + '/' + json_path), |
92 path) | 114 path) |
93 except OSError: | 115 except OSError: |
94 try: | 116 try: |
95 return self._GenerateHandlebarContext(key, | 117 return self._GenerateHandlebarContext( |
96 self._idl_cache.GetFromFile(self._base_path + '/' + idl_path), | 118 self._idl_cache.GetFromFile(self._base_path + '/' + idl_path), |
97 path) | 119 path) |
98 except OSError as e: | 120 except OSError as e: |
99 raise | 121 raise |
OLD | NEW |