#!/usr/bin/env python
"""
Compute and print the balance of each selected account.
"""

# stdlib imports
from os.path import *

# beancount imports
from beancount import cmdline
from beancount.utils import render_tree


def main():
    import optparse
    parser = optparse.OptionParser(__doc__.strip())

    parser.add_option('-l', '--local', action='store_true',
                      help="Display the local account's balance only.")

    parser.add_option('-B', '--at-cost', action='store_true',
                      help="Compute amounts at cost units instead of in amount units.")

    cmdline.addopts(parser)
    opts, ledger, args = cmdline.main(parser)

    ledger.compute_balsheet('total', opts.at_cost)
    for acc, branch, line in render_tree(ledger.get_root_account()):
        aline = branch + line
        print '  %-40s %s' % (aline, acc.balances['total'].round())


if __name__ == '__main__':
    main()

