2019-04-04 14:50:05 +02:00
|
|
|
#!/usr/bin/env python3
|
2010-12-17 20:36:40 +00:00
|
|
|
## @file
|
|
|
|
|
# @brief Example Script simple session
|
2010-12-27 15:36:15 +00:00
|
|
|
# @ingroup python_bindings_examples
|
2008-07-07 19:18:26 +00:00
|
|
|
|
2020-06-12 12:24:05 +02:00
|
|
|
from gnucash import (
|
|
|
|
|
Session, GnuCashBackendException,
|
|
|
|
|
SessionOpenMode,
|
2008-07-07 19:18:26 +00:00
|
|
|
ERR_BACKEND_LOCKED, ERR_FILEIO_FILE_NOT_FOUND
|
2020-06-12 12:24:05 +02:00
|
|
|
)
|
2008-07-07 19:18:26 +00:00
|
|
|
|
|
|
|
|
FILE_1 = "/tmp/not_there.xac"
|
|
|
|
|
FILE_2 = "/tmp/example_file.xac"
|
|
|
|
|
|
|
|
|
|
# open a file that isn't there, detect the error
|
|
|
|
|
session = None
|
|
|
|
|
try:
|
2011-01-28 20:57:54 +00:00
|
|
|
session = Session(FILE_1)
|
2019-04-04 17:32:14 +02:00
|
|
|
except GnuCashBackendException as backend_exception:
|
2008-07-07 19:18:26 +00:00
|
|
|
assert( ERR_FILEIO_FILE_NOT_FOUND in backend_exception.errors)
|
|
|
|
|
|
|
|
|
|
|
2011-01-28 20:57:54 +00:00
|
|
|
# create a new file, this requires a file type specification
|
2020-06-12 12:24:05 +02:00
|
|
|
with Session("xml://%s" % FILE_2, SessionOpenMode.SESSION_NEW_STORE) as session:
|
2020-02-22 11:28:50 -06:00
|
|
|
book = session.book
|
|
|
|
|
root = book.get_root_account()
|
2008-07-07 19:18:26 +00:00
|
|
|
|
|
|
|
|
# open the new file, try to open it a second time, detect the lock
|
2020-02-22 11:28:50 -06:00
|
|
|
with Session(FILE_2) as session:
|
|
|
|
|
try:
|
|
|
|
|
session_2 = Session(FILE_2)
|
|
|
|
|
except GnuCashBackendException as backend_exception:
|
|
|
|
|
assert( ERR_BACKEND_LOCKED in backend_exception.errors )
|