On 15/04/2021 15:31, Daniele Nicolodi wrote:
> Hello,
> 
> I am writing a tool that should operate on a selection of entries from a
> Benacount ledger. I could come up with my own little DSL to do the
> selection, but we already have BQL, thus I think reusing it would be nice.

Replying to myself. There is not a ready made function, but it is easy
to put together one. For example:

from beancount.query import query_compile
from beancount.query import query_env
from beancount.query import query_execute
from beancount.query import query_parser


def filter_entries(entries, options, what):
    env_targets = query_env.TargetsEnvironment()
    env_entries = query_env.FilterEntriesEnvironment()
    env_postings = query_env.FilterPostingsEnvironment()

    # Parse the BQL statement.
    parser = query_parser.Parser()
    statement = parser.parse(f'PRINT FROM {what:}')

    # Compile the BQL statement.
    compiled = query_compile.compile(statement,
        env_targets, env_postings, env_entries)

    # Execute only the filtering part of the BQL statement.
    context = query_execute.create_row_context(entries, options)
    entries = query_execute.filter_entries(compiled.c_from,
                                           entries, options, context)

    return entries

Cheers,
Dan

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/beancount/74e9e559-14e0-3065-63e5-171af236cf09%40grinta.net.

Reply via email to