initial
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
*.pyc
|
22
example.sql
Normal file
22
example.sql
Normal file
@ -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'
|
||||||
|
|
8
example.tmpl
Normal file
8
example.tmpl
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
$objekt
|
||||||
|
$wohnung
|
||||||
|
$anrede
|
||||||
|
|
||||||
|
$vorname $nachname
|
||||||
|
$strasse
|
||||||
|
$plz $ort
|
||||||
|
|
11
serienbrief.ini
Normal file
11
serienbrief.ini
Normal file
@ -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
|
111
serienbrief.py
Normal file
111
serienbrief.py
Normal file
@ -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))
|
Reference in New Issue
Block a user