From acc8d0b450019c415a8d2bb8f5e411dc21079db2 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Wed, 5 Apr 2023 14:33:00 +0200 Subject: support a output header and make the formatted events available to the format I use this oputput configuration: ```toml output_header = """ Woche Datum LP-Abschnitt Std. LP-A. Unterrichtseinheit ----- --------- ------------ ---------- ------------------------""" fmt = "{week:>5} {date.strftime('%d.%m'):>9} {' '*12} {' '*10} {period_events}" ``` To produce a formatted pandoc simple table looking like this: Woche Datum LP-Abschnitt Std. LP-A. Unterrichtseinheit ----- --------- ------------ ---------- ------------------------ 16 18.04 Event1 & Event2 17 25.04 Event3 18 02.05 Event3 & Event4 ... --- lessons.py | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/lessons.py b/lessons.py index ea17e06..75283ac 100755 --- a/lessons.py +++ b/lessons.py @@ -75,26 +75,29 @@ def load_events(config: dict) -> list[icalevents.icalparser.Event]: def format_period(period: webuntis.objects.PeriodObject, evs: list[icalevents.icalparser.Event], config: dict) -> str: """Format a given period""" - # Provide some usefull shortcuts for the output format - day = period.start.date() - week = day.isocalendar().week # pylint: disable=unused-variable - - output = config.get('fmt') or '{week} {day.strftime(\'%d.%m\')}' - # Using eval here instead of string.format() is potentially dangerous. - # However, I do not consider this an actual thread because this is an - # obscure script run with only trusted inputs (cli arguments, manual wirtten - # config), therefore potential code execution is not a real concern. - output = eval(F'f"{output}"') # pylint: disable=eval-used + # Provide some useful shortcuts for the output format + date = period.start.date() + week = date.isocalendar().week # pylint: disable=unused-variable + period_events = "" period_evs = [ ev for ev in evs if ev.start.timestamp() <= period.start.timestamp() <= ev.end.timestamp() ] if period_evs: for event in period_evs: - output += f' {event.summary} &' + period_events += f' {event.summary} &' # strip the trailing ' &' suffix - output = output[:-2] + period_events = period_events[:-2] + + output = config.get( + 'fmt') or '{week} {date.strftime(\'%d.%m\')} {period_events}' + # Using eval here instead of string.format() is potentially dangerous. + # However, I do not consider this an actual thread because this is an + # obscure script run with only trusted inputs (cli arguments, manual written + # config), therefore potential code execution is not a real concern. + output = eval(F'f"{output}"') # pylint: disable=eval-used + return output @@ -166,6 +169,9 @@ def main(): if args.show_events: evs = load_events(config) + if 'output_header' in config: + print(config['output_header']) + for period in filter_periods(timetable, config): if period.code: print(f'WARNING: period {period.start} is {period.code}', -- cgit v1.2.3