Add config system with TFL_DATA_DIR and CACHE_DIR

config/default.py holds defaults using ~/lib/data/tfl (expanduser, so safe
to commit). app.py loads it then overlays config/local.py if present, pushing
paths into cache and circle_line modules. config/local.py is gitignored for
machine-specific absolute paths (e.g. on the server where www-data runs).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Edward Betts 2026-04-04 13:37:44 +01:00
parent c215456620
commit cd37f0619b
6 changed files with 24 additions and 3 deletions

1
.gitignore vendored
View file

@ -12,6 +12,7 @@ venv/
# App # App
cache/ cache/
config/local.py
# Pytest # Pytest
.pytest_cache/ .pytest_cache/

12
app.py
View file

@ -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 flask import Flask, render_template, redirect, url_for, request
from datetime import date, timedelta from datetime import date, timedelta
import os
from cache import get_cached, set_cached from cache import get_cached, set_cached
import scraper.eurostar as eurostar_scraper import scraper.eurostar as eurostar_scraper
@ -22,7 +23,16 @@ RTT_BRISTOL_URL = (
"?stp=WVS&show=pax-calls&order=wtt" "?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 = { DESTINATIONS = {
'paris': 'Paris Gare du Nord', 'paris': 'Paris Gare du Nord',

View file

@ -2,7 +2,7 @@ import json
import os import os
import time 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: def _cache_path(key: str) -> str:

View file

@ -11,7 +11,7 @@ from datetime import datetime, timedelta
_PAD_STOP = '9400ZZLUPAH1' # Paddington (H&C Line) _PAD_STOP = '9400ZZLUPAH1' # Paddington (H&C Line)
_KXP_STOP = '9400ZZLUKSX3' # King's Cross St Pancras _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/'} _NS = {'t': 'http://www.transxchange.org.uk/'}
# Populated on first call to next_service(); maps day-type -> sorted list of # Populated on first call to next_service(); maps day-type -> sorted list of

0
config/__init__.py Normal file
View file

10
config/default.py Normal file
View file

@ -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')