From b5a56207ba9fefd1152c5a7b30f4e615726bceaa Mon Sep 17 00:00:00 2001 From: Geert Janssens Date: Sat, 28 Apr 2012 13:36:55 +0000 Subject: [PATCH] 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 git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@22164 57a11ea4-9604-0410-9ed3-97b8803252fd --- .../new_book_with_opening_balances.py | 109 ++++++++++-------- 1 file changed, 64 insertions(+), 45 deletions(-) diff --git a/src/optional/python-bindings/example_scripts/new_book_with_opening_balances.py b/src/optional/python-bindings/example_scripts/new_book_with_opening_balances.py index 3689393071..e5787326c6 100644 --- a/src/optional/python-bindings/example_scripts/new_book_with_opening_balances.py +++ b/src/optional/python-bindings/example_scripts/new_book_with_opening_balances.py @@ -286,51 +286,70 @@ def create_opening_balance_transaction(commodtable, namespace, mnemonic, 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() - - 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 - ) - - (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() + + 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 + + #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() + + 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() + + 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 + ) + + (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__":