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.
if path_provided:
with path.open(mode='x'):
pass
# try to copy in a template config to the user's directory if # try to copy in a template config to the user's dir if one
# one exists. # exists.
else:
fn: str = _conf_fn_w_ext(conf_name)
template: Path = repodir() / 'config' / fn template: Path = repodir() / 'config' / fn
if template.is_file(): if template.is_file():
shutil.copyfile(template, path) shutil.copyfile(template, path)
else: # just touch an empty file with same name
with path.open(mode='x'):
pass
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,6 +314,7 @@ def load_account(
'Please delete the old file!\n' 'Please delete the old file!\n'
f'|-> {legacypath}\n' f'|-> {legacypath}\n'
) )
if legacypath.is_file():
legacy_config, _ = load( legacy_config, _ = load(
path=legacypath, path=legacypath,

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 (