major refactoring

* move all scripts not intended to be called directly to helpers/ dir
* introduce sequencer.py as replacement for various scripts
* introduce --save option to generate.py
* other smaller changes/bugfixes
This commit is contained in:
2022-03-09 15:52:02 +01:00
parent da116d7b01
commit 151f11d90b
18 changed files with 164 additions and 125 deletions

101
helpers/sequencer.py Executable file
View File

@@ -0,0 +1,101 @@
#!/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)
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__":
parser = argparse.ArgumentParser()
parser.add_argument("--config", "-c", default=generate.CONFIG_FILE)
parser.add_argument("--skip", action='append')
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))
commandname = sys.argv[0]
commandname = commandname.split("/")[-1]
if commandname != "sequencer.py":
dispatch(commandname)
elif cliargs.action:
dispatch(cliargs.action)
else:
print("No action specified")
sys.exit(1)