#!/usr/bin/python3 import sys import argparse import subprocess import os import mailbox import datetime import pytz from dateutil import parser as dateutilparser import generate class Actions: @staticmethod def clean_data(): open(config["top_mbox_file"], 'w').close() for top in config["pre_tops"]: if "file" in top and os.path.isfile(top["file"]): os.remove(top["file"]) for top in config["post_tops"]: if "file" in top and os.path.isfile(top["file"]): os.remove(top["file"]) @staticmethod def read_db(): subprocess.run(["helpers/read_db.sh"], check=True) @staticmethod def read_topmails(): in_mbox = mailbox.Maildir(config["top_inbox_maildir"]) out_mbox = mailbox.mbox(config["top_mbox_file"]) out_mbox.clear() buffer = [] timezone = pytz.timezone("Europe/Berlin") last_fsr_date = datetime.date.fromisoformat(open(config["last_date_file"]).read().strip()) last_fsr_datetime = datetime.datetime.combine(last_fsr_date, datetime.time(17, 30), timezone) for message in in_mbox: if message["List-Id"]: if config["top_list_id"] in generate.decode_header(message["List-Id"]).strip(): date = dateutilparser.parse(message["Date"]) if date > last_fsr_datetime: buffer.append(message) for message in sorted(buffer, key=lambda x: dateutilparser.parse(x["Date"])): out_mbox.add(message) out_mbox.close() @staticmethod def generate(*args): subprocess.run(["helpers/generate.py", "--config", cliargs.config, *args], check=True) @staticmethod def compile_presentation(): subprocess.run(["helpers/compile_presentation.sh"], check=True) @staticmethod def external(*args): subprocess.run(args, check=True) def dispatch(action, args=[]): if action in dir(Actions): getattr(Actions, action)(*args) elif action in config["sequencer"]: for item in config["sequencer"][action]: ilist = item.split() if not ilist[0] in cliargs.skip: dispatch(ilist[0], ilist[1:]) else: raise ValueError(f"Action {action} not found") if __name__ == "__main__": commandname = sys.argv[0] commandname = commandname.split("/")[-1] using_alias = commandname != "sequencer.py" parser = argparse.ArgumentParser() parser.add_argument("--config", "-c", default=generate.CONFIG_FILE) parser.add_argument("--skip", action='append') if not using_alias: parser.add_argument("action", nargs="?") cliargs = parser.parse_args() if cliargs.skip is None: cliargs.skip = [] config = generate.get_config(cliargs.config) if os.path.dirname(cliargs.config) != "": os.chdir(os.path.dirname(cliargs.config)) if using_alias: dispatch(commandname) elif cliargs.action: dispatch(cliargs.action) else: print("No action specified") sys.exit(1)