GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gncinvoice_jinja.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 ##@file
5 # @brief exports an invoice from gnucash using a template file, see \ref py_invoice_export
6 # @ingroup python_bindings_examples
7 # @author Christoph Holtermann (c.holtermann (at) gmx.de)
8 # @date 2014-11
9 #
10 # @details
11 # Input is a template file that will be filled with information from
12 # gnucash Invoices. Jinja2 templating engine ist used. Templates can
13 # be Latex, Html or anything.
14 #
15 # Example templates for german invoices:
16 # - Invoice.tex.tmpl
17 # - Invoice_2.tex.tmpl
18 #
19 # This is a sequel to latex_invoices.py that exported to a lco file
20 # to be imported into a LaTeX letter.
21 # The approach used here is not as dependent on external files and
22 # more modular as it allows to use arbitrary templates
23 #
24 # Doxygen docs:
25 # - http://svn.gnucash.org/docs/HEAD/
26 # - see page \ref py_invoice_export
27 #
28 # Questions / Issues:
29 # - How much logic in the template, how much preprocessing in this file ?
30 # - Internationalization - currencies, formatting of numbers
31 # - Providing data of gnucash owner
32 
33 try:
34  import locale
35  import sys
36  import getopt
37  import gnucash
38  import str_methods
39  import jinja2
40  from gncinvoicefkt import *
41 except ImportError as import_error:
42  print "Problem importing modules."
43  print import_error
44  sys.exit(2)
45 
46 class Usage(Exception):
47  def __init__(self, msg):
48  self.msg = msg
49 
50 def main(argv=None):
51  if argv is None:
52  argv = sys.argv
53  try:
54  # default values
55  prog_name = argv[0]
56  ignore_lock = True
57  filename_template = None
58  filename_output = None
59  no_output = False
60  list_invoices = False
61  invoice_number = None
62  invoice_id = None
63 
64  try:
65  opts, args = getopt.getopt(argv[1:], "fhlI:t:o:", ["help"])
66  except getopt.error, msg:
67  raise Usage(msg)
68 
69  for opt in opts:
70  if opt[0] in ["-f"]:
71  print "ignoring lock"
72  ignore_lock = True
73  if opt[0] in ["-h","--help"]:
74  raise Usage("Help:")
75  if opt[0] in ["-I"]:
76  invoice_id = opt[1]
77  print "using invoice ID '" + str(invoice_id) + "'."
78  if opt[0] in ["-o"]:
79  filename_output = opt[1]
80  print "using output file", filename_output
81  if opt[0] in ["-t"]:
82  filename_template = opt[1]
83  print "using template file", filename_template
84  if opt[0] in ["-l"]:
85  list_invoices = True
86  print "listing invoices"
87 
88  # Check for correct input
89  if len(args)>1:
90  print "opts:",opts,"args:",args
91  raise Usage("Only one input possible !")
92  if len(args)==0:
93  raise Usage("No input given !")
94  input_url = args[0]
95 
96  # Check for correct template
97  if not filename_template:
98  no_output = True
99  if not list_invoices:
100  raise Usage("No template given !")
101 
102  # Check for output file
103  if not filename_output:
104  if filename_template:
105  filename_output = filename_template + ".out"
106  print "no output filename given, will be:", filename_output
107 
108  except Usage, err:
109  if err.msg == "Help:":
110  retcode=0
111  else:
112  print >>sys.stderr, "Error:",err.msg
113  print >>sys.stderr, "for help use --help"
114  retcode=2
115 
116  print
117  print "Usage:"
118  print
119  print "Invoke with",prog_name,"gnucash_url."
120  print "where input is"
121  print " filename"
122  print "or file://filename"
123  print "or mysql://user:password@host/databasename"
124  print
125  print "-f force open = ignore lock"
126  print "-l list all invoices"
127  print "-h or --help for this help"
128  print "-I ID use invoice ID"
129  print "-t filename use filename as template file"
130  print "-o filename use filename as output file"
131 
132  return retcode
133 
134  # Try to open the given input
135  try:
136  print "Opening", input_url, "."
137  session = gnucash.Session(input_url, ignore_lock=ignore_lock)
138  except Exception as exception:
139  print "Problem opening input."
140  print exception
141  return 2
142 
143  book = session.book
144  root_account = book.get_root_account()
145  comm_table = book.get_table()
146  EUR = comm_table.lookup("CURRENCY", "EUR")
147 
148  invoice_list = get_all_invoices(book)
149 
150  if list_invoices:
151  for number,invoice in enumerate(invoice_list):
152  print str(number)+")"
153  print invoice
154 
155  if not (no_output):
156 
157  if invoice_id:
158  invoice = book.InvoiceLookupByID(invoice_id)
159  if not invoice:
160  print "ID not found."
161  return 2
162 
163  if invoice_number:
164  invoice = invoice_list[invoice_number]
165 
166  print "Using the following invoice:"
167  print invoice
168 
169  loader = jinja2.FileSystemLoader('.')
170  env = jinja2.Environment(loader=loader)
171  template = env.get_template(filename_template)
172 
173  #import IPython
174  #IPython.embed()
175  output = template.render(invoice=invoice, locale=locale)
176 
177  print "Writing output", filename_output, "."
178  with open(filename_output, 'w') as f:
179  f.write(output.encode('utf-8'))
180 
181 if __name__ == "__main__":
182  sys.exit(main())