From d9e83650dc9cfe0a53371730c975b6d9152439fd Mon Sep 17 00:00:00 2001 From: ed Date: Mon, 9 Aug 2021 22:13:16 +0200 Subject: [PATCH] handle invalid XDG_CONFIG_HOME on linux --- copyparty/__init__.py | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/copyparty/__init__.py b/copyparty/__init__.py index 4b01eae3..5e62b238 100644 --- a/copyparty/__init__.py +++ b/copyparty/__init__.py @@ -25,6 +25,28 @@ ANYWIN = WINDOWS or sys.platform in ["msys"] MACOS = platform.system() == "Darwin" +def get_unix_home(): + try: + v = os.environ["XDG_CONFIG_HOME"] + if not v: + raise Exception() + ret = os.path.normpath(v) + os.listdir(ret) + return ret + except: + pass + + try: + v = os.path.expanduser("~/.config") + if v.startswith("~"): + raise Exception() + ret = os.path.normpath(v) + os.listdir(ret) + return ret + except: + return "/tmp" + + class EnvParams(object): def __init__(self): self.t0 = time.time() @@ -37,10 +59,7 @@ class EnvParams(object): elif sys.platform == "darwin": self.cfg = os.path.expanduser("~/Library/Preferences/copyparty") else: - self.cfg = os.path.normpath( - os.getenv("XDG_CONFIG_HOME", os.path.expanduser("~/.config")) - + "/copyparty" - ) + self.cfg = get_unix_home() + "/copyparty" self.cfg = self.cfg.replace("\\", "/") try: