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