183def main(argv=None):
184 if argv is None:
185 argv = sys.argv
186 try:
187 prog_name = argv[0]
188 with_ipshell = False
189 ignore_lock = False
190 no_latex_output = True
191 list_invoices = False
192 output_file_name = "data.lco"
193 invoice_number = None
194
195 try:
196 opts, args = getopt.getopt(argv[1:], "fhiln:po:", ["help"])
197 except getopt.error as msg:
198 raise Usage(msg)
199
200 for opt in opts:
201 if opt[0] in ["-f"]:
202 print("ignoring lock")
203 ignore_lock = True
204 if opt[0] in ["-h", "--help"]:
205 raise Usage("Help:")
206 if opt[0] in ["-i"]:
207 print("Using ipshell")
208 with_ipshell = True
209 if opt[0] in ["-l"]:
210 print("listing all invoices")
211 list_invoices = True
212 if opt[0] in ["-n"]:
213 invoice_number = int(opt[1])
214 print("using invoice number", invoice_number)
215 no_latex_output = False
216 if opt[0] in ["-o"]:
217 output_file_name = opt[1]
218 print("using output file", output_file_name)
219 if len(args) > 1:
220 print("opts:", opts, "args:", args)
221 raise Usage("Only one input can be accepted !")
222 if len(args) == 0:
223 raise Usage("No input given !")
224 input_url = args[0]
225 except Usage as err:
226 if err.msg == "Help:":
227 retcode = 0
228 else:
229 print("Error:", err.msg, file=sys.stderr)
230 print("for help use --help", file=sys.stderr)
231 retcode = 2
232
233 print("Generate a LaTeX invoice or print out all invoices.")
234 print()
235 print("Usage:")
236 print()
237 print("Invoke with", prog_name, "input.")
238 print("where input is")
239 print(" filename")
240 print("or file://filename")
241 print("or mysql://user:password@host/databasename")
242 print()
243 print("-f force open = ignore lock")
244 print("-h or --help for this help")
245 print("-i for ipython shell")
246 print("-l list all invoices")
247 print("-n number use invoice number (no. from previous run with -l)")
248 print("-o name use name as outputfile. default: data.lco")
249
250 return retcode
251
252
253 try:
254 session = gnucash.Session(
255 input_url,
256 SessionOpenMode.SESSION_READ_ONLY
257 if ignore_lock
258 else SessionOpenMode.SESSION_NORMAL_OPEN,
259 )
260 except Exception as exception:
261 print("Problem opening input.")
262 print(exception)
263 return 2
264
265 book = session.book
266 root_account = book.get_root_account()
267 comm_table = book.get_table()
268 EUR = comm_table.lookup("CURRENCY", "EUR")
269
270 invoice_list = get_all_invoices(book)
271
272 if list_invoices:
273 for number, invoice in enumerate(invoice_list):
274 print(str(number) + ")")
275 print(invoice)
276
277 if not (no_latex_output):
278
279 if invoice_number == None:
280 print("Using the first invoice:")
281 invoice_number = 0
282
283 invoice = invoice_list[invoice_number]
284 print("Using the following invoice:")
285 print(invoice)
286
287 lco_str = invoice_to_lco(invoice)
288
289
290 f = open(output_file_name, "w")
291 f.write(lco_str)
292 f.close()
293
294 if with_ipshell:
295 app = TerminalIPythonApp.instance()
296 app.initialize(argv=[])
297 app.start()
298
299
300 session.end()
301
302