Implement configuration file support

This commit is contained in:
Fritz Grimpen 2024-10-09 20:44:57 +00:00
parent 30a0d9a892
commit 76c1c40362

View file

@ -11,6 +11,7 @@ import operator
import pathlib
import subprocess
import tempfile
import tomllib
import babel.numbers
import dateutil.relativedelta
@ -68,7 +69,10 @@ def fee_invoice(env: Env):
invoicing_client = triad.client.InvoicingClient.from_session(env.session)
template = env.jinja2_env.get_template("beitrag.eml")
for member in csvreader:
invoice = invoicing_client.get_invoice_by_id(member['invoice_id'])
try:
invoice = invoicing_client.Invoice.find([(env.args.lookup_key, "=", str(member['invoice_id'])), ("type", "=", "out")])[0]
except IndexError:
continue
year = invoice.invoice_date or env.args.year
months = [datetime.date(year.year, month, 1) for month in range(1, 13)]
email_address = member.get('email') or invoice.party.email
@ -178,6 +182,8 @@ def main():
parser.add_argument("-O", "--output-dir", help="email output directory", default=".", type=pathlib.Path)
parser.add_argument("-T", "--template-dir", help="email template directory", default="mail_templates")
parser.add_argument("-c", "--config", type=argparse.FileType("rb"))
# Email arguments
parser.add_argument('--email-sender', help='email signature sender')
parser.add_argument("--email-from", help="email from header")
@ -190,6 +196,7 @@ def main():
fee_invoice_parser.add_argument("-F", "--csv-fields", type=lambda f: f.split(","), help="csv fields")
fee_invoice_parser.add_argument("-y", "--year", type=lambda y: datetime.date(int(y), 1, 1))
fee_invoice_parser.add_argument("--months", help="Beitragsmonate", type=lambda m: m.split(","), default="1,2,3,4,5,6,7,8,9,10,11,12")
fee_invoice_parser.add_argument("-k", "--lookup-key", choices={'number', 'id'}, default='id', help='lookup key for invoice')
fee_invoice_parser.add_argument("csvfile", type=argparse.FileType("r"), default="-", nargs="?")
fee_invoice_parser.set_defaults(func=fee_invoice)
@ -213,6 +220,22 @@ def main():
env.jinja2_env.filters["format_quantity"] = format_quantity
env.args = args
if args.config:
env.config = tomllib.load(args.config)
args.config.close()
if "tryton" in env.config:
if "uri" in env.config["tryton"]:
env.args.uri = env.config["tryton"]["uri"]
if "username" in env.config["tryton"]:
env.args.username = env.config["tryton"]["username"]
if "password" in env.config["tryton"]:
env.args.password = env.config["tryton"]["password"]
if "email" in env.config:
if "sender" in env.config["email"]:
env.args.email_sender = env.config["email"]["sender"]
if "from" in env.config["email"]:
env.args.email_from = env.config["email"]["from"]
if not args.uri:
args.uri = input("URI for Tryton: ")