1
0
This repository has been archived on 2023-12-27. You can view files and clone it, but cannot push or open issues or pull requests.
ledger-sqlite/src/main.py

44 lines
1.1 KiB
Python

"""
Convert the provided ledger file into a SQLite database.
Configure input and output files with environment variables.
"""
import os
import os.path
import sys
from db import CursorContextManager, create_tables, write_transactions
from ledger import get_transactions
# TODO: Replace with os.environ.get() and produce nice error messages.
ledger_file = os.environ["LEDGER_FILE"]
output_file = os.environ["OUTPUT_FILE"]
overwrite_output = os.environ["OVERWRITE_OUTPUT"]
print(f"Converting {ledger_file} to {output_file}")
def to_bool(val: str) -> bool:
"""
Return if the provided string is consider truthy.
"""
return val.lower() in ["true", "1", "t", "y", "yes"]
if os.path.exists(output_file):
if not to_bool(overwrite_output):
print("Output file exists. Set OVERWRITE_OUTPUT=true to overwrite.")
sys.exit(1)
else:
print("Output file exists. Overwriting.")
txs = get_transactions(ledger_file)
with CursorContextManager(filename=output_file) as db:
create_tables(db)
write_transactions(db, txs)
print("Conversion complete.")