Attached is a patch which fixes the python bindings support for
Transaction.GetImbalance() [xaccTransGetImbalance() ] . Right now the
value returned by the underlying C function is wrapped around
GncNumeric, whereas the C API has since changed to return MonetaryList *.

This patch adds support for MonetaryList * in src/base-typemaps.i and
uses that support in src/optional/python-bindings/gnucash_core.py with
some enhanced wrapping such that Transaction.GetImbalance() returns a
list of GncCommodity and GncNumeric pairs. A little bit of documentation
is included.

Transaction.GetImbalanceValue() is also wrapped with GncNumeric, and an
example script showing GetImbalance() and GetImbalanceValue() in action
is provided.


Mark Jenkins
Member
ParIT Worker Co-op

cc fellow ParIT members
cc Scott
Index: src/optional/python-bindings/example_scripts/test_imbalance_transaction.py
===================================================================
--- src/optional/python-bindings/example_scripts/test_imbalance_transaction.py	(revision 0)
+++ src/optional/python-bindings/example_scripts/test_imbalance_transaction.py	(revision 0)
@@ -0,0 +1,81 @@
+#!/usr/bin/env python
+
+# test_imbalance_transaction.py -- Test the transaction imbalace viewing
+# mechanisms
+#
+# Copyright (C) 2010 ParIT Worker Co-operative <transpare...@parit.ca>
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; either version 2 of
+# the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, contact:
+# Free Software Foundation           Voice:  +1-617-542-5942
+# 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
+# Boston, MA  02110-1301,  USA       g...@gnu.org
+#
+# @author Mark Jenkins, ParIT Worker Co-operative <m...@parit.ca>
+
+from sys import argv
+
+from gnucash import Session, Transaction, Split, Account, GncNumeric, \
+    GncCommodity
+
+# must be sqlite:///path_to_file or xml:///path_to_file
+# and must be an existing gnucash file
+#
+# You should try it out with a gnucash file with tranding accounts enabled
+# and trading accounts disabled
+session = Session(argv[1])
+book = session.book
+
+root = book.get_root_account()
+root.get_instance()
+commod_tab = session.book.get_table()
+CAD = commod_tab.lookup("ISO4217","CAD")
+USD = commod_tab.lookup("ISO4217","USD")
+account = Account(book)
+account2 = Account(book)
+root.append_child(account)
+root.append_child(account2)
+account.SetCommodity(CAD)
+account.SetName("blahblah")
+account.SetType(3)
+account2.SetCommodity(USD)
+account2.SetName("blahblahsdfs ")
+account2.SetType(3)
+
+a = Transaction(book)
+a.BeginEdit()
+
+s = Split(book)
+s.SetParent(a)
+s2 = Split(book)
+s2.SetParent(a)
+
+a.SetCurrency(CAD)
+s.SetAccount(account)
+s.SetValue(GncNumeric(2))
+s.SetAmount(GncNumeric(2))
+
+s2.SetAccount(account2)
+s2.SetValue(GncNumeric(4))
+s2.SetAmount(GncNumeric(4))
+print 'overall imbalance', a.GetImbalanceValue().to_string()
+
+print 'per-currency imbalances'
+imbalance_list = a.GetImbalance()
+for (commod, value) in imbalance_list:
+    print value.to_string(), commod.get_mnemonic()
+
+a.CommitEdit()
+
+
+session.end()
+session.destroy()
Index: src/optional/python-bindings/gnucash_core.py
===================================================================
--- src/optional/python-bindings/gnucash_core.py	(revision 568)
+++ src/optional/python-bindings/gnucash_core.py	(working copy)
@@ -251,6 +251,15 @@
         return self.do_lookup_create_oo_instance(
             gncInvoiceGetInvoiceFromTxn, Transaction )
 
+def decorate_monetary_list_returning_function(orig_function):
+    def new_function(self):
+        # warning, item.commodity has been shown to be None
+        # when the transaction doesn't have a currency
+        return [(GncCommodity(instance=item.commodity),
+                 GncNumeric(instance=item.value))
+                for item in orig_function(self) ]
+    return new_function
+
 class Split(GnuCashCoreClass):
     """A GnuCash Split
 
@@ -402,7 +411,7 @@
                     'Clone': Transaction, 
                     'Reverse': Transaction, 
                     'GetReversedBy': Transaction, 
-                    'GetImbalance': GncNumeric, 
+                    'GetImbalanceValue': GncNumeric, 
                     'GetAccountValue': GncNumeric, 
                     'GetAccountAmount': GncNumeric, 
                     'GetAccountConvRate': GncNumeric, 
@@ -411,7 +420,17 @@
                     'GetGUID': GUID
                 }
 methods_return_instance(Transaction, trans_dict)
+Transaction.decorate_functions(
+    decorate_monetary_list_returning_function, 'GetImbalance')
+Transaction.GetImbalance.__doc__ = \
+    """Returns a list of all the imbalanced currencies. Each list item
+is a two element tuple, the first element is the imbalanced commodity,
+the second element is the value.
 
+Warning, the commodity.get_instance() value can be None when there
+is no currency set for the transaction.
+"""
+
 # Split
 Split.add_methods_with_prefix('xaccSplit')
 Split.add_method('gncSplitGetGUID', 'GetGUID');
Index: src/optional/python-bindings/Makefile.am
===================================================================
--- src/optional/python-bindings/Makefile.am	(revision 568)
+++ src/optional/python-bindings/Makefile.am	(working copy)
@@ -56,6 +56,7 @@
   example_scripts/change_tax_code.py \
   example_scripts/account_analysis.py \
   example_scripts/new_book_with_opening_balances.py \
+  example_scripts/test_imbalance_transaction.py \
   glib.i
 
 MAINTAINERCLEANFILES = gnucash-core.c
Index: src/base-typemaps.i
===================================================================
--- src/base-typemaps.i	(revision 568)
+++ src/base-typemaps.i	(working copy)
@@ -130,7 +130,8 @@
     }
 }
 
-%typemap(out) GList *, CommodityList *, SplitList *, AccountList *, LotList * {
+%typemap(out) GList *, CommodityList *, SplitList *, AccountList *, LotList *,
+    MonetaryList * {
     guint i;
     gpointer data;
     PyObject *list = PyList_New(0);
@@ -146,7 +147,10 @@
         else if (GNC_IS_COMMODITY(data))
             PyList_Append(list, SWIG_NewPointerObj(data, SWIGTYPE_p_gnc_commodity, 0)); 
         else if (GNC_IS_LOT(data))
-            PyList_Append(list, SWIG_NewPointerObj(data, SWIGTYPE_p_GNCLot, 0)); 
+            PyList_Append(list, SWIG_NewPointerObj(data, SWIGTYPE_p_GNCLot, 0));
+        else if ($1_descriptor == $descriptor(MonetaryList *)){
+	    PyList_Append(list, SWIG_NewPointerObj(data, $descriptor(gnc_monetary *), 0));
+	}
         else
             PyList_Append(list, SWIG_NewPointerObj(data, SWIGTYPE_p_void, 0)); 
     }
_______________________________________________
gnucash-devel mailing list
gnucash-devel@gnucash.org
https://lists.gnucash.org/mailman/listinfo/gnucash-devel

Reply via email to