commit a376ec8d942c17aa08b90bb62f81b2ebf7374cca Author: Wolfgang Hottgenroth Date: Sat Jan 23 21:55:03 2021 +0100 initial diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0d20b64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.pyc diff --git a/example.sql b/example.sql new file mode 100644 index 0000000..3b68085 --- /dev/null +++ b/example.sql @@ -0,0 +1,22 @@ +select objekt, + gesamt_flaeche, + wohnung, + flaeche, + anrede, + vorname, + nachname, + strasse, + plz, + ort, + nutzungszeit, + anteil_pro_flaeche, + anteil_pro_flaeche_nutzungszeit, + summe_zahlungen, + summe_zahlungen_anteil_miete, + vorauszahlung, + nachzahlung, + jahr, + gesamt_betriebskosten + from jahresabrechnung_flat_v + where jahr = '2020' + \ No newline at end of file diff --git a/example.tmpl b/example.tmpl new file mode 100644 index 0000000..c6f9744 --- /dev/null +++ b/example.tmpl @@ -0,0 +1,8 @@ +$objekt +$wohnung +$anrede + +$vorname $nachname +$strasse +$plz $ort + diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..fcc7dcd --- /dev/null +++ b/readme.md @@ -0,0 +1,2 @@ +* requires mariadb +* required cheetah3 diff --git a/serienbrief.ini b/serienbrief.ini new file mode 100644 index 0000000..348792f --- /dev/null +++ b/serienbrief.ini @@ -0,0 +1,11 @@ +[database] +host = 172.16.10.18 +user = hv-serienbrief +pass = test123 +name = hausverwaltung + +[config] +queryFile = example.sql +templateFile = example.tmpl +outputPrefix = jahresabrechnung +outputSuffix = tex diff --git a/serienbrief.py b/serienbrief.py new file mode 100644 index 0000000..9be58cf --- /dev/null +++ b/serienbrief.py @@ -0,0 +1,111 @@ +import argparse +import configparser +import mariadb +import json +from Cheetah.Template import Template + +parser = argparse.ArgumentParser(description='Serienbrief') +parser.add_argument('--config', '-f', + help='Config file, default is $pwd/serienbrief.ini', + required=False, + default='./serienbrief.ini') +parser.add_argument('--query', '-q', + help='query file, default is $pwd/serienbrief.sql', + required=False, + default='./serienbrief.sql') +parser.add_argument('--template', '-t', + help='template file, default is $pwd/serienbrief.tmpl', + required=False, + default='./serienbrief.tmpl') +parser.add_argument('--outputPrefix', '-p', + help='prefix for the output file, will be extended by an auto increment number', + required=False, + default='output') +parser.add_argument('--outputSuffix', '-s', + help='suffix for the output file', + required=False, + default='txt') +parser.add_argument('--verbose', '-v', + help='verbose output', + required=False, + action='store_true', + default=False) +args = parser.parse_args() + +configFile = args.config + +localConfig = {} +localConfig["queryFile"] = args.query +localConfig["templateFile"] = args.template +localConfig["outputPrefix"] = args.outputPrefix +localConfig["outputSuffix"] = args.outputSuffix +verbose = args.verbose + +config = configparser.ConfigParser() +config.read(configFile) + +try: + dbhost = config["database"]["host"] + dbuser = config["database"]["user"] + dbpass = config["database"]["pass"] + dbname = config["database"]["name"] + + if "config" in config: + for option in [ "queryFile", "templateFile", "outputPrefix", "outputSuffix" ]: + if option in config["config"]: + localConfig[option] = config["config"][option] +except KeyError as err: + raise Exception("ERROR: problems when loading database configuration: {}".format(err)) + + +# --- load query ------------------------------ +query = '' +with open(localConfig["queryFile"]) as f: + query = f.readlines() +query = ''.join([x.strip('\n') for x in query]) +if verbose: + print("Query loaded: {}".format(query)) + +# --- load data from database +conn = None +cursor = None +loadedData = [] +try: + conn = mariadb.connect( + user = dbuser, + password = dbpass, + host = dbhost, + database = dbname + ) + conn.autocommit = False + + cursor = conn.cursor(dictionary=True) + cursor.execute(query) + loadedData = [ x for x in cursor ] + + if verbose: + print("Loaded data:") + print(loadedData) + +except mariadb.Error as err: + raise Exception("Error when accessing database: {}".format(err)) +finally: + if cursor: + cursor.close() + if conn: + conn.rollback() + conn.close() + + +# --- process and output template +num = 1 +for item in loadedData: + if verbose: + print("Processing item: {}".format(item)) + outputFile = "{}-{}.{}".format(localConfig["outputPrefix"], str(num), localConfig["outputSuffix"]) + num += 1 + tmpl = Template(file=localConfig["templateFile"], searchList=[ item ]) + if verbose: + print(tmpl) + with open(outputFile, 'w') as f: + f.write(str(tmpl))