Add `.config.load_ledger()` for transaction record files

master
Tyler Goodlet 2023-05-12 13:01:45 -04:00
parent 488a0cd119
commit 5278f8b560
1 changed files with 30 additions and 2 deletions

View File

@ -22,11 +22,13 @@ import platform
import sys import sys
import os import os
import shutil import shutil
import time
from typing import Optional from typing import Optional
from pathlib import Path from pathlib import Path
from bidict import bidict from bidict import bidict
import toml import toml
import tomli
# import tomlkit # TODO! # import tomlkit # TODO!
from .log import get_logger from .log import get_logger
@ -220,7 +222,7 @@ def load(
**tomlkws, **tomlkws,
) -> tuple[dict, str]: ) -> tuple[dict, Path]:
''' '''
Load config file by name. Load config file by name.
@ -265,7 +267,7 @@ def load_account(
brokername: str, brokername: str,
acctid: str, acctid: str,
) -> tuple[dict, str]: ) -> tuple[dict, Path]:
''' '''
Load a accounting (with positions) file from Load a accounting (with positions) file from
~/.config/piker/accounting/account.<brokername>.<acctid>.toml. ~/.config/piker/accounting/account.<brokername>.<acctid>.toml.
@ -298,6 +300,32 @@ def load_account(
return config, path return config, path
def load_ledger(
brokername: str,
acctid: str,
) -> tuple[dict, Path]:
ldir: Path = _config_dir / 'accounting' / 'ledgers'
if not ldir.is_dir():
ldir.mkdir()
fname = f'trades_{brokername}_{acctid}.toml'
fpath: Path = ldir / fname
if not fpath.is_file():
log.info(
f'Creating new local trades ledger: {fpath}'
)
fpath.touch()
with fpath.open(mode='rb') as cf:
start = time.time()
ledger_dict = tomli.load(cf)
log.debug(f'Ledger load took {time.time() - start}s')
return ledger_dict, fpath
def write( def write(
config: dict, # toml config as dict config: dict, # toml config as dict