update example scripts to SessionOpenMode

This commit is contained in:
c-holtermann 2020-06-12 12:24:05 +02:00
parent b073dbc5c3
commit ee77b713c2
11 changed files with 46 additions and 25 deletions

View File

@ -35,7 +35,7 @@ from math import log10
import csv import csv
# gnucash imports # gnucash imports
from gnucash import Session, GncNumeric, Split from gnucash import Session, GncNumeric, Split, SessionOpenMode
# Invoke this script like the following example # Invoke this script like the following example
# $ python3 account_analysis.py gnucash_file.gnucash \ # $ python3 account_analysis.py gnucash_file.gnucash \
@ -173,7 +173,7 @@ def main():
account_path = argv[8:] account_path = argv[8:]
gnucash_session = Session(gnucash_file, is_new=False) gnucash_session = Session(gnucash_file, SessionOpenMode.SESSION_NORMAL_OPEN)
root_account = gnucash_session.book.get_root_account() root_account = gnucash_session.book.get_root_account()
account_of_interest = account_from_path(root_account, account_path) account_of_interest = account_from_path(root_account, account_path)

View File

@ -39,6 +39,7 @@ try:
import str_methods import str_methods
import jinja2 import jinja2
from gncinvoicefkt import * from gncinvoicefkt import *
from gnucash import SessionOpenMode
except ImportError as import_error: except ImportError as import_error:
print("Problem importing modules.") print("Problem importing modules.")
print(import_error) print(import_error)
@ -137,7 +138,7 @@ def main(argv=None):
print("or file://filename") print("or file://filename")
print("or mysql://user:password@host/databasename") print("or mysql://user:password@host/databasename")
print() print()
print("-f force open = ignore lock") print("-f force open = ignore lock (read only)")
print("-l list all invoices") print("-l list all invoices")
print("-h or --help for this help") print("-h or --help for this help")
print("-I ID use invoice ID") print("-I ID use invoice ID")
@ -150,8 +151,15 @@ def main(argv=None):
# Try to open the given input # Try to open the given input
try: try:
print("Opening", input_url, ".") print(
session = gnucash.Session(input_url, ignore_lock=ignore_lock) "Opening", input_url, " (ignore-lock = read-only)." if ignore_lock else "."
)
session = gnucash.Session(
input_url,
SessionOpenMode.SESSION_READ_ONLY
if ignore_lock
else SessionOpenMode.SESSION_NORMAL_OPEN,
)
except Exception as exception: except Exception as exception:
print("Problem opening input.") print("Problem opening input.")
print(exception) print(exception)

View File

@ -64,6 +64,7 @@ try:
from gnucash.gnucash_business import Customer, Employee, Vendor, Job, \ from gnucash.gnucash_business import Customer, Employee, Vendor, Job, \
Address, Invoice, Entry, TaxTable, TaxTableEntry, GNC_AMT_TYPE_PERCENT, \ Address, Invoice, Entry, TaxTable, TaxTableEntry, GNC_AMT_TYPE_PERCENT, \
GNC_DISC_PRETAX GNC_DISC_PRETAX
from gnucash import SessionOpenMode
import locale import locale
except ImportError as import_error: except ImportError as import_error:
print("Problem importing modules.") print("Problem importing modules.")
@ -236,7 +237,12 @@ def main(argv=None):
# Try to open the given input # Try to open the given input
try: try:
session = gnucash.Session(input_url,ignore_lock=ignore_lock) session = gnucash.Session(
input_url,
SessionOpenMode.SESSION_READ_ONLY
if ignore_lock
else SessionOpenMode.SESSION_NORMAL_OPEN,
)
except Exception as exception: except Exception as exception:
print("Problem opening input.") print("Problem opening input.")
print(exception) print(exception)

View File

@ -28,7 +28,8 @@
# @author Mark Jenkins, ParIT Worker Co-operative <mark@parit.ca> # @author Mark Jenkins, ParIT Worker Co-operative <mark@parit.ca>
# @ingroup python_bindings_examples # @ingroup python_bindings_examples
from gnucash import Session, Account, Transaction, Split, GncNumeric from gnucash import (
Session, Account, Transaction, Split, GncNumeric, SessionOpenMode)
from gnucash.gnucash_core_c import \ from gnucash.gnucash_core_c import \
GNC_DENOM_AUTO, GNC_HOW_DENOM_EXACT, \ GNC_DENOM_AUTO, GNC_HOW_DENOM_EXACT, \
ACCT_TYPE_ASSET, ACCT_TYPE_BANK, ACCT_TYPE_CASH, ACCT_TYPE_CHECKING, \ ACCT_TYPE_ASSET, ACCT_TYPE_BANK, ACCT_TYPE_CASH, ACCT_TYPE_CHECKING, \
@ -299,8 +300,8 @@ def main():
#have everything in a try block to unable us to release our hold on stuff to the extent possible #have everything in a try block to unable us to release our hold on stuff to the extent possible
try: try:
original_book_session = Session(argv[1], is_new=False) original_book_session = Session(argv[1], SessionOpenMode.SESSION_NORMAL_OPEN)
new_book_session = Session(argv[2], is_new=True) new_book_session = Session(argv[2], SessionOpenMode.SESSION_NEW_STORE)
new_book = new_book_session.get_book() new_book = new_book_session.get_book()
new_book_root = new_book.get_root_account() new_book_root = new_book.get_root_account()

View File

@ -68,6 +68,8 @@ from gnucash import \
from gnucash import \ from gnucash import \
INVOICE_IS_PAID INVOICE_IS_PAID
from gnucash import SessionOpenMode
app = Flask(__name__) app = Flask(__name__)
app.debug = True app.debug = True
@ -1884,7 +1886,7 @@ for option, value in options:
#start gnucash session base on connection string argument #start gnucash session base on connection string argument
if is_new: if is_new:
session = gnucash.Session(arguments[0], is_new=True) session = gnucash.Session(arguments[0], SessionOpenMode.SESSION_NEW_STORE)
# seem to get errors if we use the session directly, so save it and # seem to get errors if we use the session directly, so save it and
#destroy it so it's no longer new #destroy it so it's no longer new
@ -1893,7 +1895,8 @@ if is_new:
session.end() session.end()
session.destroy() session.destroy()
session = gnucash.Session(arguments[0], ignore_lock=True) # unsure about SESSION_BREAK_LOCK - it used to be ignore_lock=True
session = gnucash.Session(arguments[0], SessionOpenMode.SESSION_BREAK_LOCK)
# register method to close gnucash connection gracefully # register method to close gnucash connection gracefully
atexit.register(shutdown) atexit.register(shutdown)

View File

@ -5,13 +5,13 @@
# @ingroup python_bindings_examples # @ingroup python_bindings_examples
import sys import sys
from gnucash import Session from gnucash import Session, SessionOpenMode
# We need to tell GnuCash the data format to create the new file as (xml://) # We need to tell GnuCash the data format to create the new file as (xml://)
uri = "xml:///tmp/simple_book.gnucash" uri = "xml:///tmp/simple_book.gnucash"
print("uri:", uri) print("uri:", uri)
with Session(uri, is_new=True) as ses: with Session(uri, SessionOpenMode.SESSION_NEW_STORE) as ses:
book = ses.get_book() book = ses.get_book()
#Call some methods that produce output to show that Book works #Call some methods that produce output to show that Book works

View File

@ -53,7 +53,7 @@ from os.path import abspath
from sys import argv, exit from sys import argv, exit
import datetime import datetime
from datetime import timedelta from datetime import timedelta
from gnucash import Session, Account, GncNumeric from gnucash import Session, Account, GncNumeric, SessionOpenMode
from gnucash.gnucash_business import Customer, Employee, Vendor, Job, \ from gnucash.gnucash_business import Customer, Employee, Vendor, Job, \
Address, Invoice, Entry, TaxTable, TaxTableEntry, GNC_AMT_TYPE_PERCENT, \ Address, Invoice, Entry, TaxTable, TaxTableEntry, GNC_AMT_TYPE_PERCENT, \
GNC_DISC_PRETAX GNC_DISC_PRETAX
@ -70,7 +70,7 @@ if len(argv) < 2:
try: try:
s = Session(argv[1], is_new=True) s = Session(argv[1], SessionOpenMode.SESSION_NEW_STORE)
book = s.book book = s.book
root = book.get_root_account() root = book.get_root_account()

View File

@ -46,7 +46,7 @@
# @author Mark Jenkins, ParIT Worker Co-operative <mark@parit.ca> # @author Mark Jenkins, ParIT Worker Co-operative <mark@parit.ca>
# @ingroup python_bindings_examples # @ingroup python_bindings_examples
from gnucash import Session, GUID, GncNumeric from gnucash import Session, GUID, GncNumeric, SessionOpenMode
from gnucash.gnucash_business import Customer, Invoice, Entry from gnucash.gnucash_business import Customer, Invoice, Entry
from gnucash.gnucash_core_c import string_to_guid from gnucash.gnucash_core_c import string_to_guid
from os.path import abspath from os.path import abspath
@ -86,7 +86,7 @@ def gnc_numeric_from_decimal(decimal_value):
return GncNumeric(numerator, denominator) return GncNumeric(numerator, denominator)
s = Session(argv[1], is_new=False) s = Session(argv[1], SessionOpenMode.SESSION_NORMAL_OPEN)
book = s.book book = s.book
root = book.get_root_account() root = book.get_root_account()

View File

@ -3,9 +3,11 @@
# @brief Example Script simple session # @brief Example Script simple session
# @ingroup python_bindings_examples # @ingroup python_bindings_examples
from gnucash import \ from gnucash import (
Session, GnuCashBackendException, \ Session, GnuCashBackendException,
SessionOpenMode,
ERR_BACKEND_LOCKED, ERR_FILEIO_FILE_NOT_FOUND ERR_BACKEND_LOCKED, ERR_FILEIO_FILE_NOT_FOUND
)
FILE_1 = "/tmp/not_there.xac" FILE_1 = "/tmp/not_there.xac"
FILE_2 = "/tmp/example_file.xac" FILE_2 = "/tmp/example_file.xac"
@ -19,7 +21,7 @@ except GnuCashBackendException as backend_exception:
# create a new file, this requires a file type specification # create a new file, this requires a file type specification
with Session("xml://%s" % FILE_2, is_new=True) as session: with Session("xml://%s" % FILE_2, SessionOpenMode.SESSION_NEW_STORE) as session:
book = session.book book = session.book
root = book.get_root_account() root = book.get_root_account()

View File

@ -3,11 +3,11 @@
# @brief Example Script simple sqlite create # @brief Example Script simple sqlite create
# @ingroup python_bindings_examples # @ingroup python_bindings_examples
from gnucash import Session, Account from gnucash import Session, Account, SessionOpenMode
from os.path import abspath from os.path import abspath
from gnucash.gnucash_core_c import ACCT_TYPE_ASSET from gnucash.gnucash_core_c import ACCT_TYPE_ASSET
s = Session('sqlite3://%s' % abspath('test.blob'), is_new=True) s = Session('sqlite3://%s' % abspath('test.blob'), SessionOpenMode.SESSION_NEW_STORE)
# this seems to make a difference in more complex cases # this seems to make a difference in more complex cases
s.save() s.save()

View File

@ -3,11 +3,12 @@
# @brief Creates a basic set of accounts and a couple of transactions # @brief Creates a basic set of accounts and a couple of transactions
# @ingroup python_bindings_examples # @ingroup python_bindings_examples
from gnucash import Session, Account, Transaction, Split, GncNumeric from gnucash import (
Session, Account, Transaction, Split, GncNumeric, SessionOpenMode)
FILE_1 = "/tmp/example.gnucash" FILE_1 = "/tmp/example.gnucash"
with Session("xml://%s" % FILE_1, is_new=True) as session: with Session("xml://%s" % FILE_1, SessionOpenMode.SESSION_NEW_STORE) as session:
book = session.book book = session.book
root_acct = Account(book) root_acct = Account(book)
@ -80,4 +81,4 @@ with Session("xml://%s" % FILE_1, is_new=True) as session:
trans1.CommitEdit() trans1.CommitEdit()
trans2.CommitEdit() trans2.CommitEdit()