rename some script
This commit is contained in:
113
serienbrief
Executable file
113
serienbrief
Executable file
@ -0,0 +1,113 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
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('--queryFile', '-q',
|
||||
help='query file, default is $pwd/serienbrief.sql',
|
||||
required=False,
|
||||
default='./serienbrief.sql')
|
||||
parser.add_argument('--templateFile', '-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.queryFile
|
||||
localConfig["templateFile"] = args.templateFile
|
||||
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