added pysample.dat, which provides a more realistic example of using Python
This commit is contained in:
parent
3a3227298d
commit
66bc51db6d
7 changed files with 94 additions and 76 deletions
129
config.cc
129
config.cc
|
|
@ -25,11 +25,11 @@ config_t::config_t()
|
||||||
balance_format = "%20T %2_%-n\n";
|
balance_format = "%20T %2_%-n\n";
|
||||||
register_format = ("%D %-.20P %-.22N %12.66t %12.80T\n%/"
|
register_format = ("%D %-.20P %-.22N %12.66t %12.80T\n%/"
|
||||||
"%32|%-.22N %12.66t %12.80T\n");
|
"%32|%-.22N %12.66t %12.80T\n");
|
||||||
plot_value_format = "%D %t\n";
|
plot_amount_format = "%D %t\n";
|
||||||
plot_total_format = "%D %T\n";
|
plot_total_format = "%D %T\n";
|
||||||
print_format = "\n%D %X%C%P\n %-34N %12o\n%/ %-34N %12o\n";
|
print_format = "\n%D %X%C%P\n %-34N %12o\n%/ %-34N %12o\n";
|
||||||
equity_format = "\n%D %X%C%P\n%/ %-34N %12t\n";
|
equity_format = "\n%D %X%C%P\n%/ %-34N %12t\n";
|
||||||
prices_format = "%D %-10N %12t %12T\n";
|
prices_format = "%[%Y/%m/%d %H:%M:%S %Z] %-10N %12t %12T\n";
|
||||||
|
|
||||||
show_collapsed = false;
|
show_collapsed = false;
|
||||||
show_subtotal = false;
|
show_subtotal = false;
|
||||||
|
|
@ -276,62 +276,64 @@ void option_help(std::ostream& out)
|
||||||
out
|
out
|
||||||
<< "usage: ledger [options] COMMAND [ACCT REGEX]... [-- [PAYEE REGEX]...]\n\n\
|
<< "usage: ledger [options] COMMAND [ACCT REGEX]... [-- [PAYEE REGEX]...]\n\n\
|
||||||
Basic options:\n\
|
Basic options:\n\
|
||||||
-h, --help display this help text\n\
|
-h, --help display this help text\n\
|
||||||
-v, --version show version information\n\
|
-v, --version show version information\n\
|
||||||
-i, --init FILE initialize ledger by loading FILE (def: ~/.ledgerrc)\n\
|
-i, --init FILE initialize ledger by loading FILE (def: ~/.ledgerrc)\n\
|
||||||
--cache FILE use FILE as a binary cache when --file is not used\n\
|
--cache FILE use FILE as a binary cache when --file is not used\n\
|
||||||
-f, --file FILE read ledger data from FILE\n\
|
-f, --file FILE read ledger data from FILE\n\
|
||||||
-o, --output FILE write output to FILE\n\
|
-o, --output FILE write output to FILE\n\
|
||||||
-z, --set-price CONV specify a commodity conversion: \"COMM=AMOUNT\"\n\
|
-z, --set-price CONV specify a commodity conversion: \"COMM=AMOUNT\"\n\
|
||||||
-a, --account NAME specify the default account (useful with QIF files)\n\n\
|
-a, --account NAME use NAME as default account (useful with QIF files)\n\n\
|
||||||
Report filtering:\n\
|
Report filtering:\n\
|
||||||
-b, --begin-date DATE set report begin date\n\
|
-b, --begin-date DATE set report begin date\n\
|
||||||
-e, --end-date DATE set report end date\n\
|
-e, --end-date DATE set report end date\n\
|
||||||
-c, --current show only current and past entries (not future)\n\
|
-c, --current show only current and past entries (not future)\n\
|
||||||
-C, --cleared consider only cleared transactions\n\
|
-C, --cleared consider only cleared transactions\n\
|
||||||
-U, --uncleared consider only uncleared transactions\n\
|
-U, --uncleared consider only uncleared transactions\n\
|
||||||
-R, --real consider only non-virtual transactions\n\
|
-R, --real consider only non-virtual transactions\n\
|
||||||
-r, --related calculate report using related transactions\n\n\
|
-r, --related calculate report using related transactions\n\n\
|
||||||
Output customization:\n\
|
Output customization:\n\
|
||||||
-F, --format STR use STR as the format; for each report type, use:\n\
|
-F, --format STR use STR as the format; for each report type, use:\n\
|
||||||
--balance-format --equity-format\n\
|
--balance-format --equity-format\n\
|
||||||
--register-format --plot-value-format\n\
|
--register-format --plot-amount-format\n\
|
||||||
--print-format --plot-total-format\n\
|
--print-format --plot-total-format\n\
|
||||||
-y, --date-format STR use STR as the date format (def: %Y/%m/%d)\n\
|
--prices-format\n\
|
||||||
-E, --empty balance: show accounts with zero balance\n\
|
-y, --date-format STR use STR as the date format (def: %Y/%m/%d)\n\
|
||||||
-n, --collapse register: collapse entries with multiple transactions\n\
|
-E, --empty balance: show accounts with zero balance\n\
|
||||||
-s, --subtotal balance: show sub-accounts; register: show subtotals\n\
|
-n, --collapse register: collapse entries with multiple transactions\n\
|
||||||
-S, --sort EXPR sort report according to the value expression EXPR\n\
|
-s, --subtotal balance: show sub-accounts; register: show subtotals\n\
|
||||||
-p, --interval STR report by interval (period), based on STR\n\
|
-S, --sort EXPR sort report according to the value expression EXPR\n\
|
||||||
--dow show a days-of-the-week report\n\
|
-p, --interval STR report by interval (period), based on STR\n\
|
||||||
-W, --weekly show weekly sub-totals\n\
|
--dow show a days-of-the-week report\n\
|
||||||
-M, --monthly show monthly sub-totals\n\
|
-W, --weekly show weekly sub-totals\n\
|
||||||
-Y, --yearly show yearly sub-totals\n\
|
-M, --monthly show monthly sub-totals\n\
|
||||||
-l, --limit EXPR calculate only transactions matching EXPR\n\
|
-Y, --yearly show yearly sub-totals\n\
|
||||||
-d, --display EXPR display only transactions matching EXPR\n\
|
-l, --limit EXPR calculate only transactions matching EXPR\n\
|
||||||
-t, --value-expr EXPR set the value expression for all report types\n\
|
-d, --display EXPR display only transactions matching EXPR\n\
|
||||||
-T, --total-expr EXPR set the total expression for all report types\n\
|
-t, --amount-expr EXPR set the amount expression for all report types\n\
|
||||||
-j, --value-data print only raw value data (useful when scripting)\n\
|
-T, --total-expr EXPR set the total expression for all report types\n\
|
||||||
-J, --total-data print only raw total data\n\n\
|
-j, --amount-data print only raw amount data (useful for scripting)\n\
|
||||||
|
-J, --total-data print only raw total data\n\n\
|
||||||
Commodity reporting:\n\
|
Commodity reporting:\n\
|
||||||
-P, --price-db FILE sets the price database to FILE (def: ~/.pricedb)\n\
|
-P, --price-db FILE sets the price database to FILE (def: ~/.pricedb)\n\
|
||||||
-L, --price-exp MINS download quotes only if newer than MINS (def: 1440)\n\
|
-L, --price-exp MINS download quotes only if newer than MINS (def: 1440)\n\
|
||||||
-Q, --download download price information when needed\n\
|
-Q, --download download price information when needed\n\
|
||||||
-O, --quantity report commodity totals (this is the default)\n\
|
-O, --quantity report commodity totals (this is the default)\n\
|
||||||
-B, --basis report commodity cost basis\n\
|
-B, --basis report commodity cost basis\n\
|
||||||
-V, --market report commodity market value\n\
|
-V, --market report commodity market value\n\
|
||||||
-G, --gain report commodity gain/loss\n\
|
-G, --gain report commodity gain/loss\n\
|
||||||
-A, --average report average transaction amount\n\
|
-A, --average report average transaction amount\n\
|
||||||
-D, --deviation report deviation from the average\n\
|
-D, --deviation report deviation from the average\n\
|
||||||
-X, --trend report average deviation from the average\n\
|
-X, --trend report average deviation from the average\n\
|
||||||
-Z, --weighted-trend same as trend, but older values are less significant\n\
|
-Z, --weighted-trend same as trend, but older values are less significant\n\
|
||||||
(-D, -X and -Z make little sense in balance reports)\n\
|
(-D, -X and -Z make little sense in balance reports)\n\
|
||||||
Commands:\n\
|
Commands:\n\
|
||||||
balance [REGEXP]... show balance totals for matching accounts\n\
|
balance [REGEXP]... show balance totals for matching accounts\n\
|
||||||
register [REGEXP]... show register of matching transactions\n\
|
register [REGEXP]... show register of matching transactions\n\
|
||||||
print [REGEXP]... print all matching entries\n\
|
print [REGEXP]... print all matching entries\n\
|
||||||
equity [REGEXP]... output equity entries for matching accounts\n\
|
equity [REGEXP]... output equity entries for matching accounts\n\
|
||||||
entry DATE PAYEE AMT output a derived entry, based on the arguments\n";
|
prices [REGEXP]... display price history for matching commodities\n\
|
||||||
|
entry DATE PAYEE AMT output a derived entry, based on the arguments\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////
|
||||||
|
|
@ -444,9 +446,9 @@ OPT_BEGIN(register_format, ":") {
|
||||||
config.register_format = optarg;
|
config.register_format = optarg;
|
||||||
} OPT_END(register_format);
|
} OPT_END(register_format);
|
||||||
|
|
||||||
OPT_BEGIN(plot_value_format, ":") {
|
OPT_BEGIN(plot_amount_format, ":") {
|
||||||
config.plot_value_format = optarg;
|
config.plot_amount_format = optarg;
|
||||||
} OPT_END(plot_value_format);
|
} OPT_END(plot_amount_format);
|
||||||
|
|
||||||
OPT_BEGIN(plot_total_format, ":") {
|
OPT_BEGIN(plot_total_format, ":") {
|
||||||
config.plot_total_format = optarg;
|
config.plot_total_format = optarg;
|
||||||
|
|
@ -460,6 +462,10 @@ OPT_BEGIN(equity_format, ":") {
|
||||||
config.equity_format = optarg;
|
config.equity_format = optarg;
|
||||||
} OPT_END(equity_format);
|
} OPT_END(equity_format);
|
||||||
|
|
||||||
|
OPT_BEGIN(prices_format, ":") {
|
||||||
|
config.prices_format = optarg;
|
||||||
|
} OPT_END(prices_format);
|
||||||
|
|
||||||
OPT_BEGIN(empty, "E") {
|
OPT_BEGIN(empty, "E") {
|
||||||
config.show_empty = true;
|
config.show_empty = true;
|
||||||
} OPT_END(empty);
|
} OPT_END(empty);
|
||||||
|
|
@ -524,10 +530,10 @@ OPT_BEGIN(total_expr, "T:") {
|
||||||
config.total_expr = optarg;
|
config.total_expr = optarg;
|
||||||
} OPT_END(total_expr);
|
} OPT_END(total_expr);
|
||||||
|
|
||||||
OPT_BEGIN(value_data, "j") {
|
OPT_BEGIN(amount_data, "j") {
|
||||||
config.amount_expr = "S" + config.amount_expr;
|
config.amount_expr = "S" + config.amount_expr;
|
||||||
config.format_string = config.plot_value_format;
|
config.format_string = config.plot_amount_format;
|
||||||
} OPT_END(value_data);
|
} OPT_END(amount_data);
|
||||||
|
|
||||||
OPT_BEGIN(total_data, "J") {
|
OPT_BEGIN(total_data, "J") {
|
||||||
config.total_expr = "S" + config.total_expr;
|
config.total_expr = "S" + config.total_expr;
|
||||||
|
|
@ -649,10 +655,11 @@ void export_config()
|
||||||
.def_readwrite("format_string", &config_t::format_string)
|
.def_readwrite("format_string", &config_t::format_string)
|
||||||
.def_readwrite("balance_format", &config_t::balance_format)
|
.def_readwrite("balance_format", &config_t::balance_format)
|
||||||
.def_readwrite("register_format", &config_t::register_format)
|
.def_readwrite("register_format", &config_t::register_format)
|
||||||
.def_readwrite("plot_value_format", &config_t::plot_value_format)
|
.def_readwrite("plot_amount_format", &config_t::plot_amount_format)
|
||||||
.def_readwrite("plot_total_format", &config_t::plot_total_format)
|
.def_readwrite("plot_total_format", &config_t::plot_total_format)
|
||||||
.def_readwrite("print_format", &config_t::print_format)
|
.def_readwrite("print_format", &config_t::print_format)
|
||||||
.def_readwrite("equity_format", &config_t::equity_format)
|
.def_readwrite("equity_format", &config_t::equity_format)
|
||||||
|
.def_readwrite("prices_format", &config_t::prices_format)
|
||||||
.def_readwrite("date_format", &config_t::date_format)
|
.def_readwrite("date_format", &config_t::date_format)
|
||||||
.def_readwrite("sort_string", &config_t::sort_string)
|
.def_readwrite("sort_string", &config_t::sort_string)
|
||||||
.def_readwrite("amount_expr", &config_t::amount_expr)
|
.def_readwrite("amount_expr", &config_t::amount_expr)
|
||||||
|
|
|
||||||
2
config.h
2
config.h
|
|
@ -31,7 +31,7 @@ struct config_t
|
||||||
std::string format_string;
|
std::string format_string;
|
||||||
std::string balance_format;
|
std::string balance_format;
|
||||||
std::string register_format;
|
std::string register_format;
|
||||||
std::string plot_value_format;
|
std::string plot_amount_format;
|
||||||
std::string plot_total_format;
|
std::string plot_total_format;
|
||||||
std::string print_format;
|
std::string print_format;
|
||||||
std::string equity_format;
|
std::string equity_format;
|
||||||
|
|
|
||||||
16
format.cc
16
format.cc
|
|
@ -130,8 +130,14 @@ element_t * format_t::parse_elements(const std::string& fmt)
|
||||||
case '(': {
|
case '(': {
|
||||||
++p;
|
++p;
|
||||||
const char * b = p;
|
const char * b = p;
|
||||||
while (*p && *p != ')')
|
int depth = 1;
|
||||||
|
while (*p) {
|
||||||
|
if (*p == ')' && --depth == 0)
|
||||||
|
break;
|
||||||
|
else if (*p == '(')
|
||||||
|
++depth;
|
||||||
p++;
|
p++;
|
||||||
|
}
|
||||||
if (*p != ')')
|
if (*p != ')')
|
||||||
throw format_error("Missing ')'");
|
throw format_error("Missing ')'");
|
||||||
|
|
||||||
|
|
@ -149,8 +155,14 @@ element_t * format_t::parse_elements(const std::string& fmt)
|
||||||
case '[': {
|
case '[': {
|
||||||
++p;
|
++p;
|
||||||
const char * b = p;
|
const char * b = p;
|
||||||
while (*p && *p != ']')
|
int depth = 1;
|
||||||
|
while (*p) {
|
||||||
|
if (*p == ']' && --depth == 0)
|
||||||
|
break;
|
||||||
|
else if (*p == '[')
|
||||||
|
++depth;
|
||||||
p++;
|
p++;
|
||||||
|
}
|
||||||
if (*p != ']')
|
if (*p != ']')
|
||||||
throw format_error("Missing ']'");
|
throw format_error("Missing ']'");
|
||||||
|
|
||||||
|
|
|
||||||
3
python.h
3
python.h
|
|
@ -1,6 +1,8 @@
|
||||||
#ifndef _PYTHON_H
|
#ifndef _PYTHON_H
|
||||||
#define _PYTHON_H
|
#define _PYTHON_H
|
||||||
|
|
||||||
|
#include "error.h"
|
||||||
|
|
||||||
#include <boost/python.hpp>
|
#include <boost/python.hpp>
|
||||||
|
|
||||||
using namespace boost::python;
|
using namespace boost::python;
|
||||||
|
|
@ -47,6 +49,7 @@ inline void python_eval(std::istream& in)
|
||||||
}
|
}
|
||||||
catch(const boost::python::error_already_set&) {
|
catch(const boost::python::error_already_set&) {
|
||||||
PyErr_Print();
|
PyErr_Print();
|
||||||
|
throw error("Evaluating Python code");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,3 @@
|
||||||
!python
|
|
||||||
def foo(d, val, x):
|
|
||||||
return d.xact.amount + val
|
|
||||||
!end
|
|
||||||
|
|
||||||
--amount-expr 'foo'({$100}, 20)
|
|
||||||
|
|
||||||
2004/05/01 Checking balance
|
2004/05/01 Checking balance
|
||||||
Assets:Checking $500.00
|
Assets:Checking $500.00
|
||||||
Equity:Opening Balances
|
Equity:Opening Balances
|
||||||
|
|
|
||||||
12
valexpr.cc
12
valexpr.cc
|
|
@ -303,7 +303,8 @@ void value_expr_t::compute(value_t& result, const details_t& details) const
|
||||||
}
|
}
|
||||||
else if (PyObject * err = PyErr_Occurred()) {
|
else if (PyObject * err = PyErr_Occurred()) {
|
||||||
PyErr_Print();
|
PyErr_Print();
|
||||||
result = 0;
|
throw value_expr_error(std::string("While calling Python function '") +
|
||||||
|
constant_s + "'");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
result = 0;
|
result = 0;
|
||||||
|
|
@ -319,7 +320,8 @@ void value_expr_t::compute(value_t& result, const details_t& details) const
|
||||||
}
|
}
|
||||||
catch(const boost::python::error_already_set&) {
|
catch(const boost::python::error_already_set&) {
|
||||||
PyErr_Print();
|
PyErr_Print();
|
||||||
result = 0;
|
throw value_expr_error(std::string("While calling Python function '") +
|
||||||
|
constant_s + "'");
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
break;
|
break;
|
||||||
|
|
@ -572,13 +574,13 @@ value_expr_t * parse_value_term(std::istream& in)
|
||||||
node->right = new value_expr_t(value_expr_t::O_ARG);
|
node->right = new value_expr_t(value_expr_t::O_ARG);
|
||||||
value_expr_t * cur = node->right;
|
value_expr_t * cur = node->right;
|
||||||
cur->left = parse_value_expr(in, true);
|
cur->left = parse_value_expr(in, true);
|
||||||
while (peek_next_nonws(in) == ',') {
|
in.get(c);
|
||||||
in.get(c);
|
while (! in.eof() && c == ',') {
|
||||||
cur->right = new value_expr_t(value_expr_t::O_ARG);
|
cur->right = new value_expr_t(value_expr_t::O_ARG);
|
||||||
cur = cur->right;
|
cur = cur->right;
|
||||||
cur->left = parse_value_expr(in, true);
|
cur->left = parse_value_expr(in, true);
|
||||||
|
in.get(c);
|
||||||
}
|
}
|
||||||
in.get(c);
|
|
||||||
if (c != ')')
|
if (c != ')')
|
||||||
unexpected(c, ')');
|
unexpected(c, ')');
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
1
value.cc
1
value.cc
|
|
@ -770,6 +770,7 @@ void export_value()
|
||||||
.def(self != int())
|
.def(self != int())
|
||||||
|
|
||||||
.def(! self)
|
.def(! self)
|
||||||
|
.def(int_(self))
|
||||||
|
|
||||||
.def(abs(self))
|
.def(abs(self))
|
||||||
.def(self_ns::str(self))
|
.def(self_ns::str(self))
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue