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.
45 lines
1.1 KiB
Python
45 lines
1.1 KiB
Python
from functools import singledispatch
|
|
|
|
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
|
|
|
|
|
|
@convert.register
|
|
def _(t: Transaction) -> DbTransaction:
|
|
return (t.date, t.description, t.bank, t.value, t.category)
|
|
|
|
|
|
@convert.register
|
|
def _(db: DbTransaction) -> Transaction:
|
|
try:
|
|
return Transaction(db)
|
|
except TransactionError:
|
|
print(f"{db} is in the wrong format")
|
|
|
|
|
|
@convert.register
|
|
def _(db: DbBank, key: str = "") -> Bank:
|
|
bank = Bank(db.name, db.bic, db.requisition_id, db.invert, key=key)
|
|
return bank
|
|
|
|
|
|
@convert.register
|
|
def _(json: dict, bank: str, invert: bool) -> Transaction:
|
|
i = -1 if invert else 1
|
|
try:
|
|
return Transaction(
|
|
json["bookingDate"],
|
|
json["remittanceInformationUnstructured"],
|
|
bank,
|
|
i * parse_decimal(json["transactionAmount"]["amount"]),
|
|
)
|
|
except TransactionError:
|
|
print(f"{json} is in the wrong format")
|