GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
simple_session.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 ## @file
3 # @brief Example Script simple session
4 # @ingroup python_bindings_examples
5 
6 from gnucash import \
7  Session, GnuCashBackendException, \
8  ERR_BACKEND_LOCKED, ERR_FILEIO_FILE_NOT_FOUND
9 
10 FILE_1 = "/tmp/not_there.xac"
11 FILE_2 = "/tmp/example_file.xac"
12 
13 # open a file that isn't there, detect the error
14 session = None
15 try:
16  session = Session(FILE_1)
17 except GnuCashBackendException, backend_exception:
18  assert( ERR_FILEIO_FILE_NOT_FOUND in backend_exception.errors)
19 
20 
21 # create a new file, this requires a file type specification
22 session = Session("xml://%s" % FILE_2, is_new=True)
23 session.save()
24 session.end()
25 session.destroy()
26 
27 # open the new file, try to open it a second time, detect the lock
28 session = Session(FILE_2)
29 try:
30  session_2 = Session(FILE_2)
31 except GnuCashBackendException, backend_exception:
32  assert( ERR_BACKEND_LOCKED in backend_exception.errors )
33 session.end()
34 session.destroy()
35 
36