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
|
|
|
|
|
|
|
|
from gnucash import \
|
|
|
|
|
Session, GnuCashBackendException, \
|
|
|
|
|
ERR_BACKEND_LOCKED, ERR_FILEIO_FILE_NOT_FOUND
|
|
|
|
|
|
|
|
|
|
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
|
2010-11-16 22:07:58 +00:00
|
|
|
session = Session("xml://%s" % FILE_2, is_new=True)
|
2008-07-07 19:18:26 +00:00
|
|
|
session.save()
|
|
|
|
|
session.end()
|
|
|
|
|
session.destroy()
|
|
|
|
|
|
|
|
|
|
# open the new file, try to open it a second time, detect the lock
|
2011-01-28 20:57:54 +00:00
|
|
|
session = Session(FILE_2)
|
2008-07-07 19:18:26 +00:00
|
|
|
try:
|
2011-01-28 20:57:54 +00:00
|
|
|
session_2 = Session(FILE_2)
|
2019-04-04 17:32:14 +02:00
|
|
|
except GnuCashBackendException as backend_exception:
|
2008-07-07 19:18:26 +00:00
|
|
|
assert( ERR_BACKEND_LOCKED in backend_exception.errors )
|
|
|
|
|
session.end()
|
|
|
|
|
session.destroy()
|
|
|
|
|
|
|
|
|
|
|