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__":