From 1cdb94374c961bd9a395a7ca41ccea2de878d2c5 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Tue, 15 Feb 2022 14:03:24 -0500 Subject: [PATCH] Extract non-sudo user for config dir path --- piker/cli/__init__.py | 20 +++----------------- piker/config.py | 28 +++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/piker/cli/__init__.py b/piker/cli/__init__.py index 7eb7b5d1..3dc946b8 100644 --- a/piker/cli/__init__.py +++ b/piker/cli/__init__.py @@ -16,20 +16,6 @@ from .. import config log = get_logger('cli') DEFAULT_BROKER = 'questrade' -_config_dir = click.get_app_dir('piker') -_watchlists_data_path = os.path.join(_config_dir, 'watchlists.json') -_context_defaults = dict( - default_map={ - # Questrade specific quote poll rates - 'monitor': { - 'rate': 3, - }, - 'optschain': { - 'rate': 1, - }, - } -) - @click.command() @click.option('--loglevel', '-l', default='warning', help='Logging level') @@ -58,7 +44,7 @@ def pikerd(loglevel, host, tl, pdb): trio.run(main) -@click.group(context_settings=_context_defaults) +@click.group(context_settings=config._context_defaults) @click.option( '--brokers', '-b', default=[DEFAULT_BROKER], @@ -87,8 +73,8 @@ def cli(ctx, brokers, loglevel, tl, configdir): 'loglevel': loglevel, 'tractorloglevel': None, 'log': get_console_log(loglevel), - 'confdir': _config_dir, - 'wl_path': _watchlists_data_path, + 'confdir': config._config_dir, + 'wl_path': config._watchlists_data_path, }) # allow enabling same loglevel in ``tractor`` machinery diff --git a/piker/config.py b/piker/config.py index 93a47378..836da432 100644 --- a/piker/config.py +++ b/piker/config.py @@ -30,8 +30,34 @@ from .log import get_logger log = get_logger('broker-config') -_config_dir = click.get_app_dir('piker') +_config_dir = _click_config_dir = click.get_app_dir('piker') +_parent_user = os.environ.get('SUDO_USER') + +if _parent_user: + non_root_user_dir = os.path.expanduser( + f'~{_parent_user}' + ) + root = 'root' + _config_dir = ( + non_root_user_dir + + _click_config_dir[ + _click_config_dir.rfind(root) + len(root): + ] + ) + _file_name = 'brokers.toml' +_watchlists_data_path = os.path.join(_config_dir, 'watchlists.json') +_context_defaults = dict( + default_map={ + # Questrade specific quote poll rates + 'monitor': { + 'rate': 3, + }, + 'optschain': { + 'rate': 1, + }, + } +) def _override_config_dir(