Browse Source

Added a __setup__ arg, because the recent list breaks if the buses aren't running

atexit
Raphael Roberts 6 years ago
parent
commit
d51a17dcfe
  1. 1
      ctabus/__init__.py
  2. 9
      ctabus/fetch.py
  3. 7
      ctabus/internal/disk_cache.py

1
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():

9
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"],

7
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

Loading…
Cancel
Save