Index: chrome/common/extensions/docs/server2/api_data_source.py |
diff --git a/chrome/common/extensions/docs/server2/api_data_source.py b/chrome/common/extensions/docs/server2/api_data_source.py |
index d4ea2a2cea1b3930a8a76d06d89e94c51b1a0d16..5e07ea25b0e763abe88890d40f2cd9db9bda4b96 100644 |
--- a/chrome/common/extensions/docs/server2/api_data_source.py |
+++ b/chrome/common/extensions/docs/server2/api_data_source.py |
@@ -16,24 +16,42 @@ class APIDataSource(object): |
"""This class fetches and loads JSON APIs from the FileSystem passed in with |
|cache_builder|, so the APIs can be plugged into templates. |
""" |
- def __init__(self, cache_builder, base_path): |
- self._json_cache = cache_builder.build(self._LoadJsonAPI) |
- self._idl_cache = cache_builder.build(self._LoadIdlAPI) |
- self._permissions_cache = cache_builder.build(self._LoadPermissions) |
- self._base_path = base_path |
+ class Factory(object): |
+ def __init__(self, cache_builder, base_path, samples_factory): |
+ self._permissions_cache = cache_builder.build(self._LoadPermissions) |
+ self._json_cache = cache_builder.build(self._LoadJsonAPI) |
+ self._idl_cache = cache_builder.build(self._LoadIdlAPI) |
+ self._samples_factory = samples_factory |
+ self._base_path = base_path |
+ |
+ def Create(self, request): |
+ return APIDataSource(self._permissions_cache, |
+ self._json_cache, |
+ self._idl_cache, |
+ self._base_path, |
+ self._samples_factory.Create(request)) |
+ |
+ def _LoadPermissions(self, json_str): |
+ return json.loads(json_comment_eater.Nom(json_str)) |
- def _LoadJsonAPI(self, api): |
- generator = HandlebarDictGenerator( |
- json.loads(json_comment_eater.Nom(api))[0]) |
- return generator.Generate() |
+ def _LoadJsonAPI(self, api): |
+ return HandlebarDictGenerator(json.loads(json_comment_eater.Nom(api))[0]) |
- def _LoadIdlAPI(self, api): |
- idl = idl_parser.IDLParser().ParseData(api) |
- generator = HandlebarDictGenerator(idl_schema.IDLSchema(idl).process()[0]) |
- return generator.Generate() |
+ def _LoadIdlAPI(self, api): |
+ idl = idl_parser.IDLParser().ParseData(api) |
+ return HandlebarDictGenerator(idl_schema.IDLSchema(idl).process()[0]) |
- def _LoadPermissions(self, perms_json): |
- return json.loads(json_comment_eater.Nom(perms_json)) |
+ def __init__(self, |
+ permissions_cache, |
+ json_cache, |
+ idl_cache, |
+ base_path, |
+ samples): |
+ self._base_path = base_path |
+ self._permissions_cache = permissions_cache |
+ self._json_cache = json_cache |
+ self._idl_cache = idl_cache |
+ self._samples = samples |
def _GetFeature(self, path): |
# Remove 'experimental_' from path name to match the keys in |
@@ -49,11 +67,17 @@ class APIDataSource(object): |
except Exception: |
return None |
- def _AddPermissionsDict(self, api_dict, path): |
+ def _GenerateHandlebarContext(self, api_name, handlebar, path): |
return_dict = { 'permissions': self._GetFeature(path) } |
- return_dict.update(api_dict) |
+ return_dict.update(handlebar.Generate( |
+ self._FilterSamples(api_name, self._samples.values()))) |
return return_dict |
+ def _FilterSamples(self, api_name, samples): |
+ api_search = '.' + api_name + '.' |
+ return [sample for sample in samples |
+ if any(api_search in api['name'] for api in sample['api_calls'])] |
+ |
def __getitem__(self, key): |
return self.get(key) |
@@ -63,12 +87,13 @@ class APIDataSource(object): |
json_path = unix_name + '.json' |
idl_path = unix_name + '.idl' |
try: |
- return self._AddPermissionsDict(self._json_cache.GetFromFile( |
- self._base_path + '/' + json_path), path) |
- except Exception: |
+ return self._GenerateHandlebarContext(key, |
+ self._json_cache.GetFromFile(self._base_path + '/' + json_path), |
+ path) |
+ except OSError: |
try: |
- return self._AddPermissionsDict(self._idl_cache.GetFromFile( |
- self._base_path + '/' + idl_path), path) |
- except Exception as e: |
- logging.warn(e) |
+ return self._GenerateHandlebarContext(key, |
+ self._idl_cache.GetFromFile(self._base_path + '/' + idl_path), |
+ path) |
+ except OSError as e: |
raise |