From cfcc182f355c8598a7bc50fc03edfb860715a202 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Murta?= Date: Sun, 9 Oct 2022 17:24:42 +0100 Subject: [PATCH] Fix converters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Python's single dispatch can“t use type aliases and lists of classes. All converters now work on a class, and lists need to be generated inplace and call the converter for each item. Adds the bank class both to the DB schema and the common types. --- pfbudget/common/types.py | 26 +++++++++++++++++++++-- pfbudget/db/schema.py | 36 +++++++++++++++++++++++++++----- pfbudget/utils/converters.py | 40 ++++++++++++++++++------------------ 3 files changed, 75 insertions(+), 27 deletions(-) diff --git a/pfbudget/common/types.py b/pfbudget/common/types.py index 19c3531..ac10d66 100644 --- a/pfbudget/common/types.py +++ b/pfbudget/common/types.py @@ -1,7 +1,7 @@ +from dataclasses import dataclass from datetime import date from decimal import Decimal, InvalidOperation - -COMMENT_TOKEN = "#" +from enum import Enum, auto class TransactionError(Exception): @@ -104,3 +104,25 @@ class Transaction: Transactions = list[Transaction] + + +class PrimaryKey(Enum): + ID = auto() + NAME = auto() + BIC = auto() + + +@dataclass +class Bank: + name: str + bic: str + requisition_id: str + invert: bool + key: PrimaryKey = PrimaryKey.ID + + +Banks = list[Bank] + + +class NoBankSelected(Exception): + pass diff --git a/pfbudget/db/schema.py b/pfbudget/db/schema.py index 3c97056..acac301 100644 --- a/pfbudget/db/schema.py +++ b/pfbudget/db/schema.py @@ -1,3 +1,4 @@ +from dataclasses import dataclass from decimal import Decimal CREATE_TRANSACTIONS_TABLE = """ @@ -12,7 +13,18 @@ CREATE TABLE IF NOT EXISTS "transactions" ( ); """ -DbTransaction = tuple[str, str | None, str, Decimal, str | None, str | None, str | None] + +@dataclass +class DbTransaction: + date: str + description: str + bank: str + value: Decimal + category: str + original: str + comments: str + + DbTransactions = list[DbTransaction] CREATE_BACKUPS_TABLE = """ @@ -25,13 +37,27 @@ CREATE TABLE IF NOT EXISTS backups ( CREATE_BANKS_TABLE = """ CREATE TABLE IF NOT EXISTS banks ( name TEXT NOT NULL PRIMARY KEY, - requisition TEXT, - invert INTEGER, - description TEXT + bic TEXT, + nordigen_id TEXT, + nordigen_name TEXT, + requisition_id TEXT, + invert INTEGER ) """ -Bank = tuple[str, str, bool] + +@dataclass +class DbBank: + name: str + bic: str + nordigen_id: str + nordigen_name: str + requisition_id: str + invert: bool + + +DbBanks = list[DbBank] + ADD_TRANSACTION = """ INSERT INTO transactions (date, description, bank, value, category) values (?,?,?,?,?) diff --git a/pfbudget/utils/converters.py b/pfbudget/utils/converters.py index 47b4159..6c78c4f 100644 --- a/pfbudget/utils/converters.py +++ b/pfbudget/utils/converters.py @@ -1,11 +1,13 @@ from functools import singledispatch -from pfbudget.common.types import Transaction, Transactions, TransactionError -from pfbudget.db.schema import DbTransaction, DbTransactions +from pfbudget.common.types import Bank, Transaction, TransactionError +from pfbudget.db.schema import DbBank, DbTransaction +from .utils import parse_decimal @singledispatch def convert(t): + print("No converter as been found") pass @@ -14,31 +16,29 @@ def _(t: Transaction) -> DbTransaction: return (t.date, t.description, t.bank, t.value, t.category) -def convert_dbtransaction(db) -> Transaction: +@convert.register +def _(db: DbTransaction) -> Transaction: try: return Transaction(db) except TransactionError: print(f"{db} is in the wrong format") -convert.register(type(DbTransaction), convert_dbtransaction) +@convert.register +def _(db: DbBank, key: str = "") -> Bank: + bank = Bank(db.name, db.bic, db.requisition_id, db.invert, key=key) + return bank -def convert_transactions(ts: Transactions) -> DbTransactions: +@convert.register +def _(json: dict, bank: str, invert: bool) -> Transaction: + i = -1 if invert else 1 try: - return [convert(t) for t in ts] + return Transaction( + json["bookingDate"], + json["remittanceInformationUnstructured"], + bank, + i * parse_decimal(json["transactionAmount"]["amount"]), + ) except TransactionError: - print(f"{ts} is in the wrong format") - - -convert.register(type(Transactions), convert_transactions) - - -def convert_dbtransactions(ts: DbTransactions) -> Transactions: - try: - return [convert(t) for t in ts] - except TransactionError: - print(f"{ts} is in the wrong format") - - -convert.register(type(DbTransactions), convert_dbtransactions) + print(f"{json} is in the wrong format")