From 79f67069ae80dc56aab1c9bf2cdc56476741e5f0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Henrik=20tom=20W=C3=B6rden?= <h.tomwoerden@indiscale.com>
Date: Tue, 18 Feb 2025 14:24:38 +0100
Subject: [PATCH] MAINT: add config file

---
 loanpy/pyproject.toml              | 16 ++++++++++++++++
 loanpy/src/loan/conf.py            | 20 +++++++++++---------
 loanpy/src/loan/default_config.ini | 25 +++++++++++++++++++++++++
 loanpy/src/loan/request_loan.py    |  4 ++--
 4 files changed, 54 insertions(+), 11 deletions(-)
 create mode 100644 loanpy/src/loan/default_config.ini

diff --git a/loanpy/pyproject.toml b/loanpy/pyproject.toml
index 0e4f069..9534aed 100644
--- a/loanpy/pyproject.toml
+++ b/loanpy/pyproject.toml
@@ -24,10 +24,26 @@ classifiers = [
 requires-python = ">= 3.8"
 dependencies = [
    "caosadvancedtools",
+   "importlib_resources",
    "py3-validate-email",
    "linkahead@git+https://gitlab.indiscale.com/caosdb/src/caosdb-pylib.git@dev"
 ]
 
+
+[project.optional-dependencies]
+test = [
+    "pytest",
+    "pytest-env",
+    "pytest-cov",
+    "coverage>=4.4.2",
+]
+
+[tool.setuptools.packages.find]
+where = ["src"]
+
+[tool.setuptools.package-data]
+"loan" = ["*.ini"]
+
 [project.urls]
 Homepage = "https://getlinkahead.com"
 Documentation = "https://docs.indiscale.com"
diff --git a/loanpy/src/loan/conf.py b/loanpy/src/loan/conf.py
index 68c36d8..390830b 100644
--- a/loanpy/src/loan/conf.py
+++ b/loanpy/src/loan/conf.py
@@ -1,25 +1,27 @@
 import linkahead as db
 import os
 import configparser
-import importlib.resources as pkg_resources
+from importlib_resources import files, as_file
 
-default_config_path = pkg_resources.path(__package__, 'default_config.ini')
-user_config_path = os.path.expanduser('~/.linkahead_loan')
-config = configparser.ConfigParser()
-config.read([default_config_path, user_config_path])
-rts = config['RecordTypes']
-ps = config['Properties']
-# RecordTypes
+with as_file(files('loan').joinpath('default_config.ini')) as default_config_path:
+    user_config_path = os.path.expanduser('~/.linkahead_loan')
+    config = configparser.ConfigParser()
+    config.read([default_config_path, user_config_path])
+    global rts, ps
+    rts = config['RecordTypes']
+    ps = config['Properties']
 
+# RecordTypes
 BOX = db.RecordType(name=rts['BOX'])
 PERSON = db.RecordType(name=rts['PERSON'])
 LOAN = db.RecordType(name=rts['LOAN'])
+LOCATION = db.RecordType(name=rts['LOCATION'])
 
 # Properties
 FIRST_NAME = db.Property(name=ps['FIRST_NAME'], datatype=db.TEXT)
 LAST_NAME = db.Property(name=ps['LAST_NAME'], datatype=db.TEXT)
 EMAIL = db.Property(name=ps['EMAIL'], datatype=db.TEXT)
-LOCATION = db.RecordType(name=rts['LOCATION'])
+
 DESTINATION = db.Property(name=ps['DESTINATION'], datatype=rts['LOCATION'])
 RETURNLOCATION = db.Property(name=ps['RETURNLOCATION'], datatype=rts['LOCATION'])
 COMMENT = db.Property(name=ps['COMMENT'], datatype=db.TEXT)
diff --git a/loanpy/src/loan/default_config.ini b/loanpy/src/loan/default_config.ini
new file mode 100644
index 0000000..7b49b0f
--- /dev/null
+++ b/loanpy/src/loan/default_config.ini
@@ -0,0 +1,25 @@
+[RecordTypes]
+BOX = Box
+PERSON = Person
+LOAN = Loan
+LOCATION = Location
+
+
+[Properties]
+FIRST_NAME = firstName
+LAST_NAME = lastName
+EMAIL = email
+DESTINATION = LoanLocation
+RETURNLOCATION = ReturnLocation
+COMMENT = comment
+EXHAUST_CONTENTS = exhaustContents
+BORROWER = Borrower
+CONTENT = Content
+LOAN_REQUESTED = loanRequested
+EXPECTED_RETURN = expectedReturn
+LOAN_ACCEPTED = loanAccepted
+LENT = lent
+RETURN_REQUESTED = returnRequested
+RETURN_ACCEPTED = returnAccepted
+RETURNED = returned
+BOX_NUMBER = Number
diff --git a/loanpy/src/loan/request_loan.py b/loanpy/src/loan/request_loan.py
index 16eb783..e582089 100755
--- a/loanpy/src/loan/request_loan.py
+++ b/loanpy/src/loan/request_loan.py
@@ -83,8 +83,8 @@ def _check_data(data):
             bad_references.append(item)
     if bad_references:
         raise ValueError(
-                f"The  following value(s) of {F_BOX} has/have the wrong type. It should be "
-                f"integers or string representations thereof.\n{bad_references}")
+            f"The  following value(s) of {F_BOX} has/have the wrong type. It should be "
+            f"integers or string representations thereof.\n{bad_references}")
     assert_date_in_future(
         data[F_EXPECTED_RETURN_DATE],
         ("The expected return date needs to be in the future."
-- 
GitLab