GnuCash c935c2f+
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions
python.gnucash_core.Session Class Reference
Inheritance diagram for python.gnucash_core.Session:
python.gnucash_core.GnuCashCoreClass

Public Member Functions

 __init__ (self, book_uri=None, mode=None, instance=None, book=None)
 A convenient constructor that allows you to specify a book URI, begin the session, and load the book.
 
 __enter__ (self)
 
 __exit__ (self, exc_type, exc_value, traceback)
 
 raise_backend_errors (self, called_function="qof_session function")
 
 generate_errors (self)
 
 pop_all_errors (self)
 
- Public Member Functions inherited from python.gnucash_core.GnuCashCoreClass
 do_lookup_create_oo_instance (self, lookup_function, cls, *args)
 

Static Public Member Functions

 raise_backend_errors_after_call (function, *args, **kwargs)
 

Additional Inherited Members

- Static Protected Attributes inherited from python.gnucash_core.GnuCashCoreClass
 _module = gnucash_core_c
 

Detailed Description

A GnuCash book editing session

To commit changes to the session you may need to call save,
(this is always the case with the file backend).

When you're down with a session you may need to call end()

Every Session has a Book in the book attribute, which you'll definitely
be interested in, as every GnuCash entity (Transaction, Split, Vendor,
Invoice..) is associated with a particular book where it is stored.

Definition at line 278 of file gnucash_core.py.

Constructor & Destructor Documentation

◆ __init__()

python.gnucash_core.Session.__init__ (   self,
  book_uri = None,
  mode = None,
  instance = None,
  book = None 
)

A convenient constructor that allows you to specify a book URI, begin the session, and load the book.

This can give you the power of calling qof_session_new, qof_session_begin, and qof_session_load all in one!

qof_session_load is only called if url scheme is "xml" and mode is SESSION_NEW_STORE or SESSION_NEW_OVERWRITE

Parameters
book_urimust be a string in the form of a URI/URL. The access method specified depends on the loaded backends. Paths may be relative or absolute. If the path is relative, that is if the argument is "file://somefile.xml", then the current working directory is assumed. Customized backends can choose to search other application-specific directories or URI schemes as well. It be None to skip the calls to qof_session_begin and qof_session_load.
instanceargument can be passed if new Session is used as a wrapper for an existing session instance
modeThe SessionOpenMode.
Note
SessionOpenMode replaces deprecated ignore_lock, is_new and force_new.
SessionOpenMode
SESSION_NORMAL_OPEN: Find an existing file or database at the provided uri and open it if it is unlocked. If it is locked post a QOF_BACKEND_LOCKED error.
SESSION_NEW_STORE: Check for an existing file or database at the provided uri and if none is found, create it. If the file or database exists post a QOF_BACKED_STORE_EXISTS and return.
SESSION_NEW_OVERWRITE: Create a new file or database at the provided uri, deleting any existing file or database.
SESSION_READ_ONLY: Find an existing file or database and open it without disturbing the lock if it exists or setting one if not. This will also set a flag on the book that will prevent many elements from being edited and will prevent the backend from saving any edits.
SESSION_BREAK_LOCK: Find an existing file or database, lock it, and open it. If there is already a lock replace it with a new one for this session.
Errors
qof_session_begin() signals failure by queuing errors. After it completes use qof_session_get_error() and test that the value is ERROR_BACKEND_NONE to determine that the session began successfully.
Exceptions
asbegin() and load() are wrapped with raise_backend_errors_after_call() this function can raise a GnuCashBackendException. If it does, you don't need to cleanup and call end() and destroy(), that is handled for you, and the exception is raised.

Definition at line 292 of file gnucash_core.py.

292 def __init__(self, book_uri=None, mode=None, instance=None, book=None):
293 """!
294 A convenient constructor that allows you to specify a book URI,
295 begin the session, and load the book.
296
297 This can give you the power of calling
298 qof_session_new, qof_session_begin, and qof_session_load all in one!
299
300 qof_session_load is only called if url scheme is "xml" and
301 mode is SESSION_NEW_STORE or SESSION_NEW_OVERWRITE
302
303 @param book_uri must be a string in the form of a URI/URL. The access
304 method specified depends on the loaded backends. Paths may be relative
305 or absolute. If the path is relative, that is if the argument is
306 "file://somefile.xml", then the current working directory is
307 assumed. Customized backends can choose to search other
308 application-specific directories or URI schemes as well.
309 It be None to skip the calls to qof_session_begin and
310 qof_session_load.
311
312 @param instance argument can be passed if new Session is used as a
313 wrapper for an existing session instance
314
315 @param mode The SessionOpenMode.
316 @note SessionOpenMode replaces deprecated ignore_lock, is_new and force_new.
317
318 @par SessionOpenMode
319 `SESSION_NORMAL_OPEN`: Find an existing file or database at the provided uri and
320 open it if it is unlocked. If it is locked post a QOF_BACKEND_LOCKED error.
321 @par
322 `SESSION_NEW_STORE`: Check for an existing file or database at the provided
323 uri and if none is found, create it. If the file or database exists post a
324 QOF_BACKED_STORE_EXISTS and return.
325 @par
326 `SESSION_NEW_OVERWRITE`: Create a new file or database at the provided uri,
327 deleting any existing file or database.
328 @par
329 `SESSION_READ_ONLY`: Find an existing file or database and open it without
330 disturbing the lock if it exists or setting one if not. This will also set a
331 flag on the book that will prevent many elements from being edited and will
332 prevent the backend from saving any edits.
333 @par
334 `SESSION_BREAK_LOCK`: Find an existing file or database, lock it, and open
335 it. If there is already a lock replace it with a new one for this session.
336
337 @par Errors
338 qof_session_begin() signals failure by queuing errors. After it completes use
339 qof_session_get_error() and test that the value is `ERROR_BACKEND_NONE` to
340 determine that the session began successfully.
341
342 @exception as begin() and load() are wrapped with raise_backend_errors_after_call()
343 this function can raise a GnuCashBackendException. If it does,
344 you don't need to cleanup and call end() and destroy(), that is handled
345 for you, and the exception is raised.
346 """
347 if instance is not None:
348 GnuCashCoreClass.__init__(self, instance=instance)
349 else:
350 if book is None:
351 book = Book()
352 GnuCashCoreClass.__init__(self, book)
353
354 if book_uri is not None:
355 try:
356 if mode is None:
357 mode = SessionOpenMode.SESSION_NORMAL_OPEN
358 self.begin(book_uri, mode)
359 is_new = mode in (SessionOpenMode.SESSION_NEW_STORE, SessionOpenMode.SESSION_NEW_OVERWRITE)
360 if not is_new:
361 self.load()
362 except GnuCashBackendException as backend_exception:
363 self.end()
364 self.destroy()
365 raise
366

Member Function Documentation

◆ __enter__()

python.gnucash_core.Session.__enter__ (   self)

Definition at line 367 of file gnucash_core.py.

367 def __enter__(self):
368 return self
369

◆ __exit__()

python.gnucash_core.Session.__exit__ (   self,
  exc_type,
  exc_value,
  traceback 
)

Definition at line 370 of file gnucash_core.py.

370 def __exit__(self, exc_type, exc_value, traceback):
371 # Roll back changes on exception by not calling save. Only works for XMl backend.
372 if not exc_type:
373 self.save()
374 self.end()
375 self.destroy()
376

◆ generate_errors()

python.gnucash_core.Session.generate_errors (   self)
A generator that yields any outstanding QofBackend errors

Definition at line 390 of file gnucash_core.py.

390 def generate_errors(self):
391 """A generator that yields any outstanding QofBackend errors
392 """
393 while self.get_error() is not ERR_BACKEND_NO_ERR:
394 error = self.pop_error()
395 yield error
396

◆ pop_all_errors()

python.gnucash_core.Session.pop_all_errors (   self)
Returns any accumulated qof backend errors as a tuple

Definition at line 397 of file gnucash_core.py.

397 def pop_all_errors(self):
398 """Returns any accumulated qof backend errors as a tuple
399 """
400 return tuple( self.generate_errors() )
401

◆ raise_backend_errors()

python.gnucash_core.Session.raise_backend_errors (   self,
  called_function = "qof_session function" 
)
Raises a GnuCashBackendException if there are outstanding
QOF_BACKEND errors.

set called_function to name the function that was last called

Definition at line 377 of file gnucash_core.py.

377 def raise_backend_errors(self, called_function="qof_session function"):
378 """Raises a GnuCashBackendException if there are outstanding
379 QOF_BACKEND errors.
380
381 set called_function to name the function that was last called
382 """
383 errors = self.pop_all_errors()
384 if errors != ():
385 raise GnuCashBackendException(
386 "call to %s resulted in the "
387 "following errors, %s" % (called_function, backend_error_dict[errors[0]]),
388 errors )
389

◆ raise_backend_errors_after_call()

python.gnucash_core.Session.raise_backend_errors_after_call (   function,
args,
**  kwargs 
)
static
A function decorator that results in a call to
raise_backend_errors after execution.

Definition at line 404 of file gnucash_core.py.

404 def raise_backend_errors_after_call(function, *args, **kwargs):
405 """A function decorator that results in a call to
406 raise_backend_errors after execution.
407 """
408 def new_function(self, *args, **kwargs):
409 return_value = function(self, *args, **kwargs)
410 self.raise_backend_errors(function.__name__)
411 return return_value
412 return new_function
413

The documentation for this class was generated from the following file: