add options iOP

This commit is contained in:
Christoph Holtermann 2018-09-06 13:15:20 +02:00 committed by c-holtermann
parent 61f7613b8d
commit 002595f4cc

View File

@ -32,6 +32,7 @@
try: try:
import locale import locale
import os
import sys import sys
import getopt import getopt
import gnucash import gnucash
@ -60,9 +61,12 @@ def main(argv=None):
list_invoices = False list_invoices = False
invoice_number = None invoice_number = None
invoice_id = None invoice_id = None
filename_from_invoice = False
output_path = None
with_ipshell = False
try: try:
opts, args = getopt.getopt(argv[1:], "fhlI:t:o:", ["help"]) opts, args = getopt.getopt(argv[1:], "fhliI:t:o:OP:", ["help"])
except getopt.error as msg: except getopt.error as msg:
raise Usage(msg) raise Usage(msg)
@ -74,16 +78,27 @@ def main(argv=None):
raise Usage("Help:") raise Usage("Help:")
if opt[0] in ["-I"]: if opt[0] in ["-I"]:
invoice_id = opt[1] invoice_id = opt[1]
print("using invoice ID '" + str(invoice_id) + "'.") print ("using invoice ID '" + str(invoice_id) + "'.")
if opt[0] in ["-i"]:
print ("Using ipshell")
with_ipshell = True
if opt[0] in ["-o"]: if opt[0] in ["-o"]:
filename_output = opt[1] filename_output = opt[1]
print("using output file", filename_output) print("using output file", filename_output)
if opt[0] in ["-O"]:
if filename_output:
print ("given output filename will be overwritten,")
print ("creating output filename from Invoice data.")
filename_from_invoice = True
if opt[0] in ["-t"]: if opt[0] in ["-t"]:
filename_template = opt[1] filename_template = opt[1]
print("using template file", filename_template) print("using template file", filename_template)
if opt[0] in ["-l"]: if opt[0] in ["-l"]:
list_invoices = True list_invoices = True
print("listing invoices") print("listing invoices")
if opt[0] in ["-P"]:
output_path = opt[1]
print ("output path is", output_path + ".")
# Check for correct input # Check for correct input
if len(args)>1: if len(args)>1:
@ -100,7 +115,7 @@ def main(argv=None):
raise Usage("No template given !") raise Usage("No template given !")
# Check for output file # Check for output file
if not filename_output: if not (filename_output or filename_from_invoice):
if filename_template: if filename_template:
filename_output = filename_template + ".out" filename_output = filename_template + ".out"
print("no output filename given, will be:", filename_output) print("no output filename given, will be:", filename_output)
@ -128,6 +143,9 @@ def main(argv=None):
print("-I ID use invoice ID") print("-I ID use invoice ID")
print("-t filename use filename as template file") print("-t filename use filename as template file")
print("-o filename use filename as output file") print("-o filename use filename as output file")
print( "-o filename use filename as output file" )
print( "-O create output filename by date, owner and invoice number" )
print( "-P path path for output file. Overwrites path in -o option" )
return retcode return retcode
@ -166,17 +184,35 @@ def main(argv=None):
print("Using the following invoice:") print("Using the following invoice:")
print(invoice) print(invoice)
loader = jinja2.FileSystemLoader('.')
path_template = os.path.dirname(filename_template)
filename_template_basename = os.path.basename(filename_template)
loader = jinja2.FileSystemLoader(path_template)
env = jinja2.Environment(loader=loader) env = jinja2.Environment(loader=loader)
template = env.get_template(filename_template) template = env.get_template(filename_template_basename)
#import IPython #company = gnucash_business.Company(book.instance)
#IPython.embed()
output = template.render(invoice=invoice, locale=locale)
print("Writing output", filename_output, ".") output = template.render(invoice=invoice, locale=locale) #, company=company)
if filename_from_invoice:
filename_date = str(invoice.GetDatePosted()) # something like 2014-11-01
filename_owner_name = str(invoice.GetOwner().GetName())
filename_invoice_id = str(invoice.GetID())
filename_output = filename_date + "_" + filename_owner_name + "_" + filename_invoice_id + ".tex"
if output_path:
filename_output = os.path.join(output_path, os.path.basename(filename_output))
print ("Writing output", filename_output, ".")
with open(filename_output, 'w') as f: with open(filename_output, 'w') as f:
f.write(output.encode('utf-8')) f.write(output)
if with_ipshell:
import IPython
IPython.embed()
if __name__ == "__main__": if __name__ == "__main__":
sys.exit(main()) sys.exit(main())