diff --git a/.gitignore b/.gitignore index 8057dbb..b86fb2a 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,7 @@ venv/ # App cache/ +config/local.py # Pytest .pytest_cache/ diff --git a/app.py b/app.py index 2d2a2e2..04c2adc 100644 --- a/app.py +++ b/app.py @@ -3,6 +3,7 @@ Combine GWR Bristol→Paddington trains with Eurostar St Pancras→destination t """ from flask import Flask, render_template, redirect, url_for, request from datetime import date, timedelta +import os from cache import get_cached, set_cached import scraper.eurostar as eurostar_scraper @@ -22,7 +23,16 @@ RTT_BRISTOL_URL = ( "?stp=WVS&show=pax-calls&order=wtt" ) -app = Flask(__name__) +app = Flask(__name__, instance_relative_config=False) +app.config.from_object('config.default') +_local = os.path.join(os.path.dirname(__file__), 'config', 'local.py') +if os.path.exists(_local): + app.config.from_pyfile(_local) + +import cache +import circle_line +cache.CACHE_DIR = app.config['CACHE_DIR'] +circle_line._TXC_XML = app.config['CIRCLE_LINE_XML'] DESTINATIONS = { 'paris': 'Paris Gare du Nord', diff --git a/cache.py b/cache.py index 2e82500..31fad05 100644 --- a/cache.py +++ b/cache.py @@ -2,7 +2,7 @@ import json import os import time -CACHE_DIR = os.path.join(os.path.dirname(__file__), 'cache') +from config.default import CACHE_DIR # overridden by app config after import def _cache_path(key: str) -> str: diff --git a/circle_line.py b/circle_line.py index 1fee867..5c942f9 100644 --- a/circle_line.py +++ b/circle_line.py @@ -11,7 +11,7 @@ from datetime import datetime, timedelta _PAD_STOP = '9400ZZLUPAH1' # Paddington (H&C Line) _KXP_STOP = '9400ZZLUKSX3' # King's Cross St Pancras -_TXC_XML = os.path.join(os.path.dirname(__file__), 'output_txc_01CIR_.xml') +from config.default import CIRCLE_LINE_XML as _TXC_XML # overridden by app config after import _NS = {'t': 'http://www.transxchange.org.uk/'} # Populated on first call to next_service(); maps day-type -> sorted list of diff --git a/config/__init__.py b/config/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/config/default.py b/config/default.py new file mode 100644 index 0000000..aff1369 --- /dev/null +++ b/config/default.py @@ -0,0 +1,10 @@ +import os + +# Directory containing TfL reference data (TransXChange XML files etc.) +TFL_DATA_DIR = os.path.expanduser('~/lib/data/tfl') + +# Directory for caching scraped train times +CACHE_DIR = os.path.expanduser('~/lib/data/tfl/cache') + +# TransXChange timetable file for the Circle Line +CIRCLE_LINE_XML = os.path.join(TFL_DATA_DIR, 'output_txc_01CIR_.xml')