r22164 - gnucash/trunk/src/optional/python-bindings/example_scripts - Bug #673855 - fixes and enhancements to example script new_book_with_opening_balances.py

Geert Janssens gjanssens at code.gnucash.org
Sat Apr 28 09:36:56 EDT 2012


Author: gjanssens
Date: 2012-04-28 09:36:55 -0400 (Sat, 28 Apr 2012)
New Revision: 22164
Trac: http://svn.gnucash.org/trac/changeset/22164

Modified:
   gnucash/trunk/src/optional/python-bindings/example_scripts/new_book_with_opening_balances.py
Log:
Bug #673855 - fixes and enhancements to example script new_book_with_opening_balances.py
1) Fixed typo for "new book" session creation in_new=True -> is_new=True
2) Added usage information echo when script is invoked without correct number
of parameters
3) Put most of the operations into an exception handling block.  When an
exception occurs, sessions which were opened are closed.  Prior to this, any
error would result in a lingering lock.
Patch by Jamie Campbell

Modified: gnucash/trunk/src/optional/python-bindings/example_scripts/new_book_with_opening_balances.py
===================================================================
--- gnucash/trunk/src/optional/python-bindings/example_scripts/new_book_with_opening_balances.py	2012-04-26 07:08:11 UTC (rev 22163)
+++ gnucash/trunk/src/optional/python-bindings/example_scripts/new_book_with_opening_balances.py	2012-04-28 13:36:55 UTC (rev 22164)
@@ -286,51 +286,70 @@
     return simple_opening_name_used
         
 def main():
-    original_book_session = Session(argv[1], is_new=False)
-    new_book_session = Session(argv[2], in_new=True)
-    new_book = new_book_session.get_book()
-    new_book_root = new_book.get_root_account()
 
-    commodtable = new_book.get_table()
-    # we discovered that if we didn't have this save early on, there would
-    # be trouble later
-    new_book_session.save()
+    if len(argv) < 3:
+        print 'not enough parameters'
+        print 'usage: new_book_with_opening_balances.py {source_book_url} {destination_book_url}'
+        print 'examples:'
+        print "gnucash-env python new_book_with_opening_balances.py '/home/username/test.gnucash' 'sqlite3:///home/username/new_test.gnucash'"
+        print "gnucash-env python new_book_with_opening_balances.py '/home/username/test.gnucash' 'xml:///crypthome/username/finances/new_test.gnucash'"
+        return
 
-    opening_balance_per_currency = {}
-    recursivly_build_account_tree(
-        original_book_session.get_book().get_root_account(),
-        new_book_root,
-        new_book,
-        commodtable,
-        opening_balance_per_currency,
-        ACCOUNT_TYPES_TO_OPEN
-        )
+    #have everything in a try block to unable us to release our hold on stuff to the extent possible
+    try:
+        original_book_session = Session(argv[1], is_new=False)
+        new_book_session = Session(argv[2], is_new=True)
+        new_book = new_book_session.get_book()
+        new_book_root = new_book.get_root_account()
 
-    (namespace, mnemonic) = PREFERED_CURRENCY_FOR_SIMPLE_OPENING_BALANCE
-    if (namespace, mnemonic) in opening_balance_per_currency:
-        opening_trans, opening_amount = opening_balance_per_currency[
-            (namespace, mnemonic)]
-        simple_opening_name_used = create_opening_balance_transaction(
-            commodtable, namespace, mnemonic,
-            new_book_root, new_book,
-            opening_trans, opening_amount,
-            False )
-        del opening_balance_per_currency[
-            PREFERED_CURRENCY_FOR_SIMPLE_OPENING_BALANCE]
-    else:
-        simple_opening_name_used = False
+        commodtable = new_book.get_table()
+        # we discovered that if we didn't have this save early on, there would
+        # be trouble later
+        new_book_session.save()
 
-    for (namespace, mnemonic), (opening_trans, opening_amount) in \
-            opening_balance_per_currency.iteritems() :
-        simple_opening_name_used = create_opening_balance_transaction(
-            commodtable, namespace, mnemonic,
-            new_book_root, new_book,
-            opening_trans, opening_amount,
-            simple_opening_name_used )
+        opening_balance_per_currency = {}
+        recursivly_build_account_tree(
+            original_book_session.get_book().get_root_account(),
+            new_book_root,
+            new_book,
+            commodtable,
+            opening_balance_per_currency,
+            ACCOUNT_TYPES_TO_OPEN
+            )
 
-    new_book_session.save()
-    new_book_session.end()
-    original_book_session.end()
+        (namespace, mnemonic) = PREFERED_CURRENCY_FOR_SIMPLE_OPENING_BALANCE
+        if (namespace, mnemonic) in opening_balance_per_currency:
+            opening_trans, opening_amount = opening_balance_per_currency[
+                (namespace, mnemonic)]
+            simple_opening_name_used = create_opening_balance_transaction(
+                commodtable, namespace, mnemonic,
+                new_book_root, new_book,
+                opening_trans, opening_amount,
+                False )
+            del opening_balance_per_currency[
+                PREFERED_CURRENCY_FOR_SIMPLE_OPENING_BALANCE]
+        else:
+            simple_opening_name_used = False
+
+        for (namespace, mnemonic), (opening_trans, opening_amount) in \
+                opening_balance_per_currency.iteritems() :
+            simple_opening_name_used = create_opening_balance_transaction(
+                commodtable, namespace, mnemonic,
+                new_book_root, new_book,
+                opening_trans, opening_amount,
+                simple_opening_name_used )
+
+        new_book_session.save()
+        new_book_session.end()
+        original_book_session.end()
+    except:
+        if not original_book_session == None:
+            original_book_session.end()
+ 
+        if not new_book_session == None:
+            new_book_session.end()
+
+        raise
     
 
 if __name__ == "__main__":



More information about the gnucash-changes mailing list