Chromium Code Reviews| 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 |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8b857f2873aee0339892451451e98b8cc76cf09d |
| --- /dev/null |
| +++ b/chrome/common/extensions/docs/server2/api_data_source.py |
| @@ -0,0 +1,55 @@ |
| +# Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +import json |
| +import logging |
| +import os |
| +import time |
| + |
| +import third_party.json_schema_compiler.json_comment_eater as json_comment_eater |
| +import third_party.json_schema_compiler.model as model |
| + |
| +class APIDataSource(object): |
|
not at google - send to devlin
2012/06/10 06:58:22
document
cduvall
2012/06/18 19:13:46
Done.
|
| + def __init__(self, fetcher, base_paths, cache_timeout_seconds): |
| + logging.info('API data source created: %s %d' % |
| + (' '.join(base_paths), cache_timeout_seconds)) |
| + self._fetcher = fetcher |
| + self._api_cache = {} |
| + self._base_paths = base_paths |
| + self._cache_timeout_seconds = cache_timeout_seconds |
| + |
| + class _CachedAPI(object): |
|
not at google - send to devlin
2012/06/10 06:58:22
This caching logic is basically the same as Templa
cduvall
2012/06/18 19:13:46
Done.
|
| + def __init__(self, api, expiry): |
| + self.api = api |
|
not at google - send to devlin
2012/06/10 06:58:22
private members should be _api
cduvall
2012/06/18 19:13:46
Done.
|
| + self._expiry = expiry |
| + |
| + def HasExpired(self): |
| + return time.time() > self._expiry |
| + |
| + def __getitem__(self, key): |
| + return self.get(key) |
| + |
| + def get(self, key): |
| + path, ext = os.path.splitext(key) |
| + key = model._UnixName(path) |
|
not at google - send to devlin
2012/06/10 06:58:22
overwriting the values of parameters confuses me,
cduvall
2012/06/18 19:13:46
Done.
|
| + path = key + '.json' |
| + if key in self._api_cache: |
| + if self._api_cache[key].HasExpired(): |
| + self._api_cache.pop(key) |
| + if key not in self._api_cache: |
| + logging.info('API cache miss for: ' + path) |
| + loaded_api = None |
| + for base_path in self._base_paths: |
| + try: |
| + api = self._fetcher.FetchResource(base_path + path).content |
|
not at google - send to devlin
2012/06/10 06:58:22
No path joining or something?
Oh I see, the paths
cduvall
2012/06/18 19:13:46
Done.
|
| + # The APIs are stored in a list, so get the api out of the list. |
| + loaded_api = json.loads(json_comment_eater.Nom(api))[0] |
| + self._api_cache[key] = self._CachedAPI( |
| + loaded_api, |
|
not at google - send to devlin
2012/06/10 06:58:22
So you're only caching the JSON? Are you planning
cduvall
2012/06/18 19:13:46
Yeah for now I'm just caching the JSON, but once t
|
| + time.time() + self._cache_timeout_seconds) |
| + break |
| + except: |
| + pass |
| + |
| + return loaded_api |
|
not at google - send to devlin
2012/06/10 06:58:22
Isn't this only getting set on a cache miss?
Oh,
cduvall
2012/06/18 19:13:46
Done.
|