Chromium Code Reviews| 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 """This class is needed so that an extensions API page does not have to fetch | |
| 17 the apps samples page and vice versa. | |
|
not at google - send to devlin
2012/08/10 06:02:18
nice
| |
| 18 """ | |
| 19 def __init__(self, api_name, samples): | |
| 20 self._api_name = api_name | |
| 21 self._samples = samples | |
| 22 | |
| 23 def get(self, key): | |
| 24 self._samples.FilterSamples(key, self._api_name) | |
| 25 | |
| 15 class APIDataSource(object): | 26 class APIDataSource(object): |
| 16 """This class fetches and loads JSON APIs from the FileSystem passed in with | 27 """This class fetches and loads JSON APIs from the FileSystem passed in with |
| 17 |cache_builder|, so the APIs can be plugged into templates. | 28 |cache_builder|, so the APIs can be plugged into templates. |
| 18 """ | 29 """ |
| 19 class Factory(object): | 30 class Factory(object): |
| 20 def __init__(self, cache_builder, base_path, samples_factory): | 31 def __init__(self, cache_builder, base_path, samples_factory): |
| 21 self._permissions_cache = cache_builder.build(self._LoadPermissions) | 32 self._permissions_cache = cache_builder.build(self._LoadPermissions) |
| 22 self._json_cache = cache_builder.build(self._LoadJsonAPI) | 33 self._json_cache = cache_builder.build(self._LoadJsonAPI) |
| 23 self._idl_cache = cache_builder.build(self._LoadIdlAPI) | 34 self._idl_cache = cache_builder.build(self._LoadIdlAPI) |
| 24 self._samples_factory = samples_factory | 35 self._samples_factory = samples_factory |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 60 try: | 71 try: |
| 61 perms = self._permissions_cache.GetFromFile( | 72 perms = self._permissions_cache.GetFromFile( |
| 62 self._base_path + '/_permission_features.json') | 73 self._base_path + '/_permission_features.json') |
| 63 api_perms = perms.get(path, None) | 74 api_perms = perms.get(path, None) |
| 64 if api_perms['channel'] == 'dev': | 75 if api_perms['channel'] == 'dev': |
| 65 api_perms['dev'] = True | 76 api_perms['dev'] = True |
| 66 return api_perms | 77 return api_perms |
| 67 except Exception: | 78 except Exception: |
| 68 return None | 79 return None |
| 69 | 80 |
| 70 def _GenerateHandlebarContext(self, api_name, handlebar, path): | 81 def _GenerateHandlebarContext(self, handlebar, path): |
| 71 return_dict = { 'permissions': self._GetFeature(path) } | 82 return_dict = { |
| 72 return_dict.update(handlebar.Generate( | 83 'permissions': self._GetFeature(path), |
| 73 self._FilterSamples(api_name, self._samples.values()))) | 84 'samples': _LazySamplesGetter(path, self._samples) |
| 85 } | |
| 86 return_dict.update(handlebar.Generate()) | |
| 74 return return_dict | 87 return return_dict |
| 75 | 88 |
| 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): | 89 def __getitem__(self, key): |
| 82 return self.get(key) | 90 return self.get(key) |
| 83 | 91 |
| 84 def get(self, key): | 92 def get(self, key): |
| 85 path, ext = os.path.splitext(key) | 93 path, ext = os.path.splitext(key) |
| 86 unix_name = model.UnixName(path) | 94 unix_name = model.UnixName(path) |
| 87 json_path = unix_name + '.json' | 95 json_path = unix_name + '.json' |
| 88 idl_path = unix_name + '.idl' | 96 idl_path = unix_name + '.idl' |
| 89 try: | 97 try: |
| 90 return self._GenerateHandlebarContext(key, | 98 return self._GenerateHandlebarContext( |
| 91 self._json_cache.GetFromFile(self._base_path + '/' + json_path), | 99 self._json_cache.GetFromFile(self._base_path + '/' + json_path), |
| 92 path) | 100 path) |
| 93 except OSError: | 101 except OSError: |
| 94 try: | 102 try: |
| 95 return self._GenerateHandlebarContext(key, | 103 return self._GenerateHandlebarContext( |
| 96 self._idl_cache.GetFromFile(self._base_path + '/' + idl_path), | 104 self._idl_cache.GetFromFile(self._base_path + '/' + idl_path), |
| 97 path) | 105 path) |
| 98 except OSError as e: | 106 except OSError as e: |
| 99 raise | 107 raise |
| OLD | NEW |