On Mon, Oct 16, 2017 at 3:24 PM, Holger Rapp <[email protected]> wrote:
> Heyho, > https://youtu.be/HI0x0KYChq4?t=1m18s > > Thanks for beancount, it is an awesome piece of technology! > Really glad it's doing things for you! :-) My situation: My root file "root.beancount" includes sub files for all my > individual accounts, i.e "my_bank.beancount". What I'd like to do is run > over all Transactions in "my_bank.beancount", potentially changing the > Accounts in the postings of each Transaction and write it back out into the > file it was read from - keeping the order of the entries in the file. > > I tried this: > > entries, _, _ = beancount.loader.load_file("root.beancount") > for e in entries: > if not os.path.basename(e.meta['filename']) == "my_bank.beancount": > continue > if isinstance(e, data.Transaction): > change_around_account(e) > printer.print_entry(e) > > This does not work as expected: > > - it changes whitespace: while all my postings used to be indented by four > spaces, after running this they are only indented by 2. > - I have several "balance" and "note" statements in the file which get > reordered and empty lines inserted before and after. I understand that this > is insignificant for beancount, but the ordering is meaningful to me. > Yep. This is all sensible. The input file and the parsed contents are fairly different. Input is incomplete, and parsing and the various interpolation and processing that Beancount does, as well as the plugins, generate a lot of other stuff. It also throws away the order (though the original location is secretely stored in the metadata, which you can access as ".meta" on all directives.) It's not really intended to be able to rerender the original input from the parsed stream. It is not too hard to parse out the individual pieces that I am interested > in the files and iterate over the items myself that way, but I wondered if > there was a blessed way of doing what I want. > In order to do that, I would suggest going LO-FI and using sed (or Python), and working off of the input text directly. Working off of the parsed input will just cause you headaches. -- You received this message because you are subscribed to the Google Groups "Beancount" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/beancount/CAK21%2BhPVEJG5YvcyeaMbrOmnAyORDFWsnO_4fi%3D2Vn-Q0vxNYA%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
