From d40aeb7d0436a8fa2d912f9e81c3a98cc87d216a Mon Sep 17 00:00:00 2001 From: Raphael Roberts Date: Fri, 26 Apr 2019 13:35:30 -0500 Subject: [PATCH] Fixed numerous bugs that stopped the interactive script from working --- ctabus/__init__.py | 32 +++++++++++++++++--------------- ctabus/internal/config.py | 8 +++++--- ctabus/internal/disk_cache.py | 6 ++---- setup.py | 6 +++--- 4 files changed, 27 insertions(+), 25 deletions(-) diff --git a/ctabus/__init__.py b/ctabus/__init__.py index 5a1c0a5..7f53a58 100644 --- a/ctabus/__init__.py +++ b/ctabus/__init__.py @@ -13,6 +13,7 @@ import os.path as osp import sys import shutil +from ctabus.internal.config import log_dir from ctabus.internal.disk_cache import disk_cache, make_key from ctabus import ctabus @@ -89,7 +90,7 @@ def pprint_delta(delta): def gen_list(objs, data, *displays, key=None, sort=0, num_pic=True): - from print2d import create_table, render_table + from ctabus.internal.print2d import create_table, render_table # sort based on column number k = displays[sort] display_data = {obj[k]: obj[data] for obj in objs} @@ -166,7 +167,7 @@ def show(data, rt_filter=None, _clear=False, enable_toast=False): def _picker(args): # save on import time slightly - from search import Search, StopSearch + from ctabus.internal.search import Search, StopSearch # routes if not args.route: data = ctabus.get_routes()['routes'] @@ -205,26 +206,27 @@ def _main_periodic(args, stop_id, init_data): data = init_data while not _done: try: - show(data, args.route, True, args.disable_toast and HAS_TOAST) - s = time.perf_counter() - timeout = 1 - if args.periodic > timeout: - timeout = args.periodic - data = ctabus.get_times(stop_id, timeout=timeout) - e = time.perf_counter() - s + try: + show(data, args.route, True, args.disable_toast and HAS_TOAST) + s = time.perf_counter() + timeout = 1 + if args.periodic > timeout: + timeout = args.periodic + data = ctabus.get_times(stop_id, timeout=timeout) + e = time.perf_counter() - s + except (urllib.error.URLError, socket.timeout): + e = time.perf_counter() - s + print("Error fetching times") + if e < args.periodic: + time.sleep(args.periodic-e) except KeyboardInterrupt: _done = True - except (urllib.error.URLError, socket.timeout): - e = time.perf_counter() - s - print("Error fetching times") - if e < args.periodic: - time.sleep(args.periodic-e) def main(args=None): if args is None: args = parser.parse_args() - sys.stderr = open(osp.join(osp.dirname(__file__), 'stderr.log'), 'w') + sys.stderr = open(osp.join(log_dir, 'stderr.log'), 'w') if args.kill_cache: for cache_obj in disk_cache.caches: cache_obj.delete_cache() diff --git a/ctabus/internal/config.py b/ctabus/internal/config.py index d3bea85..845f05d 100644 --- a/ctabus/internal/config.py +++ b/ctabus/internal/config.py @@ -4,12 +4,14 @@ import appdirs app_dirs = appdirs.AppDirs('ctabus') config_dir = app_dirs.user_config_dir -cache_path = app_dirs.user_cache_dir +cache_dir = app_dirs.user_cache_dir +log_dir = app_dirs.user_log_dir +for dir in (config_dir, cache_dir, log_dir): + if not os.path.exists(dir): + os.makedirs(dir) try: with open(os.path.join(config_dir, 'api.txt')) as file: API_KEY = file.read().rstrip() except FileNotFoundError: - if not os.path.exists(config_dir): - os.makedirs(config_dir) raise FileNotFoundError("Please place your CTA Bus Tracker api key in a text file located at '{}'".format( os.path.join(config_dir, 'api.txt'))) diff --git a/ctabus/internal/disk_cache.py b/ctabus/internal/disk_cache.py index 10b1c2c..db5960f 100644 --- a/ctabus/internal/disk_cache.py +++ b/ctabus/internal/disk_cache.py @@ -1,9 +1,7 @@ import pickle import os import lzma -from ctabus.internal.config import cache_path -if not os.path.exists(cache_path): - os.mkdir(cache_path) +from ctabus.internal.config import cache_dir def make_key(*args, **kwargs): @@ -18,7 +16,7 @@ class disk_cache: def __init__(self, func): self.fname = "{}.{}.dc".format(func.__module__, func.__name__) - self.fname = os.path.join(cache_path, self.fname) + self.fname = os.path.join(cache_dir, self.fname) self.func = func self.load_cache() disk_cache.caches.append(self) diff --git a/setup.py b/setup.py index 4acf4c0..d2cf498 100644 --- a/setup.py +++ b/setup.py @@ -1,16 +1,16 @@ -from setuptools import setup +from setuptools import setup, find_packages with open('requirements.txt') as file: INSTALL_REQUIRES = file.read().rstrip().split('\n') setup( name='ctabus', - version='1.0', + version='1.0.1', description='Python package for tracking cta bus times', install_requires=INSTALL_REQUIRES, author='rlbr', author_email='raphael.roberts48@gmail.com', - packages=['ctabus'], + packages=find_packages(), entry_points={ 'console_scripts': ['ctabus=ctabus:main'] }