toldg now uses Python build-in templating, not jinja2\ngetofx uses ofx library for extracting transactions
This commit is contained in:
38
toldg.py
38
toldg.py
@@ -8,9 +8,7 @@ import time
|
||||
import re
|
||||
import datetime
|
||||
import logging
|
||||
import jinja2
|
||||
import shutil
|
||||
import tempfile
|
||||
from dataclasses import dataclass, field
|
||||
from typing import List, Tuple
|
||||
|
||||
@@ -80,10 +78,9 @@ class LdgTransaction:
|
||||
|
||||
|
||||
LEDGER_TRANSACTION_TEMPLATE = """
|
||||
{{t.date}} {{t.description}} ; {{t.row}}
|
||||
{{t.account2}} {{t.currency}} {{t.debit}}
|
||||
{{t.account1}} {{t.currency}} {{t.credit}}
|
||||
|
||||
{t.date} {t.description} ; {t.row}
|
||||
{t.account2} {t.currency} {t.debit}
|
||||
{t.account1} {t.currency} {t.credit}
|
||||
"""
|
||||
|
||||
|
||||
@@ -207,37 +204,21 @@ def get_transactions(csv_file, config: CsvConfig, mappings: List[CsvMapping]):
|
||||
|
||||
|
||||
def render_to_file(transactions, csv_file, ledger_file, template_file=""):
|
||||
if template_file:
|
||||
dirname = os.path.dirname(template_file)
|
||||
template_file = os.path.basename(template_file)
|
||||
template_loader = jinja2.FileSystemLoader(searchpath=dirname)
|
||||
template_env = jinja2.Environment(loader=template_loader)
|
||||
template = template_env.get_template(template_file)
|
||||
else:
|
||||
template_env = jinja2.Environment(loader=jinja2.BaseLoader)
|
||||
template = template_env.from_string(LEDGER_TRANSACTION_TEMPLATE)
|
||||
|
||||
# Write transactions into virtual file. We could just create a string
|
||||
# object, but that doesn't work as nicely with the Jinja API plus I think
|
||||
# this approach is faster.
|
||||
tf = tempfile.SpooledTemporaryFile(mode='w+')
|
||||
for t in transactions:
|
||||
tf.write(template.render(t=t))
|
||||
tf.seek(0)
|
||||
new_ledger_content = tf.read()
|
||||
content = "".join([LEDGER_TRANSACTION_TEMPLATE.format(t=t)
|
||||
for t in transactions])
|
||||
|
||||
status = "no change"
|
||||
if not os.path.isfile(ledger_file):
|
||||
with open(ledger_file, 'w') as f:
|
||||
f.write(new_ledger_content)
|
||||
f.write(new_content)
|
||||
status = "new"
|
||||
else:
|
||||
with open(ledger_file, 'r') as f:
|
||||
old_ledger_content = f.read()
|
||||
old_content = f.read()
|
||||
f.close()
|
||||
if new_ledger_content != old_ledger_content:
|
||||
if old_content != content:
|
||||
with open(ledger_file, 'w') as f:
|
||||
f.write(new_ledger_content)
|
||||
f.write(content)
|
||||
status = "update"
|
||||
logging.info(f"{csv_file:30} -> {ledger_file:30} | {status}")
|
||||
|
||||
@@ -314,3 +295,4 @@ if __name__ == "__main__":
|
||||
with open(config_file, 'r') as f:
|
||||
config = Config(**json.load(f))
|
||||
main(config)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user