Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow parsing full files #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 49 additions & 2 deletions src/ofxstatement/plugins/mbankcz.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import csv
import re
from datetime import datetime
from typing import Iterable, Optional, List
from decimal import Decimal

from ofxstatement import statement
from ofxstatement.statement import StatementLine
Expand All @@ -18,9 +20,46 @@ def __init__(self, *args, **kwargs) -> None:

def split_records(self) -> Iterable[str]:
"""
Return iterable object consisting of a line per transaction
Parse metadata scattered around the actual transaction lines and return
those for further processing.
"""
return csv.reader(self.fin, delimiter=";", quotechar='"')
reading_records = False
last_meta_header = None
records = []
for line in csv.reader(self.fin, delimiter=";", quotechar='"'):
if len(line) == 0:
pass
elif len(line) == 9 and line[6] == "#Konečný zůstatek:":
self.statement.end_balance = \
Decimal(line[7]
.replace(self.statement.currency, "")
.replace(" ", "")
.replace(",", "."))
reading_records = False
elif reading_records:
records.append(line)
elif last_meta_header is not None:
if last_meta_header == "#Měna účtu:":
self.statement.currency = line[0]
elif last_meta_header == "#Číslo účtu:":
self.statement.bank_id = line[0].split("/")[1]
self.statement.account_id = line[0].split("/")[0]
elif last_meta_header == "#Za období":
self.statement.start_date = datetime.strptime(
line[0], '%d.%m.%Y')
self.statement.end_date = datetime.strptime(
line[1], '%d.%m.%Y')
last_meta_header = None
elif len(line) == 2:
last_meta_header = line[0]
elif len(line) == 9 and line[6] == "#Počáteční zůstatek:":
self.statement.start_balance = \
Decimal(line[7]
.replace(self.statement.currency, "")
.replace(" ", "")
.replace(",", "."))
reading_records = True
return records

def parse_record(self, line: List[str]) -> Optional[StatementLine]:
"""Parse given transaction line and return StatementLine object"""
Expand Down Expand Up @@ -62,6 +101,14 @@ def parse_record(self, line: List[str]) -> Optional[StatementLine]:
statement_line.date_user, self.date_format
)

date_user_match = re.match(
"^(.*) DATUM PROVEDENÍ TRANSAKCE: (\\d{4}-\\d{2}-\\d{2})$",
line[columns["#Zpráva pro příjemce"]])
if date_user_match:
line[columns["#Zpráva pro příjemce"]] = date_user_match.group(1)
statement_line.date_user = datetime.strptime(
date_user_match.group(2), "%Y-%m-%d")

statement_line.id = statement.generate_transaction_id(statement_line)

# If payee is empty or transactions is percentual saving set payee to -
Expand Down