mirror of
https://github.com/Gnucash/gnucash.git
synced 2025-02-25 18:55:30 -06:00
context manager examples
This commit is contained in:
@@ -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())
|
||||||
|
|
||||||
print("saving...")
|
#As long as there's no exceptions, book is automatically saved
|
||||||
ses.save()
|
#when session ends.
|
||||||
|
print("saving...")
|
||||||
|
|
||||||
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()
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user