~trufas/ledgeroni

ref: 7a9e4ad4a64b45840f93b4ab44b6c2e463e16b13 ledgeroni/ledgeroni/commands/print.py -rw-r--r-- 1.1 KiB
7a9e4ad4 — Rafael Castillo Clean up stdout 1 year, 10 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""
print.py: Defines the `print` subcommand
"""
import sys
import click
from ledgeroni.journal import Journal
from ledgeroni import expression


@click.command()
@click.argument('filter_strs', nargs=-1)
@click.pass_context
def print_transactions(ctx, filter_strs):
    "`ledgeroni print` subcommand"
    filter_query = None
    if filter_strs:
        filter_query = expression.build_expression(' '.join(filter_strs))

    sorter = ctx.obj.get('SORTER', None)
    journal = Journal(query=filter_query)
    price_db = ctx.obj.get('PRICE_DB', None)
    if price_db:
        journal.add_from_file(price_db)

    for filename in ctx.obj.get('LEDGER_FILES', []):
        journal.add_from_file(filename)

    errors = journal.verify_transaction_balances()
    if errors:
        for error in errors:
            errstr = 'ERROR! Transaction unbalanced: {}'.format(error.header)
            click.echo(errstr, err=True)
        sys.exit(1)

    if sorter:
        sorter.sort_journal(journal)

    click.echo('\n\n'.join(t.as_journal_format()
                           for t in journal.transactions))