Bug #673877 - fixes and enhancements to example script account_analysis.py

a) added usage information when not enough arguments added
b) put the majority of code into an exception handling block so that if
something goes wrong the session is closed.  Prior to this change a problem
would result in a lingering lock.
Patch by Jamie Campbell

git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@22165 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
Geert Janssens 2012-04-28 13:37:07 +00:00
parent b5a56207ba
commit f18688e2da

View File

@ -138,6 +138,7 @@ def generate_period_boundaries(start_year, start_month, period_type, periods):
def account_from_path(top_account, account_path, original_path=None):
if original_path==None: original_path = account_path
account, account_path = account_path[0], account_path[1:]
account = top_account.lookup_by_name(account)
if account.get_instance() == None:
raise Exception(
@ -149,6 +150,18 @@ def account_from_path(top_account, account_path, original_path=None):
def main():
if len(argv) < 10:
print 'not enough parameters'
print 'usage: account_analysis.py {book url} {start year} {start month, numeric} {period type: monthly, quarterly, or yearly} {number of periods to show, from start year and month} {whether to show debits: debits-show for true, all other values false} {whether to show credits: credits-show for true, all other values false} {space separated account path, as many nested levels as desired} '
print 'examples:\n'
print "The following example analyzes 12 months of 'Assets:Test Account' from /home/username/test.gnucash, starting in January of 2010, and shows both credits and debits"
print "gnucash-env python account_analysis.py '/home/username/test.gnucash' 2010 1 monthly 12 debits-show credits-show Assets 'Test Account'\n"
print "The following example analyzes 2 quarters of 'Liabilities:First Level:Second Level' from /home/username/test.gnucash, starting March 2011, and shows credits but not debits"
print "gnucash-env python account_analysis.py '/home/username/test.gnucash' 2011 3 quarterly 2 debits-noshow credits-show Liabilities 'First Level' 'Second Level"
return
try:
(gnucash_file, start_year, start_month, period_type, periods,
debits_show, credits_show) = argv[1:8]
start_year, start_month, periods = [int(blah)
@ -254,7 +267,11 @@ def main():
# no save needed, we're just reading..
gnucash_session.end()
except:
if not gnucash_session == None:
gnucash_session.end()
raise
if __name__ == "__main__": main()