From 779c0b73c9bb24b7696ef0bfba3119bbdcee9f85 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Sat, 13 May 2023 16:07:17 -0400 Subject: [PATCH] Make `.accounting._ledger` use `tomlkit` So that styling is preserved on write but requires that we pop `None` values (in this case any unset `.expiry` transactions) due to `tomkit` having no support for writing them as values? --- piker/accounting/_ledger.py | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/piker/accounting/_ledger.py b/piker/accounting/_ledger.py index 564ba9fe..b46b46ba 100644 --- a/piker/accounting/_ledger.py +++ b/piker/accounting/_ledger.py @@ -34,7 +34,7 @@ from pendulum import ( datetime, parse, ) -import toml +import tomlkit from .. import config from ..data.types import Struct @@ -122,17 +122,27 @@ class TransactionLedger(UserDict): def write_config(self) -> None: ''' - Render the self.data ledger dict to it's TML file form. + Render the self.data ledger dict to it's TOML file form. ''' + towrite: dict[str, Any] = self.data.copy() + + for tid, txdict in self.data.items(): + + # drop key for non-expiring assets + if ( + 'expiry' in txdict + and txdict['expiry'] is None + ): + txdict.pop('expiry') + + # re-write old acro-key + fqme = txdict.get('fqsn') + if fqme: + txdict['fqme'] = fqme + with self.file_path.open(mode='w') as fp: - - # rewrite the key name to fqme if needed - fqsn: str = self.data.get('fqsn') - if fqsn: - self.data['fqme'] = fqsn - - toml.dump(self.data, fp) + tomlkit.dump(towrite, fp) def update_from_t( self, @@ -239,7 +249,7 @@ def open_trade_ledger( # TODO: show diff output? # https://stackoverflow.com/questions/12956957/print-diff-of-python-dictionaries - log.info(f'Updating ledger for {tradesfile}:\n') + log.info(f'Updating ledger for {fpath}:\n') ledger.write_config()