summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Wiegley <johnw@newartisans.com>2004-09-14 05:14:58 -0400
committerJohn Wiegley <johnw@newartisans.com>2004-09-14 05:14:58 -0400
commit0b0c9b5bd13b1dedf100b137e3a35be60d3d3a27 (patch)
treea021447b83f7aff6c0072124a53b330242b55539
parentbd72c0cf908492abb99a1419f7f8136729747061 (diff)
downloadfork-ledger-0b0c9b5bd13b1dedf100b137e3a35be60d3d3a27.tar.gz
fork-ledger-0b0c9b5bd13b1dedf100b137e3a35be60d3d3a27.tar.bz2
fork-ledger-0b0c9b5bd13b1dedf100b137e3a35be60d3d3a27.zip
ledger.so can now call back to functions in the calling interpretor (see main.py)
-rw-r--r--Makefile.am2
-rw-r--r--main.py13
-rw-r--r--python.cc10
-rw-r--r--sample.dat2
4 files changed, 18 insertions, 9 deletions
diff --git a/Makefile.am b/Makefile.am
index 098088ee..41c25d4e 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -56,7 +56,7 @@ noinst_LIBRARIES = libledger_bpy.a
libledger_bpy_a_SOURCES = amount.cc balance.cc value.cc autoxact.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
-libledger_bpy_a_CXXFLAGS = -DUSE_BOOST_PYTHON=1
+libledger_bpy_a_CXXFLAGS = -DUSE_BOOST_PYTHON=1 -DPYTHON_MODULE=1
if READ_GNUCASH
libledger_bpy_a_SOURCES += gnucash.cc
endif
diff --git a/main.py b/main.py
index 7cf02c2b..7427961d 100644
--- a/main.py
+++ b/main.py
@@ -4,13 +4,13 @@ import time
from ledger import *
-def foo (str):
+def hello (str):
print "Hello:", str
-def bar (str):
+def goodbye (str):
print "Goodbye:", str
-register_option ("hello", "h:", foo)
-register_option ("goodbye", "g:", bar)
+register_option ("hello", "h:", hello)
+register_option ("goodbye", "g:", goodbye)
args = process_arguments (sys.argv[1:])
process_environment (os.environ, "TEST_")
@@ -28,7 +28,10 @@ class FormatTransaction (TransactionHandler):
def __call__ (self, 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/")
expr = parse_value_expr ("a*2")
diff --git a/python.cc b/python.cc
index 288825aa..980df95c 100644
--- a/python.cc
+++ b/python.cc
@@ -27,6 +27,8 @@ namespace ledger {
python_support * python_interpretor = NULL;
+#ifndef PYTHON_MODULE
+
static struct cleanup_python {
~cleanup_python() {
if (python_interpretor) {
@@ -56,14 +58,18 @@ void init_module()
export_datetime();
}
+#endif // PYTHON_MODULE
+
void init_python()
{
assert(! python_interpretor);
+#ifndef PYTHON_MODULE
Py_Initialize();
- python_interpretor = new python_support;
-
detail::init_module("ledger", &init_module);
+#endif
+
+ python_interpretor = new python_support;
}
} // namespace ledger
diff --git a/sample.dat b/sample.dat
index 69379382..7460cd4b 100644
--- a/sample.dat
+++ b/sample.dat
@@ -4,7 +4,7 @@
; !python
; from ledger import *
; def foo(d, val):
-; return val + d.xact.amount
+; return d.xact.amount + val
; !end
;
; --value-expr 'foo'{$100}