Add `touch_if_dne: bool` to `config.load()`

So that we aren't creating blank files for legacy configs (as we do name
changes or wtv). Further change `.get_conf_path()` to validate against
new `account.` prefix and a god `conf.toml` file.
master
Tyler Goodlet 2023-05-13 16:05:23 -04:00
parent df96155057
commit 50a4c425d3
1 changed files with 14 additions and 10 deletions

View File

@ -152,11 +152,9 @@ if _parent_user:
_conf_names: set[str] = { _conf_names: set[str] = {
'piker', # god config 'conf', # god config
'brokers', # sec backend deatz 'brokers', # sec backend deatz
# 'trades', # 'watchlists', # (user defined) market lists
'watchlists',
# 'paper_trades'
} }
# TODO: probably drop all this super legacy, questrade specific, # TODO: probably drop all this super legacy, questrade specific,
@ -207,7 +205,7 @@ def get_conf_path(
- strats.toml - strats.toml
''' '''
if 'pps.' not in conf_name: if 'account.' not in conf_name:
assert str(conf_name) in _conf_names assert str(conf_name) in _conf_names
fn = _conf_fn_w_ext(conf_name) fn = _conf_fn_w_ext(conf_name)
@ -223,13 +221,14 @@ def repodir() -> Path:
def load( def load(
conf_name: str = 'brokers', conf_name: str = 'brokers', # appended with .toml suffix
path: Path | None = None, path: Path | None = None,
decode: Callable[ decode: Callable[
[str | bytes,], [str | bytes,],
MutableMapping, MutableMapping,
] = tomllib.loads, ] = tomllib.loads,
touch_if_dne: bool = False,
**tomlkws, **tomlkws,
@ -237,9 +236,13 @@ def load(
''' '''
Load config file by name. Load config file by name.
If desired config is not in the top level piker-user config path then
pass the ``path: Path`` explicitly.
''' '''
path: Path = path or get_conf_path(conf_name) path: Path = path or get_conf_path(conf_name)
# 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(
parents=True, parents=True,
@ -255,8 +258,9 @@ def load(
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:
# create empty file # touch an empty file
elif touch_if_dne:
with path.open(mode='x'): with path.open(mode='x'):
pass pass
@ -292,10 +296,10 @@ def load_account(
if not config: if not config:
legacypath = dirpath / legacy_fn legacypath = dirpath / legacy_fn
log.warning( log.warning(
f'Your account file -> {legacypath}\n' f'Your account file is using the legacy `pps.` prefix..\n'
f'is using the legacy `pps.` prefix..\n'
f'Rewriting contents to new name -> {path}\n' f'Rewriting contents to new name -> {path}\n'
'Please delete the old file!\n' 'Please delete the old file!\n'
f'|-> {legacypath}\n'
) )
legacy_config, _ = load( legacy_config, _ = load(
path=legacypath, path=legacypath,