Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 import json | |
| 6 import logging | |
| 7 import os | |
| 8 import time | |
| 9 | |
| 10 import third_party.json_schema_compiler.json_comment_eater as json_comment_eater | |
| 11 import third_party.json_schema_compiler.model as model | |
| 12 | |
| 13 class APIDataSource(object): | |
|
not at google - send to devlin
2012/06/10 06:58:22
document
cduvall
2012/06/18 19:13:46
Done.
| |
| 14 def __init__(self, fetcher, base_paths, cache_timeout_seconds): | |
| 15 logging.info('API data source created: %s %d' % | |
| 16 (' '.join(base_paths), cache_timeout_seconds)) | |
| 17 self._fetcher = fetcher | |
| 18 self._api_cache = {} | |
| 19 self._base_paths = base_paths | |
| 20 self._cache_timeout_seconds = cache_timeout_seconds | |
| 21 | |
| 22 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.
| |
| 23 def __init__(self, api, expiry): | |
| 24 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.
| |
| 25 self._expiry = expiry | |
| 26 | |
| 27 def HasExpired(self): | |
| 28 return time.time() > self._expiry | |
| 29 | |
| 30 def __getitem__(self, key): | |
| 31 return self.get(key) | |
| 32 | |
| 33 def get(self, key): | |
| 34 path, ext = os.path.splitext(key) | |
| 35 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.
| |
| 36 path = key + '.json' | |
| 37 if key in self._api_cache: | |
| 38 if self._api_cache[key].HasExpired(): | |
| 39 self._api_cache.pop(key) | |
| 40 if key not in self._api_cache: | |
| 41 logging.info('API cache miss for: ' + path) | |
| 42 loaded_api = None | |
| 43 for base_path in self._base_paths: | |
| 44 try: | |
| 45 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.
| |
| 46 # The APIs are stored in a list, so get the api out of the list. | |
| 47 loaded_api = json.loads(json_comment_eater.Nom(api))[0] | |
| 48 self._api_cache[key] = self._CachedAPI( | |
| 49 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
| |
| 50 time.time() + self._cache_timeout_seconds) | |
| 51 break | |
| 52 except: | |
| 53 pass | |
| 54 | |
| 55 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.
| |
| OLD | NEW |