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
# gnucash imports
from gnucash import Session, GncNumeric, Split
from gnucash import Session, GncNumeric, Split, SessionOpenMode
# Invoke this script like the following example
# $ python3 account_analysis.py gnucash_file.gnucash \
@ -173,7 +173,7 @@ def main():
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()
account_of_interest = account_from_path(root_account, account_path)

View File

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

View File

@ -64,6 +64,7 @@ try:
from gnucash.gnucash_business import Customer, Employee, Vendor, Job, \
Address, Invoice, Entry, TaxTable, TaxTableEntry, GNC_AMT_TYPE_PERCENT, \
GNC_DISC_PRETAX
from gnucash import SessionOpenMode
import locale
except ImportError as import_error:
print("Problem importing modules.")
@ -236,7 +237,12 @@ def main(argv=None):
# Try to open the given input
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:
print("Problem opening input.")
print(exception)

View File

@ -28,7 +28,8 @@
# @author Mark Jenkins, ParIT Worker Co-operative <mark@parit.ca>
# @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 \
GNC_DENOM_AUTO, GNC_HOW_DENOM_EXACT, \
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
try:
original_book_session = Session(argv[1], is_new=False)
new_book_session = Session(argv[2], is_new=True)
original_book_session = Session(argv[1], SessionOpenMode.SESSION_NORMAL_OPEN)
new_book_session = Session(argv[2], SessionOpenMode.SESSION_NEW_STORE)
new_book = new_book_session.get_book()
new_book_root = new_book.get_root_account()

View File

@ -68,6 +68,8 @@ from gnucash import \
from gnucash import \
INVOICE_IS_PAID
from gnucash import SessionOpenMode
app = Flask(__name__)
app.debug = True
@ -1884,7 +1886,7 @@ for option, value in options:
#start gnucash session base on connection string argument
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
#destroy it so it's no longer new
@ -1893,7 +1895,8 @@ if is_new:
session.end()
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
atexit.register(shutdown)

View File

@ -5,13 +5,13 @@
# @ingroup python_bindings_examples
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://)
uri = "xml:///tmp/simple_book.gnucash"
print("uri:", uri)
with Session(uri, is_new=True) as ses:
with Session(uri, SessionOpenMode.SESSION_NEW_STORE) as ses:
book = ses.get_book()
#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
import datetime
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, \
Address, Invoice, Entry, TaxTable, TaxTableEntry, GNC_AMT_TYPE_PERCENT, \
GNC_DISC_PRETAX
@ -70,7 +70,7 @@ if len(argv) < 2:
try:
s = Session(argv[1], is_new=True)
s = Session(argv[1], SessionOpenMode.SESSION_NEW_STORE)
book = s.book
root = book.get_root_account()

View File

@ -46,7 +46,7 @@
# @author Mark Jenkins, ParIT Worker Co-operative <mark@parit.ca>
# @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_core_c import string_to_guid
from os.path import abspath
@ -86,7 +86,7 @@ def gnc_numeric_from_decimal(decimal_value):
return GncNumeric(numerator, denominator)
s = Session(argv[1], is_new=False)
s = Session(argv[1], SessionOpenMode.SESSION_NORMAL_OPEN)
book = s.book
root = book.get_root_account()

View File

@ -3,9 +3,11 @@
# @brief Example Script simple session
# @ingroup python_bindings_examples
from gnucash import \
Session, GnuCashBackendException, \
from gnucash import (
Session, GnuCashBackendException,
SessionOpenMode,
ERR_BACKEND_LOCKED, ERR_FILEIO_FILE_NOT_FOUND
)
FILE_1 = "/tmp/not_there.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
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
root = book.get_root_account()

View File

@ -3,11 +3,11 @@
# @brief Example Script simple sqlite create
# @ingroup python_bindings_examples
from gnucash import Session, Account
from gnucash import Session, Account, SessionOpenMode
from os.path import abspath
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
s.save()

View File

@ -3,11 +3,12 @@
# @brief Creates a basic set of accounts and a couple of transactions
# @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"
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
root_acct = Account(book)
@ -80,4 +81,4 @@ with Session("xml://%s" % FILE_1, is_new=True) as session:
trans1.CommitEdit()
trans2.CommitEdit()
trans2.CommitEdit()