GnuCash c935c2f+
Loading...
Searching...
No Matches
simple_session.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2
5
6from gnucash import (
7 Session, GnuCashBackendException,
8 SessionOpenMode,
9 ERR_BACKEND_LOCKED, ERR_FILEIO_FILE_NOT_FOUND
10)
11
12FILE_1 = "/tmp/not_there.xac"
13FILE_2 = "/tmp/example_file.xac"
14
15# open a file that isn't there, detect the error
16session = None
17try:
18 session = Session(FILE_1)
19except 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
24with 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
29with 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 )