Use flask config instead of configparser

Closes: #74
This commit is contained in:
Edward Betts 2023-12-07 15:52:48 +00:00
parent 8cf0b982f0
commit 9ff61c3af8
3 changed files with 15 additions and 28 deletions

View file

@ -1,13 +1,13 @@
"""Agenda data.""" """Agenda data."""
import asyncio import asyncio
import configparser
import os import os
import typing import typing
from datetime import date, datetime, timedelta from datetime import date, datetime, timedelta
import dateutil.rrule import dateutil.rrule
import dateutil.tz import dateutil.tz
import flask
import holidays # type: ignore import holidays # type: ignore
import isodate # type: ignore import isodate # type: ignore
import lxml import lxml
@ -185,23 +185,11 @@ def read_events_yaml(data_dir: str, start: date, end: date) -> list[Event]:
return events return events
def get_config() -> configparser.ConfigParser: async def get_data(
"""Load config file.""" now: datetime, config: flask.config.Config
config_filename = os.path.join(os.path.dirname(__file__), "..", "config") ) -> typing.Mapping[str, str | object]:
assert os.path.exists(config_filename)
config = configparser.ConfigParser()
config.read(config_filename)
return config
async def get_data(now: datetime) -> typing.Mapping[str, str | object]:
"""Get data to display on agenda dashboard.""" """Get data to display on agenda dashboard."""
config = get_config() data_dir = config["DATA_DIR"]
data_dir = config.get("data", "dir")
rocket_dir = os.path.join(data_dir, "thespacedevs") rocket_dir = os.path.join(data_dir, "thespacedevs")
today = now.date() today = now.date()
@ -237,7 +225,7 @@ async def get_data(now: datetime) -> typing.Mapping[str, str | object]:
"gwr_advance_tickets": gwr_advance_tickets, "gwr_advance_tickets": gwr_advance_tickets,
} }
my_data = config["data"]["personal-data"] my_data = config["PERSONAL_DATA"]
events = ( events = (
[ [
Event(name="mothers_day", date=uk_holiday.get_mothers_day(today)), Event(name="mothers_day", date=uk_holiday.get_mothers_day(today)),
@ -254,7 +242,7 @@ async def get_data(now: datetime) -> typing.Mapping[str, str | object]:
events += combine_holidays(bank_holiday + get_us_holidays(last_year, next_year)) events += combine_holidays(bank_holiday + get_us_holidays(last_year, next_year))
events += birthday.get_birthdays(last_year, os.path.join(my_data, "entities.yaml")) events += birthday.get_birthdays(last_year, os.path.join(my_data, "entities.yaml"))
events += accommodation.get_events(os.path.join(my_data, "accommodation.yaml")) events += accommodation.get_events(os.path.join(my_data, "accommodation.yaml"))
events += travel.all_events(config["data"]["personal-data"]) events += travel.all_events(my_data)
events += conference.get_list(os.path.join(my_data, "conferences.yaml")) events += conference.get_list(os.path.join(my_data, "conferences.yaml"))
events += backwell_bins + bristol_bins events += backwell_bins + bristol_bins
events += read_events_yaml(my_data, last_year, next_year) events += read_events_yaml(my_data, last_year, next_year)

View file

@ -1,19 +1,19 @@
"""Currency exchange rates.""" """Currency exchange rates."""
import configparser
import json import json
import os import os
import typing import typing
from datetime import datetime, timedelta from datetime import datetime, timedelta
from decimal import Decimal from decimal import Decimal
import flask
import httpx import httpx
async def get_gbpusd(config: configparser.ConfigParser) -> Decimal: async def get_gbpusd(config: flask.config.Config) -> Decimal:
"""Get the current value for GBPUSD, with caching.""" """Get the current value for GBPUSD, with caching."""
access_key = config.get("exchangerate", "access_key") access_key = config["EXCHANGERATE_ACCESS_KEY"]
data_dir = config.get("data", "dir") data_dir = config["DATA_DIR"]
now = datetime.now() now = datetime.now()
now_str = now.strftime("%Y-%m-%d_%H:%M") now_str = now.strftime("%Y-%m-%d_%H:%M")

View file

@ -19,6 +19,7 @@ import agenda.travel
app = flask.Flask(__name__) app = flask.Flask(__name__)
app.debug = False app.debug = False
app.config.from_object("config.default")
@app.errorhandler(werkzeug.exceptions.InternalServerError) @app.errorhandler(werkzeug.exceptions.InternalServerError)
@ -52,7 +53,7 @@ def exception_handler(e: werkzeug.exceptions.InternalServerError) -> tuple[str,
async def index() -> str: async def index() -> str:
"""Index page.""" """Index page."""
now = datetime.now() now = datetime.now()
data = await agenda.data.get_data(now) data = await agenda.data.get_data(now, app.config)
return flask.render_template("index.html", today=now.date(), **data) return flask.render_template("index.html", today=now.date(), **data)
@ -60,8 +61,7 @@ async def index() -> str:
@app.route("/travel") @app.route("/travel")
def travel_list() -> str: def travel_list() -> str:
"""Page showing a list of upcoming travel.""" """Page showing a list of upcoming travel."""
config = agenda.data.get_config() data_dir = app.config["PERSONAL_DATA"]
data_dir = config["data"]["personal-data"]
flights = agenda.travel.parse_yaml("flights", data_dir) flights = agenda.travel.parse_yaml("flights", data_dir)
trains = agenda.travel.parse_yaml("trains", data_dir) trains = agenda.travel.parse_yaml("trains", data_dir)
@ -76,8 +76,7 @@ def as_date(d: date | datetime) -> date:
@app.route("/conference") @app.route("/conference")
def conference_list() -> str: def conference_list() -> str:
"""Page showing a list of conferences.""" """Page showing a list of conferences."""
config = agenda.data.get_config() data_dir = app.config["PERSONAL_DATA"]
data_dir = config["data"]["personal-data"]
filepath = os.path.join(data_dir, "conferences.yaml") filepath = os.path.join(data_dir, "conferences.yaml")
item_list = yaml.safe_load(open(filepath))["conferences"] item_list = yaml.safe_load(open(filepath))["conferences"]
today = date.today() today = date.today()