mirror of
https://github.com/Gnucash/gnucash.git
synced 2025-02-25 18:55:30 -06:00
make Session a context manager
This commit is contained in:
@@ -7,7 +7,7 @@ from gnucash import Session, Account, Transaction, Split, GncNumeric
|
||||
|
||||
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
|
||||
root_acct = Account(book)
|
||||
@@ -81,7 +81,3 @@ split4.SetValue(num2.neg())
|
||||
|
||||
trans1.CommitEdit()
|
||||
trans2.CommitEdit()
|
||||
|
||||
session.save()
|
||||
session.end()
|
||||
session.destroy()
|
||||
|
||||
@@ -117,6 +117,15 @@ class Session(GnuCashCoreClass):
|
||||
self.destroy()
|
||||
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"):
|
||||
"""Raises a GnuCashBackendException if there are outstanding
|
||||
QOF_BACKEND errors.
|
||||
|
||||
Reference in New Issue
Block a user