ledger.so can now call back to functions in the calling interpretor (see main.py)

This commit is contained in:
John Wiegley 2004-09-14 05:14:58 -04:00
parent bd72c0cf90
commit 0b0c9b5bd1
4 changed files with 18 additions and 9 deletions

View file

@ -56,7 +56,7 @@ noinst_LIBRARIES = libledger_bpy.a
libledger_bpy_a_SOURCES = amount.cc balance.cc value.cc autoxact.cc \ libledger_bpy_a_SOURCES = amount.cc balance.cc value.cc autoxact.cc \
binary.cc config.cc datetime.cc format.cc journal.cc option.cc \ binary.cc config.cc datetime.cc format.cc journal.cc option.cc \
parser.cc qif.cc quotes.cc textual.cc valexpr.cc walk.cc python.cc parser.cc qif.cc quotes.cc textual.cc valexpr.cc walk.cc python.cc
libledger_bpy_a_CXXFLAGS = -DUSE_BOOST_PYTHON=1 libledger_bpy_a_CXXFLAGS = -DUSE_BOOST_PYTHON=1 -DPYTHON_MODULE=1
if READ_GNUCASH if READ_GNUCASH
libledger_bpy_a_SOURCES += gnucash.cc libledger_bpy_a_SOURCES += gnucash.cc
endif endif

13
main.py
View file

@ -4,13 +4,13 @@ import time
from ledger import * from ledger import *
def foo (str): def hello (str):
print "Hello:", str print "Hello:", str
def bar (str): def goodbye (str):
print "Goodbye:", str print "Goodbye:", str
register_option ("hello", "h:", foo) register_option ("hello", "h:", hello)
register_option ("goodbye", "g:", bar) register_option ("goodbye", "g:", goodbye)
args = process_arguments (sys.argv[1:]) args = process_arguments (sys.argv[1:])
process_environment (os.environ, "TEST_") process_environment (os.environ, "TEST_")
@ -28,7 +28,10 @@ class FormatTransaction (TransactionHandler):
def __call__ (self, xact): def __call__ (self, xact):
print self.formatter.format(xact) print self.formatter.format(xact)
handler = FormatTransaction("%D %-20P %N %('foo'100)") def foo(d, val):
return d.xact.amount + val
handler = FormatTransaction("%D %-20P %N %('foo'{$100})")
handler = FilterTransactions (handler, "/Checking/") handler = FilterTransactions (handler, "/Checking/")
expr = parse_value_expr ("a*2") expr = parse_value_expr ("a*2")

View file

@ -27,6 +27,8 @@ namespace ledger {
python_support * python_interpretor = NULL; python_support * python_interpretor = NULL;
#ifndef PYTHON_MODULE
static struct cleanup_python { static struct cleanup_python {
~cleanup_python() { ~cleanup_python() {
if (python_interpretor) { if (python_interpretor) {
@ -56,14 +58,18 @@ void init_module()
export_datetime(); export_datetime();
} }
#endif // PYTHON_MODULE
void init_python() void init_python()
{ {
assert(! python_interpretor); assert(! python_interpretor);
#ifndef PYTHON_MODULE
Py_Initialize(); Py_Initialize();
python_interpretor = new python_support;
detail::init_module("ledger", &init_module); detail::init_module("ledger", &init_module);
#endif
python_interpretor = new python_support;
} }
} // namespace ledger } // namespace ledger

View file

@ -4,7 +4,7 @@
; !python ; !python
; from ledger import * ; from ledger import *
; def foo(d, val): ; def foo(d, val):
; return val + d.xact.amount ; return d.xact.amount + val
; !end ; !end
; ;
; --value-expr 'foo'{$100} ; --value-expr 'foo'{$100}