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."""
import asyncio
import configparser
import os
import typing
from datetime import date, datetime, timedelta
import dateutil.rrule
import dateutil.tz
import flask
import holidays # type: ignore
import isodate # type: ignore
import lxml
@ -185,23 +185,11 @@ def read_events_yaml(data_dir: str, start: date, end: date) -> list[Event]:
return events
def get_config() -> configparser.ConfigParser:
"""Load config file."""
config_filename = os.path.join(os.path.dirname(__file__), "..", "config")
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]:
async def get_data(
now: datetime, config: flask.config.Config
) -> typing.Mapping[str, str | object]:
"""Get data to display on agenda dashboard."""
config = get_config()
data_dir = config.get("data", "dir")
data_dir = config["DATA_DIR"]
rocket_dir = os.path.join(data_dir, "thespacedevs")
today = now.date()
@ -237,7 +225,7 @@ async def get_data(now: datetime) -> typing.Mapping[str, str | object]:
"gwr_advance_tickets": gwr_advance_tickets,
}
my_data = config["data"]["personal-data"]
my_data = config["PERSONAL_DATA"]
events = (
[
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 += birthday.get_birthdays(last_year, os.path.join(my_data, "entities.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 += backwell_bins + bristol_bins
events += read_events_yaml(my_data, last_year, next_year)

View file

@ -1,19 +1,19 @@
"""Currency exchange rates."""
import configparser
import json
import os
import typing
from datetime import datetime, timedelta
from decimal import Decimal
import flask
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."""
access_key = config.get("exchangerate", "access_key")
data_dir = config.get("data", "dir")
access_key = config["EXCHANGERATE_ACCESS_KEY"]
data_dir = config["DATA_DIR"]
now = datetime.now()
now_str = now.strftime("%Y-%m-%d_%H:%M")

View file

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