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 | |
7 import os | 6 import os |
8 | 7 |
8 from file_system import FileNotFoundError | |
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 """ |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
53 self._idl_cache = idl_cache | 53 self._idl_cache = idl_cache |
54 self._samples = samples | 54 self._samples = samples |
55 | 55 |
56 def _GetFeature(self, path): | 56 def _GetFeature(self, path): |
57 # Remove 'experimental_' from path name to match the keys in | 57 # Remove 'experimental_' from path name to match the keys in |
58 # _permissions_features.json. | 58 # _permissions_features.json. |
59 path = path.replace('experimental_', '') | 59 path = path.replace('experimental_', '') |
60 try: | 60 try: |
61 perms = self._permissions_cache.GetFromFile( | 61 perms = self._permissions_cache.GetFromFile( |
62 self._base_path + '/_permission_features.json') | 62 self._base_path + '/_permission_features.json') |
63 api_perms = perms.get(path, None) | 63 except FileNotFoundError: |
64 if api_perms['channel'] == 'dev': | |
65 api_perms['dev'] = True | |
66 return api_perms | |
67 except Exception: | |
68 return None | 64 return None |
65 api_perms = perms.get(path, None) | |
66 if api_perms is None: | |
67 return None | |
68 if api_perms['channel'] == 'dev': | |
69 api_perms['dev'] = True | |
70 return api_perms | |
69 | 71 |
70 def _GenerateHandlebarContext(self, api_name, handlebar, path): | 72 def _GenerateHandlebarContext(self, api_name, handlebar, path): |
71 return_dict = { 'permissions': self._GetFeature(path) } | 73 return_dict = { 'permissions': self._GetFeature(path) } |
72 return_dict.update(handlebar.Generate( | 74 return_dict.update(handlebar.Generate( |
73 self._FilterSamples(api_name, self._samples.values()))) | 75 self._FilterSamples(api_name, self._samples.values()))) |
74 return return_dict | 76 return return_dict |
75 | 77 |
76 def _FilterSamples(self, api_name, samples): | 78 def _FilterSamples(self, api_name, samples): |
77 api_search = '.' + api_name + '.' | 79 api_search = '.' + api_name + '.' |
78 return [sample for sample in samples | 80 return [sample for sample in samples |
79 if any(api_search in api['name'] for api in sample['api_calls'])] | 81 if any(api_search in api['name'] for api in sample['api_calls'])] |
80 | 82 |
81 def __getitem__(self, key): | 83 def __getitem__(self, key): |
82 return self.get(key) | 84 return self.get(key) |
83 | 85 |
84 def get(self, key): | 86 def get(self, key): |
85 path, ext = os.path.splitext(key) | 87 path, ext = os.path.splitext(key) |
86 unix_name = model.UnixName(path) | 88 unix_name = model.UnixName(path) |
87 json_path = unix_name + '.json' | 89 json_path = unix_name + '.json' |
88 idl_path = unix_name + '.idl' | 90 idl_path = unix_name + '.idl' |
89 try: | 91 try: |
90 return self._GenerateHandlebarContext(key, | 92 return self._GenerateHandlebarContext(key, |
91 self._json_cache.GetFromFile(self._base_path + '/' + json_path), | 93 self._json_cache.GetFromFile(self._base_path + '/' + json_path), |
92 path) | 94 path) |
93 except OSError: | 95 except FileNotFoundError: |
94 try: | 96 return self._GenerateHandlebarContext(key, |
95 return self._GenerateHandlebarContext(key, | 97 self._idl_cache.GetFromFile(self._base_path + '/' + idl_path), |
96 self._idl_cache.GetFromFile(self._base_path + '/' + idl_path), | 98 path) |
not at google - send to devlin
2012/08/10 06:12:16
not sure, perhaps this should also be catching Fil
cduvall
2012/08/10 17:25:16
I would rather have it throw the exception than re
not at google - send to devlin
2012/08/12 22:22:58
Ok sg. Makes me wonder if there are other things (
cduvall
2012/08/13 18:44:05
Done.
| |
97 path) | |
98 except OSError as e: | |
99 raise | |
OLD | NEW |