Update ledgerai to read existing transactions from beancount file

This commit is contained in:
2025-12-20 15:33:08 -05:00
parent f56c559c84
commit 702fdccf0a
11 changed files with 154 additions and 461 deletions

View File

@@ -14,10 +14,9 @@ from toldg.models import Config, CsvConfig, Mapping, Transaction
def process_ldg_files(config: Config):
with open(config.output_file, "a") as f_out:
f_out.write("\n")
for ldg_file in toldg.utils.get_ldg_files(config.input_directory):
ldg_rel = os.path.relpath(ldg_file, os.path.dirname(config.output_file))
f_out.write(f"include \"{ldg_rel}\"\n")
f_out.write(f'include "{ldg_rel}"\n')
def get_csv_config(csv_file: str, csv_configs: list[CsvConfig]) -> CsvConfig:
@@ -63,8 +62,7 @@ def get_transactions(csv_file: str, config: CsvConfig) -> list[Transaction]:
for _ in range(config.skip):
next(reader)
rows = [row for row in reader if row]
transactions = [row_to_transaction(i, row, fields)
for i, row in enumerate(reversed(rows))]
transactions = [row_to_transaction(i, row, fields) for i, row in enumerate(reversed(rows))]
return transactions
@@ -74,9 +72,7 @@ def apply_mappings(transactions: list[Transaction], mappings: dict[str, Mapping]
if t.key() in mappings:
mapping = mappings[t.key()]
assert isinstance(mapping, Mapping)
assert (
mapping.count > 0
), f"{mapping} used by {t} but count is not greater than '0'."
assert mapping.count > 0, f"{mapping} used by {t} but count is not greater than '0'."
mapping.count -= 1
t.mapping = mapping
else:
@@ -86,7 +82,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, write_outputs: bool = True) -> list[Transaction]:
csv_files = toldg.utils.get_csv_files(config.input_directory)
transactions = []
for csv_file in csv_files:
@@ -97,6 +93,7 @@ def process_csv_files(config: Config) -> list[Transaction]:
mappings = toldg.utils.read_mappings(config.mappings_file)
apply_mappings(transactions, mappings)
toldg.predict.add_account2(config.model, transactions, config.categories)
toldg.utils.write_mappings(transactions, config.mappings_file)
toldg.write.render_to_file(transactions, config)
if write_outputs:
toldg.utils.write_mappings(transactions, config.mappings_file)
toldg.write.render_to_file(transactions, config)
return transactions