diff --git a/fsmi_fsr/actions.py b/fsmi_fsr/actions.py new file mode 100644 index 0000000..ec3cc42 --- /dev/null +++ b/fsmi_fsr/actions.py @@ -0,0 +1,49 @@ +import subprocess +import os +import mailbox +import datetime + +import pytz +from dateutil import parser as dateutilparser + +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"]) + +def read_db(): + subprocess.run(["helpers/read_db.sh"], check=True) + +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() + +def compile_presentation(): + subprocess.run(["helpers/compile_presentation.sh"], check=True) diff --git a/fsmi_fsr/generator.conf b/fsmi_fsr/generator.conf index 7fddb10..d211648 100644 --- a/fsmi_fsr/generator.conf +++ b/fsmi_fsr/generator.conf @@ -137,5 +137,6 @@ Beschlussvorlage" sendmail: ["/usr/sbin/sendmail"] override_file: "@import(personal.conf:override_file)" +package_actions_file: "actions.py" # vim: filetype=yaml diff --git a/helpers/sequencer.py b/helpers/sequencer.py index ee07cbe..f4946ca 100755 --- a/helpers/sequencer.py +++ b/helpers/sequencer.py @@ -3,73 +3,25 @@ import sys import argparse import subprocess import os -import mailbox -import datetime - -import pytz -from dateutil import parser as dateutilparser +import importlib.machinery import generate import util -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() - +class BuiltinActions: @staticmethod def generate(*args): generate.main(["--config", config_path, *args]) - @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) + if package_actions and action in dir(package_actions): + getattr(package_actions, action)(*args) + elif action in dir(BuiltinActions): + getattr(BuiltinActions, action)(*args) elif action in config["sequencer"]: for item in config["sequencer"][action]: ilist = item.split() @@ -79,8 +31,7 @@ def dispatch(action, args=[]): raise ValueError(f"Action {action} not found") if __name__ == "__main__": - commandname = sys.argv[0] - commandname = commandname.split("/")[-1] + commandname = sys.argv[0].split("/")[-1] using_alias = commandname != "sequencer.py" parser = argparse.ArgumentParser() @@ -96,8 +47,6 @@ if __name__ == "__main__": config_path = util.get_normalized_config_path(cliargs.config) config_path = os.path.abspath(config_path) - config = util.get_config(config_path) - original_working_dir = os.getcwd() normalized_working_dir = original_working_dir @@ -105,6 +54,12 @@ if __name__ == "__main__": normalized_working_dir = os.path.dirname(config_path) os.chdir(normalized_working_dir) + config = util.get_config(config_path) + + package_actions = None + if "package_actions_file" in config: + package_actions = importlib.machinery.SourceFileLoader("package_actions",config["package_actions_file"]).load_module() + if using_alias: dispatch(commandname) elif cliargs.action: @@ -112,4 +67,3 @@ if __name__ == "__main__": else: print("No action specified") sys.exit(1) - diff --git a/helpers/util.py b/helpers/util.py index 58292ff..63b3b2a 100755 --- a/helpers/util.py +++ b/helpers/util.py @@ -19,7 +19,7 @@ from pathlib import Path IMPORT_RE = re.compile(r"@import\((.*)\)") FILE_RE = re.compile(r"@file\((.*)\)") -CONFIG_FILE = "fsmi_fsr/generator.conf" +CONFIG_FILE = "fsmi_fsr/" WEEKDAYS = { 0: "Montag", 1: "Dienstag",