diff --git a/piker/pp.py b/piker/pp.py index 33040654fb..6dc5c2ce71 100644 --- a/piker/pp.py +++ b/piker/pp.py @@ -22,6 +22,7 @@ ''' from __future__ import annotations from contextlib import contextmanager as cm +from pathlib import Path from decimal import Decimal, ROUND_HALF_EVEN from pprint import pformat import os @@ -198,11 +199,9 @@ def to_pretoml(self) -> tuple[str, dict]: sym_info = s.broker_info[broker] d['symbol'] = { - 'info': { - 'asset_type': sym_info['asset_type'], - 'price_tick_size': sym_info['price_tick_size'], - 'lot_tick_size': sym_info['lot_tick_size'] - } + 'asset_type': sym_info['asset_type'], + 'price_tick_size': sym_info['price_tick_size'], + 'lot_tick_size': sym_info['lot_tick_size'] } if self.expiry is None: @@ -693,25 +692,32 @@ def write_config(self) -> None: ''' # TODO: show diff output? # https://stackoverflow.com/questions/12956957/print-diff-of-python-dictionaries - log.info(f'Updating ``pps.toml`` for {path}:\n') # active, closed_pp_objs = table.dump_active() pp_entries = self.to_toml() - log.info(f'Current positions:\n{pp_entries}') - self.conf[self.brokername][self.acctid] = pp_entries - - # TODO: why tf haven't they already done this for inline - # tables smh.. - enc = PpsEncoder(preserve=True) - # table_bs_type = type(toml.TomlDecoder().get_empty_inline_table()) - enc.dump_funcs[ - toml.decoder.InlineTableDict - ] = enc.dump_inline_table - - config.write( - self.conf, - 'pps', - encoder=enc, - ) + if pp_entries: + log.info(f'Updating ``pps.toml`` for {path}:\n') + log.info(f'Current positions:\n{pp_entries}') + self.conf[self.brokername][self.acctid] = pp_entries + + # TODO: why tf haven't they already done this for inline + # tables smh.. + enc = PpsEncoder(preserve=True) + # table_bs_type = type(toml.TomlDecoder().get_empty_inline_table()) + enc.dump_funcs[ + toml.decoder.InlineTableDict + ] = enc.dump_inline_table + + config.write( + self.conf, + 'pps', + encoder=enc, + ) + + else: + pp_path = Path(config.get_conf_path('pps')) + if pp_path.is_file(): + log.warning(f'pp_entries empty, removing pps.toml...') + pp_path.unlink() def load_pps_from_ledger( @@ -972,7 +978,8 @@ def open_pps( expiry = pendulum.parse(expiry) pp = pp_objs[bsuid] = Position( - Symbol.from_fqsn(fqsn, info={}), + Symbol.from_fqsn( + fqsn, entry['symbol']), size=size, ppu=ppu, split_ratio=split_ratio,