From 8acaa28df03ff92a5f859dbbc2ea24886d58e067 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Wed, 16 Feb 2022 08:53:54 -0500 Subject: [PATCH] Drop import, it's got madness with and SIGINT? --- piker/config.py | 66 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 2 deletions(-) diff --git a/piker/config.py b/piker/config.py index 836da432..cf946405 100644 --- a/piker/config.py +++ b/piker/config.py @@ -17,6 +17,8 @@ """ Broker configuration mgmt. """ +import platform +import sys import os from os.path import dirname import shutil @@ -24,13 +26,73 @@ from typing import Optional from bidict import bidict import toml -import click from .log import get_logger log = get_logger('broker-config') -_config_dir = _click_config_dir = click.get_app_dir('piker') + +# taken from ``click`` since apparently they have some +# super weirdness with sigint and sudo..no clue +def get_app_dir(app_name, roaming=True, force_posix=False): + r"""Returns the config folder for the application. The default behavior + is to return whatever is most appropriate for the operating system. + + To give you an idea, for an app called ``"Foo Bar"``, something like + the following folders could be returned: + + Mac OS X: + ``~/Library/Application Support/Foo Bar`` + Mac OS X (POSIX): + ``~/.foo-bar`` + Unix: + ``~/.config/foo-bar`` + Unix (POSIX): + ``~/.foo-bar`` + Win XP (roaming): + ``C:\Documents and Settings\\Local Settings\Application Data\Foo Bar`` + Win XP (not roaming): + ``C:\Documents and Settings\\Application Data\Foo Bar`` + Win 7 (roaming): + ``C:\Users\\AppData\Roaming\Foo Bar`` + Win 7 (not roaming): + ``C:\Users\\AppData\Local\Foo Bar`` + + .. versionadded:: 2.0 + + :param app_name: the application name. This should be properly capitalized + and can contain whitespace. + :param roaming: controls if the folder should be roaming or not on Windows. + Has no affect otherwise. + :param force_posix: if this is set to `True` then on any POSIX system the + folder will be stored in the home folder with a leading + dot instead of the XDG config home or darwin's + application support folder. + """ + + def _posixify(name): + return "-".join(name.split()).lower() + + # if WIN: + if platform.system() == 'Windows': + key = "APPDATA" if roaming else "LOCALAPPDATA" + folder = os.environ.get(key) + if folder is None: + folder = os.path.expanduser("~") + return os.path.join(folder, app_name) + if force_posix: + return os.path.join(os.path.expanduser("~/.{}".format(_posixify(app_name)))) + if sys.platform == "darwin": + return os.path.join( + os.path.expanduser("~/Library/Application Support"), app_name + ) + return os.path.join( + os.environ.get("XDG_CONFIG_HOME", os.path.expanduser("~/.config")), + _posixify(app_name), + ) + + +_config_dir = _click_config_dir = get_app_dir('piker') _parent_user = os.environ.get('SUDO_USER') if _parent_user: