budget/pfbudget/utils.py
Luís Murta 4d6c865bb5
Parsers configuration now on parsers.yaml
parsers.py redid with single default parser that takes configurating
parameters from a yaml file.
Additional parsing configuration can be achieved with the additional_parser
attribute on yaml and rewriting the func and parser method on child
classes of Parser.
func will be called after each transaction is created and the parser
should call the parent parser method or rewrite the entire parser
process.
The parse_data function is now called from the runnable and the parsing
process is now called from there. The parse command can take an optional
bank before is tries to extract it from the filename and multiple paths,
either files or directories.
The Transaction __init__ was fixed to take inputs from previously
initiated Transaction.
Also adds utils.py with helper functions.
2021-06-03 23:30:32 +01:00

55 lines
1.1 KiB
Python

from decimal import Decimal
from pathlib import Path
class WrongFilenameError(Exception):
pass
class BankNotAvailableError(Exception):
pass
class CreditCardNotAvailableError(Exception):
pass
def parse_decimal(s: str) -> Decimal:
try:
float(s)
return Decimal(s)
except ValueError:
pass
s = s.strip().replace(u"\xa0", "").replace(" ", "")
s = s.strip().replace("", "").replace("+", "")
if s.rfind(",") > s.rfind("."):
s = s.replace(".", "")
i = s.rfind(",")
li = list(s)
li[i] = "."
s = "".join(li)
return Decimal(s.replace(",", ""))
def find_credit_institution(fn, banks, creditcards):
name = Path(fn).stem.split("_")
bank, cc = None, None
for i in name:
try:
int(i)
except ValueError:
if not bank:
bank = i
else:
cc = i
if not bank:
raise WrongFilenameError
if bank not in banks:
raise BankNotAvailableError
if cc and cc not in creditcards:
raise CreditCardNotAvailableError
return bank, cc