comdirect reader tweak: rollback
This commit is contained in:
+30
-32
@@ -1,9 +1,7 @@
|
|||||||
import csv
|
import csv
|
||||||
from dataclasses import dataclass
|
|
||||||
import re
|
import re
|
||||||
from enum import Enum, auto
|
from enum import Enum, auto
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Callable
|
|
||||||
|
|
||||||
from src.models import B4wExport
|
from src.models import B4wExport
|
||||||
|
|
||||||
@@ -20,44 +18,44 @@ def reader_b4w(path: Path):
|
|||||||
|
|
||||||
def reader_comdirect(path: Path):
|
def reader_comdirect(path: Path):
|
||||||
|
|
||||||
@dataclass
|
PATTERN_ACCOUNT_NAME = r'^"Umsätze ([^"]+)";"Zeitraum: .*$'
|
||||||
class Pattern:
|
PATTERN_HEADER = r'"([^"]+)"'
|
||||||
pattern: str
|
PATTERN_DATA = r'"([^"]+)"'
|
||||||
|
|
||||||
@classmethod
|
class EXPECT_LINE(Enum):
|
||||||
def extract_pattern(cls, txt):
|
ACCOUNT_NAME = auto()
|
||||||
return re.findall(cls.pattern, txt)
|
BLANK = auto()
|
||||||
|
HEADER = auto()
|
||||||
class EXPECT_LINE(Enum, Pattern):
|
DATA = auto()
|
||||||
ACCOUNT_NAME = Pattern(r'^"Umsätze ([^"]+)";"Zeitraum: .*$')
|
|
||||||
BLANK = Pattern(r"\n")
|
|
||||||
HEADER = Pattern(r'"([^"]+)"')
|
|
||||||
DATA = Pattern(r'"([^"]+)"')
|
|
||||||
|
|
||||||
with path.open(encoding="latin1") as fid:
|
with path.open(encoding="latin1") as fid:
|
||||||
lines = fid.readlines()
|
lines = fid.readlines()
|
||||||
|
|
||||||
expect_next = EXPECT_LINE.ACCOUNT_NAME
|
expect_next = EXPECT_LINE.ACCOUNT_NAME
|
||||||
account_name = ""
|
account_name = None
|
||||||
header = ""
|
|
||||||
result = {}
|
result = {}
|
||||||
for line in lines:
|
for line in lines:
|
||||||
parsed = expect_next.extract_pattern(line)
|
if expect_next == EXPECT_LINE.ACCOUNT_NAME:
|
||||||
if expect_next == EXPECT_LINE.ACCOUNT_NAME and parsed:
|
account_name = re.findall(PATTERN_ACCOUNT_NAME, line)
|
||||||
account_name = parsed[0]
|
if account_name:
|
||||||
result[account_name] = {}
|
account_name = account_name[0]
|
||||||
expect_next = EXPECT_LINE.BLANK
|
result[account_name] = {}
|
||||||
elif expect_next == EXPECT_LINE.BLANK and parsed:
|
expect_next = EXPECT_LINE.BLANK
|
||||||
expect_next = EXPECT_LINE.HEADER
|
elif expect_next == EXPECT_LINE.BLANK:
|
||||||
elif expect_next == EXPECT_LINE.HEADER and parsed:
|
if line == "\n":
|
||||||
header = parsed
|
expect_next = EXPECT_LINE.HEADER
|
||||||
result[account_name]["data"] = []
|
elif expect_next == EXPECT_LINE.HEADER:
|
||||||
expect_next = EXPECT_LINE.DATA
|
header = re.findall(PATTERN_HEADER, line)
|
||||||
elif expect_next == EXPECT_LINE.DATA and parsed:
|
if header:
|
||||||
result[account_name]["data"].append(
|
result[account_name]["data"] = []
|
||||||
{column: value for column, value in zip(header, parsed)}
|
expect_next = EXPECT_LINE.DATA
|
||||||
)
|
elif expect_next == EXPECT_LINE.DATA:
|
||||||
expect_next = EXPECT_LINE.ACCOUNT_NAME
|
data = re.findall(PATTERN_DATA, line)
|
||||||
|
if data:
|
||||||
|
result[account_name]["data"].append(
|
||||||
|
{column: value for column, value in zip(header, data)}
|
||||||
|
)
|
||||||
|
expect_next = EXPECT_LINE.ACCOUNT_NAME
|
||||||
else:
|
else:
|
||||||
expect_next = EXPECT_LINE.ACCOUNT_NAME
|
expect_next = EXPECT_LINE.ACCOUNT_NAME
|
||||||
return result
|
return result
|
||||||
|
|||||||
Reference in New Issue
Block a user