mirror of
https://github.com/Gnucash/gnucash.git
synced 2024-11-22 17:06:36 -06:00
quality-of-life improvements for python bindings
This commit is contained in:
commit
ab2edfccf1
@ -11,15 +11,15 @@ from gnucash import Session
|
|||||||
uri = "xml:///tmp/simple_book.gnucash"
|
uri = "xml:///tmp/simple_book.gnucash"
|
||||||
|
|
||||||
print("uri:", uri)
|
print("uri:", uri)
|
||||||
ses = Session(uri, is_new=True)
|
with Session(uri, is_new=True) as ses:
|
||||||
book = ses.get_book()
|
book = ses.get_book()
|
||||||
|
|
||||||
#Call some methods that produce output to show that Book works
|
#Call some methods that produce output to show that Book works
|
||||||
book.get_root_account().SetDescription("hello, book")
|
book.get_root_account().SetDescription("hello, book")
|
||||||
print("Book is saved:", not book.session_not_saved())
|
print("Book is saved:", not book.session_not_saved())
|
||||||
|
|
||||||
|
#As long as there's no exceptions, book is automatically saved
|
||||||
|
#when session ends.
|
||||||
print("saving...")
|
print("saving...")
|
||||||
ses.save()
|
|
||||||
|
|
||||||
print("Book is saved:", not book.session_not_saved())
|
print("Book is saved:", not book.session_not_saved())
|
||||||
ses.end()
|
|
||||||
|
@ -19,18 +19,13 @@ except GnuCashBackendException as backend_exception:
|
|||||||
|
|
||||||
|
|
||||||
# create a new file, this requires a file type specification
|
# create a new file, this requires a file type specification
|
||||||
session = Session("xml://%s" % FILE_2, is_new=True)
|
with Session("xml://%s" % FILE_2, is_new=True) as session:
|
||||||
session.save()
|
book = session.book
|
||||||
session.end()
|
root = book.get_root_account()
|
||||||
session.destroy()
|
|
||||||
|
|
||||||
# open the new file, try to open it a second time, detect the lock
|
# open the new file, try to open it a second time, detect the lock
|
||||||
session = Session(FILE_2)
|
with Session(FILE_2) as session:
|
||||||
try:
|
try:
|
||||||
session_2 = Session(FILE_2)
|
session_2 = Session(FILE_2)
|
||||||
except GnuCashBackendException as backend_exception:
|
except GnuCashBackendException as backend_exception:
|
||||||
assert( ERR_BACKEND_LOCKED in backend_exception.errors )
|
assert( ERR_BACKEND_LOCKED in backend_exception.errors )
|
||||||
session.end()
|
|
||||||
session.destroy()
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@ from gnucash import Session, Account, Transaction, Split, GncNumeric
|
|||||||
|
|
||||||
FILE_1 = "/tmp/example.gnucash"
|
FILE_1 = "/tmp/example.gnucash"
|
||||||
|
|
||||||
session = Session("xml://%s" % FILE_1, is_new=True)
|
with Session("xml://%s" % FILE_1, is_new=True) as session:
|
||||||
|
|
||||||
book = session.book
|
book = session.book
|
||||||
root_acct = Account(book)
|
root_acct = Account(book)
|
||||||
@ -81,7 +81,3 @@ split4.SetValue(num2.neg())
|
|||||||
|
|
||||||
trans1.CommitEdit()
|
trans1.CommitEdit()
|
||||||
trans2.CommitEdit()
|
trans2.CommitEdit()
|
||||||
|
|
||||||
session.save()
|
|
||||||
session.end()
|
|
||||||
session.destroy()
|
|
||||||
|
@ -117,6 +117,15 @@ class Session(GnuCashCoreClass):
|
|||||||
self.destroy()
|
self.destroy()
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(self, exc_type, exc_value, traceback):
|
||||||
|
# Roll back changes on exception by not calling save. Only works for XMl backend.
|
||||||
|
if not exc_type:
|
||||||
|
self.save()
|
||||||
|
self.end()
|
||||||
|
|
||||||
def raise_backend_errors(self, called_function="qof_session function"):
|
def raise_backend_errors(self, called_function="qof_session function"):
|
||||||
"""Raises a GnuCashBackendException if there are outstanding
|
"""Raises a GnuCashBackendException if there are outstanding
|
||||||
QOF_BACKEND errors.
|
QOF_BACKEND errors.
|
||||||
@ -434,6 +443,9 @@ class Transaction(GnuCashCoreClass):
|
|||||||
return self.do_lookup_create_oo_instance(
|
return self.do_lookup_create_oo_instance(
|
||||||
gncInvoiceGetInvoiceFromTxn, Transaction )
|
gncInvoiceGetInvoiceFromTxn, Transaction )
|
||||||
|
|
||||||
|
def __eq__(self, other):
|
||||||
|
return self.Equal(other, True, False, False, False)
|
||||||
|
|
||||||
def decorate_monetary_list_returning_function(orig_function):
|
def decorate_monetary_list_returning_function(orig_function):
|
||||||
def new_function(self, *args):
|
def new_function(self, *args):
|
||||||
"""decorate function that returns list of gnc_monetary to return tuples of GncCommodity and GncNumeric
|
"""decorate function that returns list of gnc_monetary to return tuples of GncCommodity and GncNumeric
|
||||||
@ -461,6 +473,9 @@ class Split(GnuCashCoreClass):
|
|||||||
"""
|
"""
|
||||||
_new_instance = 'xaccMallocSplit'
|
_new_instance = 'xaccMallocSplit'
|
||||||
|
|
||||||
|
def __eq__(self, other):
|
||||||
|
return self.Equal(other, True, False, False)
|
||||||
|
|
||||||
class Account(GnuCashCoreClass):
|
class Account(GnuCashCoreClass):
|
||||||
"""A GnuCash Account.
|
"""A GnuCash Account.
|
||||||
|
|
||||||
|
@ -57,6 +57,11 @@ class TestSplit( SplitSession ):
|
|||||||
def test_equal(self):
|
def test_equal(self):
|
||||||
COPY = self.split
|
COPY = self.split
|
||||||
self.assertTrue( self.split.Equal(COPY, True, False, False) )
|
self.assertTrue( self.split.Equal(COPY, True, False, False) )
|
||||||
|
# test __eq__ implementation
|
||||||
|
TRANS = Transaction(self.book)
|
||||||
|
self.split.SetParent(TRANS)
|
||||||
|
self.assertTrue( self.split == TRANS.GetSplitList()[0] )
|
||||||
|
self.assertTrue( self.split != Split(self.book) )
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
@ -44,6 +44,11 @@ class TestTransaction( TransactionSession ):
|
|||||||
def test_equal(self):
|
def test_equal(self):
|
||||||
TRANS = self.trans
|
TRANS = self.trans
|
||||||
self.assertTrue( TRANS.Equal(self.trans, True, False, False, False) )
|
self.assertTrue( TRANS.Equal(self.trans, True, False, False, False) )
|
||||||
|
# test __eq__ implementation
|
||||||
|
SPLIT = Split(self.book)
|
||||||
|
SPLIT.SetParent(TRANS)
|
||||||
|
self.assertTrue( self.trans == SPLIT.GetParent() )
|
||||||
|
self.assertTrue( self.trans != Transaction(self.book) )
|
||||||
|
|
||||||
def test_clone(self):
|
def test_clone(self):
|
||||||
domain = "gnc.engine"
|
domain = "gnc.engine"
|
||||||
|
Loading…
Reference in New Issue
Block a user