generated from felixm/defaultpy
Add source info and sort transactions
This commit is contained in:
@@ -3,7 +3,6 @@ import datetime
|
||||
import logging
|
||||
import re
|
||||
import sys
|
||||
from typing import Any, Dict, List
|
||||
|
||||
import toldg.models
|
||||
import toldg.predict
|
||||
@@ -19,7 +18,7 @@ def process_ldg_files(config: Config):
|
||||
f_out.write(f_in.read())
|
||||
|
||||
|
||||
def get_csv_config(csv_file: str, csv_configs: List[CsvConfig]) -> CsvConfig:
|
||||
def get_csv_config(csv_file: str, csv_configs: list[CsvConfig]) -> CsvConfig:
|
||||
cs = [c for c in csv_configs if re.match(c.file_match_regex, csv_file)]
|
||||
if not cs:
|
||||
logging.critical(f"No CSV config for {csv_file}.")
|
||||
@@ -30,7 +29,7 @@ def get_csv_config(csv_file: str, csv_configs: List[CsvConfig]) -> CsvConfig:
|
||||
return cs[0]
|
||||
|
||||
|
||||
def get_transactions(csv_file: str, config: CsvConfig) -> List[Transaction]:
|
||||
def get_transactions(csv_file: str, config: CsvConfig) -> list[Transaction]:
|
||||
def date_to_date(date: str) -> str:
|
||||
d = datetime.datetime.strptime(date, config.input_date_format)
|
||||
return d.strftime(config.output_date_format)
|
||||
@@ -38,7 +37,7 @@ def get_transactions(csv_file: str, config: CsvConfig) -> List[Transaction]:
|
||||
def flip_sign(amount: str) -> str:
|
||||
return amount[1:] if amount.startswith("-") else "-" + amount
|
||||
|
||||
def row_to_transaction(row, fields):
|
||||
def row_to_transaction(idx, row, fields):
|
||||
"""The user can configure the mapping of CSV fields to the three
|
||||
required fields date, amount and description via the CsvConfig."""
|
||||
t = {field: row[index] for index, field in fields}
|
||||
@@ -52,7 +51,8 @@ def get_transactions(csv_file: str, config: CsvConfig) -> List[Transaction]:
|
||||
account2=toldg.models.UNKNOWN_CATEGORY,
|
||||
description=t["description"],
|
||||
csv_file=csv_file,
|
||||
row=csv_file + ", " + ", ".join(row),
|
||||
row=", ".join(row),
|
||||
index=idx,
|
||||
)
|
||||
|
||||
fields = [(i, f) for i, f in enumerate(config.fields) if f]
|
||||
@@ -60,15 +60,17 @@ def get_transactions(csv_file: str, config: CsvConfig) -> List[Transaction]:
|
||||
reader = csv.reader(f, delimiter=config.delimiter, quotechar=config.quotechar)
|
||||
for _ in range(config.skip):
|
||||
next(reader)
|
||||
transactions = [row_to_transaction(row, fields) for row in reader if row]
|
||||
rows = [row for row in reader if row]
|
||||
transactions = [row_to_transaction(i, row, fields)
|
||||
for i, row in enumerate(reversed(rows))]
|
||||
return transactions
|
||||
|
||||
|
||||
def apply_mappings(transactions: List[Transaction], mappings: Dict[str, Mapping]):
|
||||
def apply_mappings(transactions: list[Transaction], mappings: dict[str, Mapping]):
|
||||
"""Apply mappings to transactions."""
|
||||
for t in transactions:
|
||||
if t.row in mappings:
|
||||
mapping = mappings[t.row]
|
||||
if t.key() in mappings:
|
||||
mapping = mappings[t.key()]
|
||||
assert isinstance(mapping, Mapping)
|
||||
assert (
|
||||
mapping.count > 0
|
||||
@@ -82,7 +84,7 @@ def apply_mappings(transactions: List[Transaction], mappings: Dict[str, Mapping]
|
||||
assert mapping.count == 0, f"{mapping} was not used as often as expected!"
|
||||
|
||||
|
||||
def process_csv_files(config: Config) -> List[Transaction]:
|
||||
def process_csv_files(config: Config) -> list[Transaction]:
|
||||
csv_files = toldg.utils.get_csv_files(config.input_directory)
|
||||
transactions = []
|
||||
for csv_file in csv_files:
|
||||
|
||||
Reference in New Issue
Block a user