generated from felixm/defaultpy
Implement option to add account2s and commodities to resolve #2.
This commit is contained in:
@@ -43,6 +43,8 @@ class Config(BaseModel):
|
|||||||
mappings_file: Path
|
mappings_file: Path
|
||||||
output_file: Path = Path("output.ldg")
|
output_file: Path = Path("output.ldg")
|
||||||
csv_configs: List[CsvConfig]
|
csv_configs: List[CsvConfig]
|
||||||
|
categories: List[str]
|
||||||
|
commodities: List[str]
|
||||||
|
|
||||||
|
|
||||||
class Transaction(BaseModel):
|
class Transaction(BaseModel):
|
||||||
|
|||||||
@@ -3,12 +3,6 @@ from src.fzf import iterfzf
|
|||||||
from typing import List
|
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 get_sort_categories():
|
||||||
def sort_categories(row: str, categories: List[str]):
|
def sort_categories(row: str, categories: List[str]):
|
||||||
if learn is None:
|
if learn is None:
|
||||||
@@ -29,8 +23,7 @@ def get_sort_categories():
|
|||||||
return sort_categories
|
return sort_categories
|
||||||
|
|
||||||
|
|
||||||
def add_account2(transactions: List[Transaction]):
|
def add_account2(transactions: List[Transaction], categories: List[str]):
|
||||||
categories = get_categories(transactions)
|
|
||||||
unmapped_transactions = filter(lambda t: t.account2 == UNKNOWN_CATEGORY, transactions)
|
unmapped_transactions = filter(lambda t: t.account2 == UNKNOWN_CATEGORY, transactions)
|
||||||
sort_categories = get_sort_categories()
|
sort_categories = get_sort_categories()
|
||||||
for t in unmapped_transactions:
|
for t in unmapped_transactions:
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ def process_csv_files(config: Config):
|
|||||||
find_duplicates(transactions)
|
find_duplicates(transactions)
|
||||||
mappings = src.utils.read_mappings(config.mappings_file)
|
mappings = src.utils.read_mappings(config.mappings_file)
|
||||||
apply_mappings(transactions, mappings)
|
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.utils.write_mappings(transactions, config.mappings_file)
|
||||||
src.write.render_to_file(transactions, config.output_file)
|
src.write.render_to_file(transactions, config)
|
||||||
|
|
||||||
|
|||||||
11
src/utils.py
11
src/utils.py
@@ -45,6 +45,17 @@ def load_config() -> Config:
|
|||||||
return 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):
|
def write_mappings(transactions: List[Transaction], mappings_file: Path):
|
||||||
mappings = {}
|
mappings = {}
|
||||||
for t in transactions:
|
for t in transactions:
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import List
|
from typing import List
|
||||||
from src.models import Transaction
|
from src.models import Transaction, Config
|
||||||
|
|
||||||
|
|
||||||
LEDGER_TRANSACTION_TEMPLATE = """
|
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)
|
content = "".join([LEDGER_TRANSACTION_TEMPLATE.format(t=t)
|
||||||
for t in transactions])
|
for t in transactions])
|
||||||
with open(ledger_file, 'a') as f:
|
with open(config.output_file, 'a') as f:
|
||||||
f.write(content)
|
f.write(content)
|
||||||
|
|||||||
1
toldg.py
1
toldg.py
@@ -34,6 +34,7 @@ def main():
|
|||||||
init_logging()
|
init_logging()
|
||||||
config = src.utils.load_config()
|
config = src.utils.load_config()
|
||||||
src.utils.remove_if_exists(config.output_file)
|
src.utils.remove_if_exists(config.output_file)
|
||||||
|
src.utils.write_meta(config)
|
||||||
src.process.process_ldg_files(config)
|
src.process.process_ldg_files(config)
|
||||||
src.process.process_csv_files(config)
|
src.process.process_csv_files(config)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user