From 0434acbe1035ed679d23242a412c48a680ac5a07 Mon Sep 17 00:00:00 2001 From: c-holtermann Date: Fri, 19 Jun 2020 20:45:21 +0200 Subject: [PATCH] reformat two python example scripts with black use black python code formatter on latex_invoices.py and gncinvoice_jinja.py --- .../example_scripts/gncinvoice_jinja.py | 61 +++--- .../python/example_scripts/latex_invoices.py | 198 ++++++++++-------- 2 files changed, 143 insertions(+), 116 deletions(-) diff --git a/bindings/python/example_scripts/gncinvoice_jinja.py b/bindings/python/example_scripts/gncinvoice_jinja.py index bfa7c09a58..f13675a690 100755 --- a/bindings/python/example_scripts/gncinvoice_jinja.py +++ b/bindings/python/example_scripts/gncinvoice_jinja.py @@ -45,10 +45,12 @@ except ImportError as import_error: print(import_error) sys.exit(2) + class Usage(Exception): def __init__(self, msg): self.msg = msg + def main(argv=None): if argv is None: argv = sys.argv @@ -69,27 +71,27 @@ def main(argv=None): try: opts, args = getopt.getopt(argv[1:], "fhliI:t:o:OP:", ["help"]) except getopt.error as msg: - raise Usage(msg) + raise Usage(msg) for opt in opts: if opt[0] in ["-f"]: print("ignoring lock") ignore_lock = True - if opt[0] in ["-h","--help"]: + if opt[0] in ["-h", "--help"]: raise Usage("Help:") if opt[0] in ["-I"]: 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") + print("Using ipshell") with_ipshell = True if opt[0] in ["-o"]: filename_output = opt[1] 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.") + print("given output filename will be overwritten,") + print("creating output filename from Invoice data.") filename_from_invoice = True if opt[0] in ["-t"]: filename_template = opt[1] @@ -99,13 +101,13 @@ def main(argv=None): print("listing invoices") if opt[0] in ["-P"]: output_path = opt[1] - print ("output path is", output_path + ".") + print("output path is", output_path + ".") # Check for correct input - if len(args)>1: - print("opts:",opts,"args:",args) + if len(args) > 1: + print("opts:", opts, "args:", args) raise Usage("Only one input possible !") - if len(args)==0: + if len(args) == 0: raise Usage("No input given !") input_url = args[0] @@ -123,16 +125,16 @@ def main(argv=None): except Usage as err: if err.msg == "Help:": - retcode=0 + retcode = 0 else: print("Error:", err.msg, file=sys.stderr) print("for help use --help", file=sys.stderr) - retcode=2 + retcode = 2 print() print("Usage:") print() - print("Invoke with",prog_name,"gnucash_url.") + print("Invoke with", prog_name, "gnucash_url.") print("where input is") print(" filename") print("or file://filename") @@ -173,9 +175,9 @@ def main(argv=None): invoice_list = get_all_invoices(book) if list_invoices: - for number,invoice in enumerate(invoice_list): - print(str(number)+")") - print(invoice) + for number, invoice in enumerate(invoice_list): + print(str(number) + ")") + print(invoice) if not (no_output): @@ -191,7 +193,6 @@ def main(argv=None): print("Using the following invoice:") print(invoice) - path_template = os.path.dirname(filename_template) filename_template_basename = os.path.basename(filename_template) @@ -199,25 +200,37 @@ def main(argv=None): env = jinja2.Environment(loader=loader) template = env.get_template(filename_template_basename) - #company = gnucash_business.Company(book.instance) + # company = gnucash_business.Company(book.instance) - output = template.render(invoice=invoice, locale=locale) #, company=company) + output = template.render(invoice=invoice, locale=locale) # , company=company) if filename_from_invoice: - filename_date = invoice.GetDatePosted().strftime("%Y-%m-%d") # something like 2014-11-01 + filename_date = invoice.GetDatePosted().strftime( + "%Y-%m-%d" + ) # 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" + 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)) + filename_output = os.path.join( + output_path, os.path.basename(filename_output) + ) - print ("Writing output", filename_output, ".") - with open(filename_output, 'w') as f: + print("Writing output", filename_output, ".") + with open(filename_output, "w") as f: f.write(output) if with_ipshell: import IPython + IPython.embed() diff --git a/bindings/python/example_scripts/latex_invoices.py b/bindings/python/example_scripts/latex_invoices.py index c29922e05d..829021adfc 100644 --- a/bindings/python/example_scripts/latex_invoices.py +++ b/bindings/python/example_scripts/latex_invoices.py @@ -57,13 +57,24 @@ try: import str_methods from gncinvoicefkt import * from IPython import version_info as IPython_version_info - if IPython_version_info[0]>=1: + + if IPython_version_info[0] >= 1: from IPython.terminal.ipapp import TerminalIPythonApp else: from IPython.frontend.terminal.ipapp import TerminalIPythonApp - from gnucash.gnucash_business import Customer, Employee, Vendor, Job, \ - Address, Invoice, Entry, TaxTable, TaxTableEntry, GNC_AMT_TYPE_PERCENT, \ - GNC_DISC_PRETAX + from gnucash.gnucash_business import ( + Customer, + Employee, + Vendor, + Job, + Address, + Invoice, + Entry, + TaxTable, + TaxTableEntry, + GNC_AMT_TYPE_PERCENT, + GNC_DISC_PRETAX, + ) from gnucash import SessionOpenMode import locale except ImportError as import_error: @@ -71,99 +82,102 @@ except ImportError as import_error: print(import_error) sys.exit(2) + class Usage(Exception): def __init__(self, msg): self.msg = msg + def invoice_to_lco(invoice): - """returns a string which forms a lco-file for use with LaTeX""" + """returns a string which forms a lco-file for use with LaTeX""" - lco_out=u"\ProvidesFile{data.lco}[]\n" + lco_out = u"\ProvidesFile{data.lco}[]\n" - def write_variable(ukey, uvalue, replace_linebreak=True): + def write_variable(ukey, uvalue, replace_linebreak=True): - outstr = u"" - if uvalue.endswith("\n"): - uvalue=uvalue[0:len(uvalue)-1] + outstr = u"" + if uvalue.endswith("\n"): + uvalue = uvalue[0 : len(uvalue) - 1] - if not ukey in [u"fromaddress",u"toaddress",u"date"]: - outstr += u'\\newkomavar{' + if not ukey in [u"fromaddress", u"toaddress", u"date"]: + outstr += u"\\newkomavar{" + outstr += ukey + outstr += u"}\n" + + outstr += u"\\setkomavar{" outstr += ukey - outstr += u"}\n" + outstr += u"}{" + if replace_linebreak: + outstr += uvalue.replace(u"\n", u"\\\\") + "}" + return outstr - outstr += u"\\setkomavar{" - outstr += ukey - outstr += u"}{" - if replace_linebreak: - outstr += uvalue.replace(u"\n",u"\\\\")+"}" - return outstr + # Write owners address + add_str = u"" + owner = invoice.GetOwner() + if owner.GetName() != "": + add_str += owner.GetName().decode("UTF-8") + "\n" - # Write owners address - add_str=u"" - owner = invoice.GetOwner() - if owner.GetName() != "": - add_str += owner.GetName().decode("UTF-8")+"\n" + addr = owner.GetAddr() + if addr.GetName() != "": + add_str += addr.GetName().decode("UTF-8") + "\n" + if addr.GetAddr1() != "": + add_str += addr.GetAddr1().decode("UTF-8") + "\n" + if addr.GetAddr2() != "": + add_str += addr.GetAddr2().decode("UTF-8") + "\n" + if addr.GetAddr3() != "": + add_str += addr.GetAddr3().decode("UTF-8") + "\n" + if addr.GetAddr4() != "": + add_str += addr.GetAddr4().decode("UTF-8") + "\n" - addr = owner.GetAddr() - if addr.GetName() != "": - add_str += addr.GetName().decode("UTF-8")+"\n" - if addr.GetAddr1() != "": - add_str += addr.GetAddr1().decode("UTF-8")+"\n" - if addr.GetAddr2() != "": - add_str += addr.GetAddr2().decode("UTF-8")+"\n" - if addr.GetAddr3() != "": - add_str += addr.GetAddr3().decode("UTF-8")+"\n" - if addr.GetAddr4() != "": - add_str += addr.GetAddr4().decode("UTF-8")+"\n" + lco_out += write_variable("toaddress2", add_str) - lco_out += write_variable("toaddress2",add_str) + # Invoice number + inr_str = invoice.GetID() + lco_out += write_variable("rechnungsnummer", inr_str) - # Invoice number - inr_str = invoice.GetID() - lco_out += write_variable("rechnungsnummer",inr_str) + # date + date = invoice.GetDatePosted() + udate = date.strftime("%d.%m.%Y") + lco_out += write_variable("date", udate) + "\n" - # date - date = invoice.GetDatePosted() - udate = date.strftime("%d.%m.%Y") - lco_out += write_variable("date",udate)+"\n" + # date due + date_due = invoice.GetDateDue() + udate_due = date_due.strftime("%d.%m.%Y") + lco_out += write_variable("date_due", udate_due) + "\n" - # date due - date_due = invoice.GetDateDue() - udate_due = date_due.strftime("%d.%m.%Y") - lco_out += write_variable("date_due",udate_due)+"\n" + # Write the entries + ent_str = u"" + locale.setlocale(locale.LC_ALL, "de_DE") + for n, ent in enumerate(invoice.GetEntries()): + line_str = u"" - # Write the entries - ent_str = u"" - locale.setlocale(locale.LC_ALL,"de_DE") - for n,ent in enumerate(invoice.GetEntries()): + if type(ent) != Entry: + ent = Entry(instance=ent) # Add to method_returns_list - line_str = u"" + descr = ent.GetDescription() + price = ent.GetInvPrice().to_double() + n = ent.GetQuantity() - if type(ent) != Entry: - ent=Entry(instance=ent) # Add to method_returns_list + uprice = locale.currency(price).rstrip(" EUR") + un = unicode( + int(float(n.num()) / n.denom()) + ) # choose best way to format numbers according to locale - descr = ent.GetDescription() - price = ent.GetInvPrice().to_double() - n = ent.GetQuantity() + line_str = u"\Artikel{" + line_str += un + line_str += u"}{" + line_str += descr.decode("UTF-8") + line_str += u"}{" + line_str += uprice + line_str += u"}" - uprice = locale.currency(price).rstrip(" EUR") - un = unicode(int(float(n.num())/n.denom())) # choose best way to format numbers according to locale + # print(line_str) + ent_str += line_str - line_str = u"\Artikel{" - line_str += un - line_str += u"}{" - line_str += descr.decode("UTF-8") - line_str += u"}{" - line_str += uprice - line_str += u"}" + lco_out += write_variable("entries", ent_str) - #print(line_str) - ent_str += line_str - - lco_out += write_variable("entries",ent_str) - - return lco_out + return lco_out def main(argv=None): @@ -181,20 +195,20 @@ def main(argv=None): try: opts, args = getopt.getopt(argv[1:], "fhiln:po:", ["help"]) except getopt.error as msg: - raise Usage(msg) + raise Usage(msg) for opt in opts: if opt[0] in ["-f"]: print("ignoring lock") ignore_lock = True - if opt[0] in ["-h","--help"]: + if opt[0] in ["-h", "--help"]: raise Usage("Help:") if opt[0] in ["-i"]: print("Using ipshell") with_ipshell = True if opt[0] in ["-l"]: print("listing all invoices") - list_invoices=True + list_invoices = True if opt[0] in ["-n"]: invoice_number = int(opt[1]) print("using invoice number", invoice_number) @@ -202,25 +216,25 @@ def main(argv=None): if opt[0] in ["-o"]: output_file_name = opt[1] print("using output file", output_file_name) - if len(args)>1: - print("opts:",opts,"args:",args) + if len(args) > 1: + print("opts:", opts, "args:", args) raise Usage("Only one input can be accepted !") - if len(args)==0: + if len(args) == 0: raise Usage("No input given !") input_url = args[0] except Usage as err: if err.msg == "Help:": - retcode=0 + retcode = 0 else: print("Error:", err.msg, file=sys.stderr) print("for help use --help", file=sys.stderr) - retcode=2 + retcode = 2 print("Generate a LaTeX invoice or print out all invoices.") print() print("Usage:") print() - print("Invoke with",prog_name,"input.") + print("Invoke with", prog_name, "input.") print("where input is") print(" filename") print("or file://filename") @@ -253,39 +267,39 @@ def main(argv=None): comm_table = book.get_table() EUR = comm_table.lookup("CURRENCY", "EUR") - invoice_list=get_all_invoices(book) + invoice_list = get_all_invoices(book) if list_invoices: - for number,invoice in enumerate(invoice_list): - print(str(number)+")") + for number, invoice in enumerate(invoice_list): + print(str(number) + ")") print(invoice) if not (no_latex_output): if invoice_number == None: print("Using the first invoice:") - invoice_number=0 + invoice_number = 0 - invoice=invoice_list[invoice_number] + invoice = invoice_list[invoice_number] print("Using the following invoice:") print(invoice) - lco_str=invoice_to_lco(invoice) + lco_str = invoice_to_lco(invoice) # Opening output file - f=open(output_file_name,"w") - lco_str=lco_str.encode("latin1") + f = open(output_file_name, "w") + lco_str = lco_str.encode("latin1") f.write(lco_str) f.close() if with_ipshell: app = TerminalIPythonApp.instance() - app.initialize(argv=[]) # argv=[] instructs IPython to ignore sys.argv + app.initialize(argv=[]) # argv=[] instructs IPython to ignore sys.argv app.start() - #session.save() + # session.save() session.end() + if __name__ == "__main__": sys.exit(main()) -