piecash.core.session module

class piecash.core.session.Version(table_name, table_version)[source]

Bases: sqlalchemy.ext.declarative.api.DeclarativeBase

The declarative class for the ‘versions’ table.

table_version

The version for the table

piecash.core.session.build_uri(sqlite_file=None, uri_conn=None, db_type=None, db_user=None, db_password=None, db_name=None, db_host=None, db_port=None, check_same_thread=True)[source]

Create the connection string in function of some choices.

Parameters
  • sqlite_file (str) – a path to an sqlite3 file (only used if uri_conn is None)

  • uri_conn (str) – a sqlalchemy connection string

  • db_type (str) – type of database in [“postgres”,”mysql”]

  • db_user (str) – username of database

  • db_password (str) – password for the use of database

  • db_name (str) – name of database

  • db_host (str) – host of database

  • db_port (int) – port of database

  • check_same_thread (bool) – sqlite flag that restricts connection use to the thread that created (see False for use in ipython/flask/… but read first https://docs.python.org/3/library/sqlite3.html)

Returns

the connection string

Return type

str

piecash.core.session.create_book(sqlite_file=None, uri_conn=None, currency='EUR', overwrite=False, keep_foreign_keys=False, db_type=None, db_user=None, db_password=None, db_name=None, db_host=None, db_port=None, check_same_thread=True, pg_template='template0', **kwargs)[source]

Create a new empty GnuCash book. If both sqlite_file and uri_conn are None, then an “in memory” sqlite book is created.

Parameters
  • sqlite_file (str) – a path to an sqlite3 file (only used if uri_conn is None)

  • uri_conn (str) – a sqlalchemy connection string

  • currency (str) – the ISO symbol of the default currency of the book

  • overwrite (bool) – True if book should be deleted and recreated if it exists already

  • keep_foreign_keys (bool) – True if the foreign keys should be kept (may not work at all with GnuCash)

  • db_type (str) – type of database in [“postgres”,”mysql”]

  • db_user (str) – username of database

  • db_password (str) – password for the use of database

  • db_name (str) – name of database

  • db_host (str) – host of database

  • db_port (int) – port of database

  • check_same_thread (bool) – sqlite flag that restricts connection use to the thread that created (see False for use in ipython/flask/… but read first https://docs.python.org/3/library/sqlite3.html)

  • pg_template (str) – the postgres template to use when creating the database. One of template1 or template0 (default template0). Irrelevant for other databases than postgres.

Returns

the document as a gnucash session

Return type

GncSession

Raises

GnucashException – if document already exists and overwrite is False

piecash.core.session.open_book(sqlite_file=None, uri_conn=None, readonly=True, open_if_lock=False, do_backup=True, db_type=None, db_user=None, db_password=None, db_name=None, db_host=None, db_port=None, check_same_thread=True, check_exists=True, **kwargs)[source]

Open an existing GnuCash book

Parameters
  • sqlite_file (str) – a path to an sqlite3 file (only used if uri_conn is None)

  • uri_conn (str) – a sqlalchemy connection string

  • readonly (bool) – open the file as readonly (useful to play with and avoid any unwanted save)

  • open_if_lock (bool) – open the file even if it is locked by another user (using open_if_lock=True with readonly=False is not recommended)

  • do_backup (bool) – do a backup if the file written in RW (i.e. readonly=False) (this only works with the sqlite backend and copy the file with .{:%Y%m%d%H%M%S}.gnucash appended to it)

  • db_type (str) – type of database in [“postgres”,”mysql”]

  • db_user (str) – username of database

  • db_password (str) – password for the use of database

  • db_name (str) – name of database

  • db_host (str) – host of database

  • db_port (str) – port of database

  • check_same_thread (bool) – sqlite flag that restricts connection use to the thread that created (see False for use in ipython/flask/… but read first https://docs.python.org/3/library/sqlite3.html)

  • check_exists (bool) – check if the database exists before connecting

Returns

the document as a gnucash session

Return type

GncSession

Raises
piecash.core.session.adapt_session(session, book, readonly)[source]

Change the SA session object to add some features.

Parameters
  • session – the SA session object that will be modified in place

  • book – the gnucash singleton book linked to the SA session

  • readonly – True if the session should not allow commits.

Returns