Fix account config loading logic discovered in new test XD

master
Tyler Goodlet 2023-05-25 17:56:14 -04:00
parent 9bc11d8dd9
commit 4a8e8a32f9
2 changed files with 41 additions and 32 deletions

View File

@ -241,8 +241,6 @@ def load(
pass the ``path: Path`` explicitly. pass the ``path: Path`` explicitly.
''' '''
path: Path = path or get_conf_path(conf_name)
# create the $HOME/.config/piker dir if dne # create the $HOME/.config/piker dir if dne
if not _config_dir.is_dir(): if not _config_dir.is_dir():
_config_dir.mkdir( _config_dir.mkdir(
@ -250,22 +248,27 @@ def load(
exist_ok=True, exist_ok=True,
) )
path_provided: bool = path is not None
path: Path = path or get_conf_path(conf_name)
if ( if (
not path.is_file() not path.is_file()
and touch_if_dne and touch_if_dne
): ):
fn: str = _conf_fn_w_ext(conf_name) # only do a template if no path provided,
# just touch an empty file with same name.
# try to copy in a template config to the user's directory if if path_provided:
# one exists.
template: Path = repodir() / 'config' / fn
if template.is_file():
shutil.copyfile(template, path)
else: # just touch an empty file with same name
with path.open(mode='x'): with path.open(mode='x'):
pass pass
# try to copy in a template config to the user's dir if one
# exists.
else:
fn: str = _conf_fn_w_ext(conf_name)
template: Path = repodir() / 'config' / fn
if template.is_file():
shutil.copyfile(template, path)
with path.open(mode='r') as fp: with path.open(mode='r') as fp:
config: dict = decode( config: dict = decode(
fp.read(), fp.read(),
@ -283,16 +286,24 @@ def load_account(
) -> tuple[dict, Path]: ) -> tuple[dict, Path]:
''' '''
Load a accounting (with positions) file from Load a accounting (with positions) file from
~/.config/piker/accounting/account.<brokername>.<acctid>.toml. $PIKER_CONFIG_DIR/accounting/account.<brokername>.<acctid>.toml.
Where normally $PIKER_CONFIG_DIR = ~/.config/piker/
and we implicitly create a accounting subdir which should
normally be linked to a git repo managed by the user B)
''' '''
legacy_fn: str = f'pps.{brokername}.{acctid}.toml' legacy_fn: str = f'pps.{brokername}.{acctid}.toml'
fn: str = f'account.{brokername}.{acctid}.toml' fn: str = f'account.{brokername}.{acctid}.toml'
dirpath: Path = _config_dir / 'accounting' dirpath: Path = _config_dir / 'accounting'
if not dirpath.is_dir():
dirpath.mkdir()
config, path = load( config, path = load(
path=dirpath / fn, path=dirpath / fn,
decode=tomlkit.parse, decode=tomlkit.parse,
touch_if_dne=True,
) )
if not config: if not config:
@ -303,25 +314,26 @@ def load_account(
'Please delete the old file!\n' 'Please delete the old file!\n'
f'|-> {legacypath}\n' f'|-> {legacypath}\n'
) )
legacy_config, _ = load( if legacypath.is_file():
path=legacypath, legacy_config, _ = load(
path=legacypath,
# TODO: move to tomlkit: # TODO: move to tomlkit:
# - needs to be fixed to support bidict? # - needs to be fixed to support bidict?
# https://github.com/sdispater/tomlkit/issues/289 # https://github.com/sdispater/tomlkit/issues/289
# - we need to use or fork's fix to do multiline array # - we need to use or fork's fix to do multiline array
# indenting. # indenting.
decode=tomlkit.parse, decode=tomlkit.parse,
) )
config.update(legacy_config) config.update(legacy_config)
# XXX: override the presumably previously non-existant # XXX: override the presumably previously non-existant
# file with legacy's contents. # file with legacy's contents.
write( write(
config, config,
path=path, path=path,
fail_empty=False, fail_empty=False,
) )
return config, path return config, path

View File

@ -19,8 +19,6 @@
""" """
from __future__ import annotations from __future__ import annotations
from pprint import pformat
from functools import partial
import os import os
from typing import ( from typing import (
Optional, Optional,
@ -35,7 +33,6 @@ import tractor
import trio import trio
from ._util import ( from ._util import (
log, # sub-sys logger
get_console_log, get_console_log,
) )
from ._mngr import ( from ._mngr import (