Implement option to add account2s and commodities to resolve #2.

This commit is contained in:
felixm 2023-10-17 13:45:20 +02:00
parent c96752e599
commit e29d08e0d7
6 changed files with 20 additions and 13 deletions

View File

@ -43,6 +43,8 @@ class Config(BaseModel):
mappings_file: Path
output_file: Path = Path("output.ldg")
csv_configs: List[CsvConfig]
categories: List[str]
commodities: List[str]
class Transaction(BaseModel):

View File

@ -3,12 +3,6 @@ from src.fzf import iterfzf
from typing import List
def get_categories(transactions: List[Transaction]) -> List[str]:
categories = set([t.account2 for t in transactions])
categories.add(UNKNOWN_CATEGORY)
return list(categories)
def get_sort_categories():
def sort_categories(row: str, categories: List[str]):
if learn is None:
@ -29,8 +23,7 @@ def get_sort_categories():
return sort_categories
def add_account2(transactions: List[Transaction]):
categories = get_categories(transactions)
def add_account2(transactions: List[Transaction], categories: List[str]):
unmapped_transactions = filter(lambda t: t.account2 == UNKNOWN_CATEGORY, transactions)
sort_categories = get_sort_categories()
for t in unmapped_transactions:

View File

@ -99,7 +99,7 @@ def process_csv_files(config: Config):
find_duplicates(transactions)
mappings = src.utils.read_mappings(config.mappings_file)
apply_mappings(transactions, mappings)
src.predict.add_account2(transactions)
src.predict.add_account2(transactions, config.categories)
src.utils.write_mappings(transactions, config.mappings_file)
src.write.render_to_file(transactions, config.output_file)
src.write.render_to_file(transactions, config)

View File

@ -45,6 +45,17 @@ def load_config() -> Config:
return config
def write_meta(config: Config):
with open(config.output_file, 'a') as f:
for category in config.categories:
f.write(f"account {category}\n")
f.write("\n")
for commodity in config.commodities:
f.write(f"commodity {commodity}\n")
f.write("\n")
def write_mappings(transactions: List[Transaction], mappings_file: Path):
mappings = {}
for t in transactions:

View File

@ -1,6 +1,6 @@
from pathlib import Path
from typing import List
from src.models import Transaction
from src.models import Transaction, Config
LEDGER_TRANSACTION_TEMPLATE = """
@ -10,8 +10,8 @@ LEDGER_TRANSACTION_TEMPLATE = """
"""
def render_to_file(transactions: List[Transaction], ledger_file: Path):
def render_to_file(transactions: List[Transaction], config: Config):
content = "".join([LEDGER_TRANSACTION_TEMPLATE.format(t=t)
for t in transactions])
with open(ledger_file, 'a') as f:
with open(config.output_file, 'a') as f:
f.write(content)

View File

@ -34,6 +34,7 @@ def main():
init_logging()
config = src.utils.load_config()
src.utils.remove_if_exists(config.output_file)
src.utils.write_meta(config)
src.process.process_ldg_files(config)
src.process.process_csv_files(config)