Reordering file

45 views
Skip to first unread message

m.red...@gmail.com

unread,
Oct 11, 2019, 5:06:33 PM10/11/19
to Beancount
I've been playing with Beancount API for some custom interactive importers, and so far it is doing everything I want, except (perhaps) for one thing.

I would like to make a `merge` that takes the new entries generated by the import and inserts them in the main file (let's assume only one file for now), in the right location ordering them by date.

So I did the obvious round-trip (for the time being to a new file):

    existing, err, opts = load_file(main)
    if err:
        raise Exception("The main file contains errors. Aborting")
    new, _, _ = load_file(filename)
    existing.extend(new)

    res = sorted(existing, key=lambda c: c.date)

    with open(f"{main}_new", "w") as f:
        print_entries(res, file=f)


But this way comments, tags, options (perhaps more) are not preserved.

Is there a simple way to do this?

Thanks

M.

Martin Blais

unread,
Oct 11, 2019, 9:54:01 PM10/11/19
to Beancount
Yes, that won't work. What it'll output is the processed stream of
transactions, not the ones it parsed.
(There are two stages: parsing, then running the plugins makes a bunch
of changes to the stream.)


>
> Is there a simple way to do this?

I would do this differently; the lo-fi version is to insert a comment
with a unique marker and have your script insert the new transactions
right before it.
You'll have to sort them out based on your rules.




> Thanks
>
> M.
>
> --
> 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 beancount+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/beancount/fe38849b-79e5-4055-b77e-d5ba6f3cd920%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages