rework config mechanism
This commit is contained in:
@@ -11,14 +11,16 @@ import argparse
|
|||||||
import quopri
|
import quopri
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
|
|
||||||
|
import generate
|
||||||
|
|
||||||
CONFIG_FILE = "generator.conf"
|
CONFIG_FILE = "generator.conf"
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("--config", default=CONFIG_FILE)
|
parser.add_argument("--config", "-c", default=CONFIG_FILE)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
config = yaml.full_load(open(args.config))
|
config = generate.get_config(args.config)
|
||||||
|
|
||||||
open(config["top_mbox_file"], 'w').close()
|
open(config["top_mbox_file"], 'w').close()
|
||||||
|
|
||||||
|
|||||||
63
generate.py
63
generate.py
@@ -25,6 +25,9 @@ WEEKDAYS = { 0: "Montag",
|
|||||||
5: "Samstag",
|
5: "Samstag",
|
||||||
6: "Sonntag" }
|
6: "Sonntag" }
|
||||||
|
|
||||||
|
IMPORT_RE = re.compile(r"@import\((.*)\)")
|
||||||
|
FILE_RE = re.compile(r"@file\((.*)\)")
|
||||||
|
|
||||||
def decode_header(header):
|
def decode_header(header):
|
||||||
decoded_header = email.header.decode_header(header)[0]
|
decoded_header = email.header.decode_header(header)[0]
|
||||||
encoding = decoded_header[1] or "ascii"
|
encoding = decoded_header[1] or "ascii"
|
||||||
@@ -152,6 +155,49 @@ def conf2top(top):
|
|||||||
|
|
||||||
return Top(top["title"], sender, body, protostub)
|
return Top(top["title"], sender, body, protostub)
|
||||||
|
|
||||||
|
import_cache = {}
|
||||||
|
|
||||||
|
def get_imported_conf_entry(f, key):
|
||||||
|
if f not in import_cache:
|
||||||
|
import_cache[f] = yaml.safe_load(open(f))
|
||||||
|
return import_cache[f][key]
|
||||||
|
|
||||||
|
|
||||||
|
def do_imports(entry):
|
||||||
|
if isinstance(entry, dict):
|
||||||
|
d = entry.items()
|
||||||
|
result = {}
|
||||||
|
for key,value in d:
|
||||||
|
try:
|
||||||
|
result[key] = do_imports(value)
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
return result
|
||||||
|
|
||||||
|
if isinstance(entry, list):
|
||||||
|
l = iter(entry)
|
||||||
|
result = []
|
||||||
|
for item in l:
|
||||||
|
result.append(do_imports(item))
|
||||||
|
return result
|
||||||
|
|
||||||
|
if isinstance(entry, str):
|
||||||
|
match = IMPORT_RE.match(entry)
|
||||||
|
if match:
|
||||||
|
f, key = match.group(1).split(":")
|
||||||
|
return get_imported_conf_entry(f, key)
|
||||||
|
match = FILE_RE.match(entry)
|
||||||
|
if match:
|
||||||
|
with open(match.group(1)) as f:
|
||||||
|
return f.read().strip()
|
||||||
|
|
||||||
|
return entry
|
||||||
|
|
||||||
|
def get_config(f):
|
||||||
|
raw_config = yaml.safe_load(open(f))
|
||||||
|
config = do_imports(raw_config)
|
||||||
|
return config
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("--config", "-c", default=CONFIG_FILE)
|
parser.add_argument("--config", "-c", default=CONFIG_FILE)
|
||||||
@@ -161,24 +207,17 @@ if __name__ == "__main__":
|
|||||||
mode.add_argument("--presentation", action="store_true")
|
mode.add_argument("--presentation", action="store_true")
|
||||||
mode.add_argument("--protocol", action="store_true")
|
mode.add_argument("--protocol", action="store_true")
|
||||||
parser.add_argument("--debug", action="store_true", help=argparse.SUPPRESS)
|
parser.add_argument("--debug", action="store_true", help=argparse.SUPPRESS)
|
||||||
|
parser.add_argument("--print-config", action="store_true", help=argparse.SUPPRESS)
|
||||||
parser.add_argument("--write-mbox", action="store_true")
|
parser.add_argument("--write-mbox", action="store_true")
|
||||||
parser.add_argument("--send-mm", action="store_true")
|
parser.add_argument("--send-mm", action="store_true")
|
||||||
parser.add_argument("--send-mail", action="store_true")
|
parser.add_argument("--send-mail", action="store_true")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
config = yaml.safe_load(open(args.config))
|
config = get_config(args.config)
|
||||||
|
|
||||||
if config["redeleitung"]["name"].startswith("./"):
|
if args.print_config:
|
||||||
with open(config["redeleitung"]["name"]) as f:
|
pprint(config)
|
||||||
config["redeleitung"]["name"] = f.read().strip()
|
sys.exit(0)
|
||||||
|
|
||||||
if config["protokoll"]["name"].startswith("./"):
|
|
||||||
with open(config["protokoll"]["name"]) as f:
|
|
||||||
config["protokoll"]["name"] = f.read().strip()
|
|
||||||
|
|
||||||
if config["place"].startswith("./"):
|
|
||||||
with open(config["place"]) as f:
|
|
||||||
config["place"] = f.read().strip()
|
|
||||||
|
|
||||||
if args.invite:
|
if args.invite:
|
||||||
template_file = config["invite_template_file"]
|
template_file = config["invite_template_file"]
|
||||||
|
|||||||
@@ -13,7 +13,8 @@ import pytz
|
|||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
from dateutil import parser
|
from dateutil import parser
|
||||||
|
|
||||||
MAILDIR = "/home/yannik/mail/INBOX.Mailinglisten.FSMI/"
|
import generate
|
||||||
|
|
||||||
CONFIG_FILE = "generator.conf"
|
CONFIG_FILE = "generator.conf"
|
||||||
|
|
||||||
def decode_header(header):
|
def decode_header(header):
|
||||||
@@ -24,8 +25,12 @@ def decode_header(header):
|
|||||||
return decoded_header[0].decode(encoding, errors="replace") if isinstance(decoded_header[0], bytes) else decoded_header[0]
|
return decoded_header[0].decode(encoding, errors="replace") if isinstance(decoded_header[0], bytes) else decoded_header[0]
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
config = yaml.full_load(open(CONFIG_FILE))
|
parser = argparse.ArgumentParser()
|
||||||
in_mbox = mailbox.Maildir(MAILDIR)
|
parser.add_argument("--config", "-c", default=CONFIG_FILE)
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
config = generate.get_config(args.config)
|
||||||
|
in_mbox = mailbox.Maildir(config["top_inbox_maildir"])
|
||||||
out_mbox = mailbox.mbox(config["top_mbox_file"])
|
out_mbox = mailbox.mbox(config["top_mbox_file"])
|
||||||
|
|
||||||
out_mbox.clear()
|
out_mbox.clear()
|
||||||
@@ -33,7 +38,7 @@ if __name__ == "__main__":
|
|||||||
buffer = []
|
buffer = []
|
||||||
|
|
||||||
timezone = pytz.timezone("Europe/Berlin")
|
timezone = pytz.timezone("Europe/Berlin")
|
||||||
last_fsr_date = datetime.date.fromisoformat(open("data/last_date").read().strip())
|
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)
|
last_fsr_datetime = datetime.datetime.combine(last_fsr_date, datetime.time(17, 30), timezone)
|
||||||
|
|
||||||
for message in in_mbox:
|
for message in in_mbox:
|
||||||
|
|||||||
@@ -12,7 +12,9 @@ import quopri
|
|||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
from dateutil import parser
|
from dateutil import parser
|
||||||
|
|
||||||
MAILDIR = "/home/yannik/mail/INBOX.FSMI/"
|
import generate
|
||||||
|
|
||||||
|
CONFIG_FILE = "generator.conf"
|
||||||
|
|
||||||
def decode_header(header):
|
def decode_header(header):
|
||||||
decoded_header = email.header.decode_header(header)[0]
|
decoded_header = email.header.decode_header(header)[0]
|
||||||
@@ -22,7 +24,12 @@ def decode_header(header):
|
|||||||
return decoded_header[0].decode(encoding, errors="replace") if isinstance(decoded_header[0], bytes) else decoded_header[0]
|
return decoded_header[0].decode(encoding, errors="replace") if isinstance(decoded_header[0], bytes) else decoded_header[0]
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
mbox = mailbox.Maildir(MAILDIR)
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument("--config", "-c", default=CONFIG_FILE)
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
config = generate.get_config(args.config)
|
||||||
|
mbox = mailbox.Maildir(config["ubmails_inbox_maildir"])
|
||||||
|
|
||||||
latest = None
|
latest = None
|
||||||
latest_date = None
|
latest_date = None
|
||||||
|
|||||||
Reference in New Issue
Block a user