diff --git a/mslib/msui/constants.py b/mslib/msui/constants.py index fd4315081..cf65dff68 100644 --- a/mslib/msui/constants.py +++ b/mslib/msui/constants.py @@ -34,17 +34,33 @@ # ToDo refactor to generic functions, keep only constants HOME = os.path.expanduser(f"~{os.path.sep}") MSUI_CONFIG_PATH = os.getenv("MSUI_CONFIG_PATH", os.path.join(HOME, ".config", "msui")) +_fs = None if '://' in MSUI_CONFIG_PATH: try: - _fs = fs.open_fs(MSUI_CONFIG_PATH) - except fs.errors.CreateFailed: - _fs.makedirs(MSUI_CONFIG_PATH) + _fs = fs.open_fs(fs.path.dirname(MSUI_CONFIG_PATH)) except fs.opener.errors.UnsupportedProtocol: logging.error('FS url "%s" not supported', MSUI_CONFIG_PATH) + except fs.errors.DirectoryExists: + logging.warning('Directory "%s" already exists', MSUI_CONFIG_PATH) + except fs.errors.CreateFailed: + try: + _fs = fs.open_fs(fs.path.dirname(MSUI_CONFIG_PATH)) + _fs.makedirs(fs.path.basename(MSUI_CONFIG_PATH)) + except fs.errors.DirectoryExists: + logging.warning('Directory "%s" already exists', MSUI_CONFIG_PATH) + except fs.opener.errors.UnsupportedProtocol: + logging.error('FS url "%s" not supported', MSUI_CONFIG_PATH) + except Exception as e: + logging.error('Failed to create directory "%s": %s', MSUI_CONFIG_PATH, e) else: _dir = os.path.expanduser(MSUI_CONFIG_PATH) if not os.path.exists(_dir): - os.makedirs(_dir) + try: + os.makedirs(_dir) + except FileExistsError: + logging.warning('Directory "%s" already exists', MSUI_CONFIG_PATH) + except Exception as e: + logging.error('Failed to create directory "%s": %s', MSUI_CONFIG_PATH, e) GRAVATAR_DIR_PATH = fs.path.join(MSUI_CONFIG_PATH, "gravatars")