invoice2data
invoice2data copied to clipboard
Template variables and the original filename should be allowed as part of --filename-format !
I would like to use something like the following
--copy ./ --filename-format "{date}.{invoice_number}.{issuer}.{orig_filename}"
However, invoice2data does not recognize {issuer} (although it is defined in the template), and {orig_filename} is not supported at all.
Should not --filename-format allow using any variable (attribute) defined in the template file? And should the original filename not also be available to construct the new filename? That would seem logical to me. If anyone knows any workarounds for what I need, that would be great, too.
Sorry, I'm not much of a python hacker, especially the OO stuff, so I could not figure this out from studying the source code.
I tried to see if I could hack main.py so that {issuer} could be used in --filename-format. Generally, it looks like res['keyword'] is how attribute values are accessed. Hence I tried to put
issuer=res['issuer'],
into the appropriate place(s) in main.py, but python complained about KeyError:
KeyError: 'issuer'
All I did was to change main.py as follows
diff main.py.ORIG main.py 8a9 import pdb 201a203,204
209a213
issuer=res['issuer'],
210a215,216
#pdb.set_trace() #breakpoint()
216a223
issuer=res['issuer'],
PS: Also, it looks like what I called orig_filename above is called invoicefile in main.py. Nevertheless, one cannot refer to {invoicefile} when constructing --filename-format.
You can probably add a pull request to make more variables available for the filename. Should be simple.
But I don't know how to do it. I tried the following but get a runtime error as described above: (KeyError: 'issuer')
if res:
logger.info(res)
output.append(res)
if args.copy:
filename = args.filename.format(
date=res['date'].strftime('%Y-%m-%d'),
invoice_number=res['invoice_number'],
desc=res['desc'],
issuer=res['issuer'], #<----------------------------------added this
)
#pdb.set_trace()
#breakpoint()
shutil.copyfile(f.name, join(args.copy, filename))
if args.move:
filename = args.filename.format(
date=res['date'].strftime('%Y-%m-%d'),
invoice_number=res['invoice_number'],
desc=res['desc'],
issuer=res['issuer'], #<----------------------------------added this
)
shutil.move(f.name, join(args.move, filename))
Can the author or anyone provide some hints on what I have to do?
An update/edit to this comment 18 days later: Hmm, after doing some uninstall/reinstall it appears that the above lines of code started working, no more error message. Somehow there may have been a conflict between various attempts at doing a "develop" install and some earlier pip3 install.
This is what worked:
uninstall any pip-based install download source code python setup.py develop
Then I could make edits in main.py the source directory and have the changes apply (and no error) when running /usr/local/bin/invoice2data. Seems like no .egg file being installed anymore, which is probably good, given that what is in the .egg (.zip) file might override what is in the source directory. Anyway, not even sure THAT was the underlying problem.
Doing some more experiments now.
Fixed in #403
I have not tested yet, but this sounds awesome. A great enhancement.