GnuCash  5.6-150-g038405b370+
simple_session.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
5 
6 from gnucash import (
7  Session, GnuCashBackendException,
8  SessionOpenMode,
9  ERR_BACKEND_LOCKED, ERR_FILEIO_FILE_NOT_FOUND
10 )
11 
12 FILE_1 = "/tmp/not_there.xac"
13 FILE_2 = "/tmp/example_file.xac"
14 
15 # open a file that isn't there, detect the error
16 session = None
17 try:
18  session = Session(FILE_1)
19 except GnuCashBackendException as backend_exception:
20  assert( ERR_FILEIO_FILE_NOT_FOUND in backend_exception.errors)
21 
22 
23 # create a new file, this requires a file type specification
24 with Session("xml://%s" % FILE_2, SessionOpenMode.SESSION_NEW_STORE) as session:
25  book = session.book
26  root = book.get_root_account()
27 
28 # open the new file, try to open it a second time, detect the lock
29 with Session(FILE_2) as session:
30  try:
31  session_2 = Session(FILE_2)
32  except GnuCashBackendException as backend_exception:
33  assert( ERR_BACKEND_LOCKED in backend_exception.errors )