#!/usr/bin/python3 """Web page to show upcoming events.""" import inspect import os.path import sys import traceback from datetime import date, datetime import flask import werkzeug import werkzeug.debug.tbtools import yaml import agenda.data import agenda.travel app = flask.Flask(__name__) app.debug = False @app.errorhandler(werkzeug.exceptions.InternalServerError) def exception_handler(e: werkzeug.exceptions.InternalServerError) -> tuple[str, int]: """Handle exception.""" exec_type, exc_value, current_traceback = sys.exc_info() assert exc_value tb = werkzeug.debug.tbtools.DebugTraceback(exc_value) summary = tb.render_traceback_html(include_title=False) exc_lines = "".join(tb._te.format_exception_only()) last_frame = list(traceback.walk_tb(current_traceback))[-1][0] last_frame_args = inspect.getargs(last_frame.f_code) return ( flask.render_template( "show_error.html", plaintext=tb.render_traceback_text(), exception=exc_lines, exception_type=tb._te.exc_type.__name__, summary=summary, last_frame=last_frame, last_frame_args=last_frame_args, ), 500, ) @app.route("/") async def index() -> str: """Index page.""" now = datetime.now() data = await agenda.data.get_data(now) return flask.render_template("index.html", today=now.date(), **data) @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"] flights = agenda.travel.parse_yaml("flights", data_dir) trains = agenda.travel.parse_yaml("trains", data_dir) return flask.render_template("travel.html", flights=flights, trains=trains) def as_date(d: date | datetime) -> date: """Date of event.""" return d.date() if isinstance(d, datetime) else d @app.route("/conference") def conference_list() -> str: """Page showing a list of conferences.""" config = agenda.data.get_config() data_dir = config["data"]["personal-data"] filepath = os.path.join(data_dir, "conferences.yaml") item_list = yaml.safe_load(open(filepath))["conferences"] item_list.sort(key=lambda conf: as_date(conf["start"])) return flask.render_template("conference_list.html", item_list=item_list) if __name__ == "__main__": app.run(host="0.0.0.0")