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 file_system import FileNotFoundError | 9 from file_system import FileNotFoundError |
10 from handlebar_dict_generator import HandlebarDictGenerator | 10 from handlebar_dict_generator import HandlebarDictGenerator |
11 import third_party.json_schema_compiler.json_comment_eater as json_comment_eater | 11 import third_party.json_schema_compiler.json_comment_eater as json_comment_eater |
12 import third_party.json_schema_compiler.model as model | 12 import third_party.json_schema_compiler.model as model |
13 import third_party.json_schema_compiler.idl_schema as idl_schema | 13 import third_party.json_schema_compiler.idl_schema as idl_schema |
14 import third_party.json_schema_compiler.idl_parser as idl_parser | 14 import third_party.json_schema_compiler.idl_parser as idl_parser |
15 | 15 |
16 class _LazySamplesGetter(object): | |
17 """This class is needed so that an extensions API page does not have to fetch | |
18 the apps samples page and vice versa. | |
19 """ | |
20 def __init__(self, api_name, samples): | |
21 self._api_name = api_name | |
22 self._samples = samples | |
23 | |
24 def get(self, key): | |
25 return self._samples.FilterSamples(key, self._api_name) | |
26 | |
16 class APIDataSource(object): | 27 class APIDataSource(object): |
17 """This class fetches and loads JSON APIs from the FileSystem passed in with | 28 """This class fetches and loads JSON APIs from the FileSystem passed in with |
18 |cache_builder|, so the APIs can be plugged into templates. | 29 |cache_builder|, so the APIs can be plugged into templates. |
19 """ | 30 """ |
20 class Factory(object): | 31 class Factory(object): |
21 def __init__(self, cache_builder, base_path, samples_factory): | 32 def __init__(self, cache_builder, base_path, samples_factory): |
22 self._permissions_cache = cache_builder.build(self._LoadPermissions) | 33 self._permissions_cache = cache_builder.build(self._LoadPermissions) |
23 self._json_cache = cache_builder.build(self._LoadJsonAPI) | 34 self._json_cache = cache_builder.build(self._LoadJsonAPI) |
24 self._idl_cache = cache_builder.build(self._LoadIdlAPI) | 35 self._idl_cache = cache_builder.build(self._LoadIdlAPI) |
25 self._samples_factory = samples_factory | 36 self._samples_factory = samples_factory |
(...skipping 30 matching lines...) Expand all Loading... | |
56 | 67 |
57 def _GetFeature(self, path): | 68 def _GetFeature(self, path): |
58 # Remove 'experimental_' from path name to match the keys in | 69 # Remove 'experimental_' from path name to match the keys in |
59 # _permissions_features.json. | 70 # _permissions_features.json. |
60 path = path.replace('experimental_', '') | 71 path = path.replace('experimental_', '') |
61 try: | 72 try: |
62 perms = self._permissions_cache.GetFromFile( | 73 perms = self._permissions_cache.GetFromFile( |
63 self._base_path + '/_permission_features.json') | 74 self._base_path + '/_permission_features.json') |
64 except FileNotFoundError: | 75 except FileNotFoundError: |
65 return None | 76 return None |
77 perms = dict([(model.UnixName(k), v) for k, v in perms.iteritems()]) | |
not at google - send to devlin
2012/08/13 01:51:11
i think that just dict(...) will work, don't need
cduvall
2012/08/13 17:47:06
Done.
| |
66 api_perms = perms.get(path, None) | 78 api_perms = perms.get(path, None) |
67 if api_perms is None: | 79 if api_perms is None: |
68 return None | 80 return None |
69 if api_perms['channel'] == 'dev': | 81 if api_perms['channel'] == 'dev': |
70 api_perms['dev'] = True | 82 api_perms['dev'] = True |
71 return api_perms | 83 return api_perms |
72 | 84 |
73 def _GenerateHandlebarContext(self, api_name, handlebar, path): | 85 def _GenerateHandlebarContext(self, handlebar, path): |
74 return_dict = { 'permissions': self._GetFeature(path) } | 86 return_dict = { |
75 return_dict.update(handlebar.Generate( | 87 'permissions': self._GetFeature(path), |
76 self._FilterSamples(api_name, self._samples.values()))) | 88 'samples': _LazySamplesGetter(path, self._samples) |
89 } | |
90 return_dict.update(handlebar.Generate()) | |
77 return return_dict | 91 return return_dict |
78 | 92 |
79 def _FilterSamples(self, api_name, samples): | |
80 api_search = '.' + api_name + '.' | |
81 return [sample for sample in samples | |
82 if any(api_search in api['name'] for api in sample['api_calls'])] | |
83 | |
84 def __getitem__(self, key): | 93 def __getitem__(self, key): |
85 return self.get(key) | 94 return self.get(key) |
86 | 95 |
87 def get(self, key): | 96 def get(self, key): |
88 path, ext = os.path.splitext(key) | 97 path, ext = os.path.splitext(key) |
89 unix_name = model.UnixName(path) | 98 unix_name = model.UnixName(path) |
90 json_path = unix_name + '.json' | 99 json_path = unix_name + '.json' |
91 idl_path = unix_name + '.idl' | 100 idl_path = unix_name + '.idl' |
92 try: | 101 try: |
93 return self._GenerateHandlebarContext(key, | 102 return self._GenerateHandlebarContext( |
94 self._json_cache.GetFromFile(self._base_path + '/' + json_path), | 103 self._json_cache.GetFromFile(self._base_path + '/' + json_path), |
95 path) | 104 path) |
96 except FileNotFoundError: | 105 except FileNotFoundError: |
97 try: | 106 try: |
98 return self._GenerateHandlebarContext(key, | 107 return self._GenerateHandlebarContext( |
99 self._idl_cache.GetFromFile(self._base_path + '/' + idl_path), | 108 self._idl_cache.GetFromFile(self._base_path + '/' + idl_path), |
100 path) | 109 path) |
101 except FileNotFoundError as e: | 110 except FileNotFoundError as e: |
102 logging.error(e) | 111 logging.error(e) |
103 raise | 112 raise |
OLD | NEW |