invoice2data icon indicating copy to clipboard operation
invoice2data copied to clipboard

Template variables and the original filename should be allowed as part of --filename-format !

Open reikred opened this issue 4 years ago • 4 comments

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.

reikred avatar Dec 14 '20 04:12 reikred

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.

reikred avatar Dec 16 '20 16:12 reikred

You can probably add a pull request to make more variables available for the filename. Should be simple.

m3nu avatar Dec 17 '20 02:12 m3nu

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?

reikred avatar Dec 17 '20 03:12 reikred

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.

reikred avatar Dec 18 '20 17:12 reikred

Fixed in #403

bosd avatar Oct 21 '22 21:10 bosd

I have not tested yet, but this sounds awesome. A great enhancement.

reikred avatar Oct 25 '22 23:10 reikred