generated from felixm/defaultpy
Implement new mapping format
This commit is contained in:
@@ -3,13 +3,13 @@ import datetime
|
||||
import logging
|
||||
import re
|
||||
import sys
|
||||
from typing import Dict, List
|
||||
from typing import Any, Dict, List
|
||||
|
||||
import toldg.models
|
||||
import toldg.predict
|
||||
import toldg.utils
|
||||
import toldg.write
|
||||
from toldg.models import Config, CsvConfig, Transaction
|
||||
from toldg.models import Config, CsvConfig, Mapping, Transaction
|
||||
|
||||
|
||||
def process_ldg_files(config: Config):
|
||||
@@ -76,26 +76,33 @@ def find_duplicates(transactions: List[Transaction]):
|
||||
rows.add(row)
|
||||
|
||||
|
||||
def apply_mappings(transactions: List[Transaction], mappings: Dict[str, str]):
|
||||
def apply_mappings(transactions: List[Transaction], mappings: Dict[str, Mapping]):
|
||||
"""Apply mappings to transactions."""
|
||||
unused_mappings = set(mappings.keys())
|
||||
|
||||
for t in transactions:
|
||||
if t.row in mappings:
|
||||
t.account2 = mappings[t.row]
|
||||
mapping = mappings[t.row]
|
||||
|
||||
assert isinstance(
|
||||
mapping, Mapping
|
||||
), "Only new mappings format is supported."
|
||||
t.account2 = mapping.account2
|
||||
|
||||
if mapping.narration:
|
||||
t.narration = mapping.narration
|
||||
|
||||
if mapping.payee:
|
||||
t.payee = mapping.payee
|
||||
|
||||
unused_mappings.discard(t.row)
|
||||
else:
|
||||
logging.warning(f"No mapping for '{t}'.")
|
||||
|
||||
for row in unused_mappings:
|
||||
logging.warning(f"Unused mapping '{row}' -> {mappings[row]}.")
|
||||
|
||||
|
||||
def apply_descriptions(transactions: List[Transaction], descriptions: Dict[str, str]):
|
||||
unused_descriptions = set(descriptions.keys())
|
||||
for t in transactions:
|
||||
if t.row in descriptions:
|
||||
t.description = descriptions[t.row]
|
||||
unused_descriptions.discard(t.row)
|
||||
for row in unused_descriptions:
|
||||
logging.warning(f"Unused mapping '{row}' -> {descriptions[row]}.")
|
||||
mapping_info = mappings[row]
|
||||
account2 = mapping_info["account2"]
|
||||
logging.warning(f"Unused mapping '{row}' -> {account2}")
|
||||
|
||||
|
||||
def process_csv_files(config: Config):
|
||||
@@ -109,13 +116,8 @@ def process_csv_files(config: Config):
|
||||
if config.find_duplicates:
|
||||
find_duplicates(transactions)
|
||||
|
||||
if config.descriptions_file is not None:
|
||||
descriptions = toldg.utils.read_descriptions(config.descriptions_file)
|
||||
apply_descriptions(transactions, descriptions)
|
||||
|
||||
mappings = toldg.utils.read_mappings(config.mappings_file)
|
||||
apply_mappings(transactions, mappings)
|
||||
|
||||
toldg.predict.add_account2(transactions, config.categories)
|
||||
toldg.utils.write_mappings(transactions, config.mappings_file)
|
||||
toldg.write.render_to_file(transactions, config)
|
||||
|
||||
Reference in New Issue
Block a user