Re: Machine-readable reports

105 views
Skip to first unread message

Peter Keen

unread,
May 12, 2013, 9:52:21 PM5/12/13
to Ledger
Hi Oleg,

You can specify your own formats for both the balance and register reports. See the docs for output customization[1] and formatting codes[2]. I use a custom format when loading data into my sql reporting database which is effectively the CSV output plus a bunch of fields. You can see the register format I use in the source for ledger-web[3].


--Pete


On Sun, May 12, 2013 at 1:54 AM, oleg dashevskii <olegdas...@gmail.com> wrote:
Hi,

having been a proud user of ledger+vim+custom shell scripts combo for a long time, I want to integrate ledger into some bigger system now. Another program would generate transactions, save them into a file consumable by ledger, run "balance" or "register" commands and parse the report.

The question is what's the best way to do that. 3.0 docs describe XML format, but I couldn't make "xml" command work (besides, does XML support metadata?). Still, input data can be put into a text file. What concerns me more is output of "balance" and "register". The normal ledger output is custom-tailored to fit screen width, some account names are presented in a compact way, etc. This presents a problem for parsing, since parser must identify the exact account name.

So, how can I solve this problem? Can XML (or CSV which is also mentioned in the docs) be used for input and output? Or, are there any flags which make ledger output more machine-readable?

--
Oleg.

--
 
---
You received this message because you are subscribed to the Google Groups "Ledger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ledger-cli+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Zack Williams

unread,
May 12, 2013, 11:49:37 PM5/12/13
to ledge...@googlegroups.com

On Sun, May 12, 2013 at 1:54 AM, oleg dashevskii <olegdas...@gmail.com> wrote:
So, how can I solve this problem?

The "xml" output format is basically the same as "balance", not "register" 

As for the rest of it, I'd probably go to the python interface.  It works OK, but is a bit rough around the edges (You can add/subtract amounts, but no multiplication, etc.) 

My generic python boilerplate, if anyone is interested: 

---
#!/usr/bin/env python 
# must be run via `ledger python <thisfile>.py <journalfile>`

import ledger
import sys

journal = ledger.read_journal(sys.argv[1])

# create a USD commodity
comms = ledger.commodities
usd = comms.find_or_create('$')

# return an amount for the balance
def balance_for_account(acctname):
    account = journal.find_account_re(acctname)
    return balance_posts_subaccts(account)

# recursively determine the balance for all posts/subaccounts in given account
def balance_posts_subaccts(account):
    total = ledger.Balance()
    for post in account.posts():
        total += post.amount
    for subacct in account.accounts():
        total += balance_posts_subaccts(subacct)
    return total

print balance_for_account("Equity:Accounts").value(usd);
--

- Zack 
Reply all
Reply to author
Forward
0 new messages