budget/pfbudget/utils/converters.py
Luís Murta 0a42db8995
Persists banks information and requisition ID
This patch saves the bank information in the DB, in a new table. It also
adds two new CLI commands, register/unregister, so enter the banking
information. (This should later be done internally).

It also adds new types alias for the DB transaction type and new
converters.
Input `transactions` method renamed to `parse`.

Issue #18
2022-10-06 22:22:56 +01:00

45 lines
1.1 KiB
Python

from functools import singledispatch
from pfbudget.core.transactions import Transaction, TransactionError, Transactions
from pfbudget.db.schema import DbTransaction, DbTransactions
@singledispatch
def convert(t):
pass
@convert.register
def _(t: Transaction) -> DbTransaction:
return (t.date, t.description, t.bank, t.value, t.category)
def convert_dbtransaction(db) -> Transaction:
try:
return Transaction(db)
except TransactionError:
print(f"{db} is in the wrong format")
convert.register(type(DbTransaction), convert_dbtransaction)
def convert_transactions(ts: Transactions) -> DbTransactions:
try:
return [convert(t) for t in ts]
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)