From d51a17dcfe241f4835a64ac0b965181a7abf217f Mon Sep 17 00:00:00 2001 From: Raphael Roberts Date: Mon, 15 Jul 2019 17:44:05 -0500 Subject: [PATCH] Added a __setup__ arg, because the recent list breaks if the buses aren't running --- ctabus/__init__.py | 1 + ctabus/fetch.py | 9 ++++++--- ctabus/internal/disk_cache.py | 7 ++++++- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/ctabus/__init__.py b/ctabus/__init__.py index bc961fb..f6f730d 100644 --- a/ctabus/__init__.py +++ b/ctabus/__init__.py @@ -299,6 +299,7 @@ def main(args=None): if not from_recent: recent_list.add(stop_id) data = fetch.get_times(stop_id) + fetch.get_data_from_stop_id(stop_id, __setup__=data) info = data["prd"][0] key = make_key(info["rt"], info["rtdir"], fetch.api, None) if key not in fetch.get_name_from_direction.cache.keys(): diff --git a/ctabus/fetch.py b/ctabus/fetch.py index 789b02d..d8b01c4 100644 --- a/ctabus/fetch.py +++ b/ctabus/fetch.py @@ -56,9 +56,12 @@ def get_name_from_direction(route, direction, api_key=api, timeout=None): return get_times(test_stop, api_key=api, timeout=timeout)["prd"][0]["des"] -@disk_cache -def get_data_from_stop_id(stop_id): - info = get_times(stop_id)["prd"][0] +@disk_cache(uses_setup=True) +def get_data_from_stop_id(stop_id, __setup__=None): + if __setup__ is None: + info = get_times(stop_id)["prd"][0] + else: + info = __setup__ ret = { "route_direction": info["rtdir"], "route_name": info["des"], diff --git a/ctabus/internal/disk_cache.py b/ctabus/internal/disk_cache.py index 90c06bd..5f054db 100644 --- a/ctabus/internal/disk_cache.py +++ b/ctabus/internal/disk_cache.py @@ -17,7 +17,7 @@ class disk_cache: caches = [] use_lzma = True - def __init__(self, func): + def __init__(self, func, uses_setup=False): if disk_cache.use_lzma: self.fname = "{}.{}.dc.lzma".format(func.__module__, func.__name__) else: @@ -25,16 +25,21 @@ class disk_cache: self.fname = os.path.join(cache_dir, self.fname) self.func = func + self.uses_setup = uses_setup self.load_cache() disk_cache.caches.append(self) def __call__(self, *args, **kwargs): + if self.uses_setup: + setup = kwargs.pop("__setup__", None) key = make_key(*args, **kwargs) try: res = self.cache[key] return res except KeyError: self.fresh = True + if self.uses_setup: + kwargs["__setup__"] = setup res = self.func(*args, **kwargs) self.cache[key] = res return res