allow package specific actions

This commit is contained in:
Yannik Enss
2022-03-16 14:24:27 +01:00
parent b3dfd2f59e
commit eab2a0d976
4 changed files with 64 additions and 60 deletions

49
fsmi_fsr/actions.py Normal file
View File

@@ -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)

View File

@@ -137,5 +137,6 @@ Beschlussvorlage"
sendmail: ["/usr/sbin/sendmail"] sendmail: ["/usr/sbin/sendmail"]
override_file: "@import(personal.conf:override_file)" override_file: "@import(personal.conf:override_file)"
package_actions_file: "actions.py"
# vim: filetype=yaml # vim: filetype=yaml

View File

@@ -3,73 +3,25 @@ import sys
import argparse import argparse
import subprocess import subprocess
import os import os
import mailbox import importlib.machinery
import datetime
import pytz
from dateutil import parser as dateutilparser
import generate import generate
import util import util
class Actions: class BuiltinActions:
@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 @staticmethod
def generate(*args): def generate(*args):
generate.main(["--config", config_path, *args]) generate.main(["--config", config_path, *args])
@staticmethod
def compile_presentation():
subprocess.run(["helpers/compile_presentation.sh"], check=True)
@staticmethod @staticmethod
def external(*args): def external(*args):
subprocess.run(args, check=True) subprocess.run(args, check=True)
def dispatch(action, args=[]): def dispatch(action, args=[]):
if action in dir(Actions): if package_actions and action in dir(package_actions):
getattr(Actions, action)(*args) getattr(package_actions, action)(*args)
elif action in dir(BuiltinActions):
getattr(BuiltinActions, action)(*args)
elif action in config["sequencer"]: elif action in config["sequencer"]:
for item in config["sequencer"][action]: for item in config["sequencer"][action]:
ilist = item.split() ilist = item.split()
@@ -79,8 +31,7 @@ def dispatch(action, args=[]):
raise ValueError(f"Action {action} not found") raise ValueError(f"Action {action} not found")
if __name__ == "__main__": if __name__ == "__main__":
commandname = sys.argv[0] commandname = sys.argv[0].split("/")[-1]
commandname = commandname.split("/")[-1]
using_alias = commandname != "sequencer.py" using_alias = commandname != "sequencer.py"
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
@@ -96,8 +47,6 @@ if __name__ == "__main__":
config_path = util.get_normalized_config_path(cliargs.config) config_path = util.get_normalized_config_path(cliargs.config)
config_path = os.path.abspath(config_path) config_path = os.path.abspath(config_path)
config = util.get_config(config_path)
original_working_dir = os.getcwd() original_working_dir = os.getcwd()
normalized_working_dir = original_working_dir normalized_working_dir = original_working_dir
@@ -105,6 +54,12 @@ if __name__ == "__main__":
normalized_working_dir = os.path.dirname(config_path) normalized_working_dir = os.path.dirname(config_path)
os.chdir(normalized_working_dir) 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: if using_alias:
dispatch(commandname) dispatch(commandname)
elif cliargs.action: elif cliargs.action:
@@ -112,4 +67,3 @@ if __name__ == "__main__":
else: else:
print("No action specified") print("No action specified")
sys.exit(1) sys.exit(1)

View File

@@ -19,7 +19,7 @@ from pathlib import Path
IMPORT_RE = re.compile(r"@import\((.*)\)") IMPORT_RE = re.compile(r"@import\((.*)\)")
FILE_RE = re.compile(r"@file\((.*)\)") FILE_RE = re.compile(r"@file\((.*)\)")
CONFIG_FILE = "fsmi_fsr/generator.conf" CONFIG_FILE = "fsmi_fsr/"
WEEKDAYS = { 0: "Montag", WEEKDAYS = { 0: "Montag",
1: "Dienstag", 1: "Dienstag",