feat: load configuration from TOML file for calendar settings
1 file changed, 28 insertions(+), 10 deletions(-)
changed files
M mycal.py → mycal.py
@@ -1,14 +1,33 @@ -import os from icalendar.cal import Calendar, Event import requests from flask import Flask from datetime import date, datetime, timedelta, time import zoneinfo +from dataclasses import dataclass +from danoan.toml_dataclass import TomlDataClassIO +from os import environ +import calendar + +@dataclass +class CalendarConfig(TomlDataClassIO): + file: str = "" + url: str = "" + +@dataclass +class Config(TomlDataClassIO): + name: str + timezone: str + calendar: CalendarConfig + +def load_config(file_path): + with open(file_path, "r") as fr: + return Config.read(fr) app = Flask(__name__) -user_name = (os.environ.get("NAME") or os.environ.get("USER") or "unknown").title() -tz = zoneinfo.ZoneInfo(os.environ.get("TZ") or "Europe/Berlin") +config = load_config(environ.get("CONFIG_FILE", "config.toml")) +user_name = config.name +tz = zoneinfo.ZoneInfo(config.timezone) def fetch_calendar(calendar_url): return requests.get(calendar_url).content@@ -18,14 +37,13 @@ with open(calendar_file, 'rb') as f: return f.read() def get_calendar(): - file = os.environ.get('CALENDAR_FILE') - if file is not None: - return read_calendar_file(file) + calendar_config = config.calendar + if calendar_config.file != "": + return read_calendar_file(calendar_config.file) else: - calendar_url = os.environ.get("CALENDAR_URL") - if calendar_url is None: - raise ValueError("CALENDAR_URL not set.") - return fetch_calendar(calendar_url) + if calendar_config.url != "": + return fetch_calendar(calendar_config.url) + raise ValueError("Calendar URL not configured.") def fixup_date(dt): if type(dt) == date: