From abb28a76f9b69a8916f1e7d7d42ac665e9c27ff4 Mon Sep 17 00:00:00 2001 From: Romanos Skiadas Date: Fri, 17 Sep 2021 20:41:42 +0300 Subject: [PATCH] linux/storagepath: Revamp handling of user-dirs - dont assume that ~/.config/user-dirs.dirs exists - don't assume it does have contents or the relevant value - don't use exception handling as happy path control flow - when defaulting, use the path under HOME, not e.g. /Pictures --- plyer/platforms/linux/storagepath.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/plyer/platforms/linux/storagepath.py b/plyer/platforms/linux/storagepath.py index 5811273e..1753176a 100755 --- a/plyer/platforms/linux/storagepath.py +++ b/plyer/platforms/linux/storagepath.py @@ -4,7 +4,7 @@ ''' from plyer.facades import StoragePath -from os.path import expanduser, dirname, abspath +from os.path import expanduser, dirname, abspath, join, exists # Default paths for each name USER_DIRS = "/.config/user-dirs.dirs" @@ -22,17 +22,18 @@ class LinuxStoragePath(StoragePath): def _get_from_user_dirs(self, name): - try: - with open(self._get_home_dir() + USER_DIRS, "r") as f: - lines = f.readlines() - # Find the line that starts with XDG_ to get the path - index = [i for i, v in enumerate(lines) - if v.startswith("XDG_" + name)][0] - return lines[index].split('"')[1] - except KeyError: - return PATHS[name] - except Exception as e: - raise e + home_dir = self.__get_home_dir(), + default = join(home_dir, PATHS[name]) + user_dirs = join(home_dir, USER_DIRS) + if not exists(user_dirs): + return default + + with open(user_dirs, "r") as f: + for l in f.readlines(): + if l.startswith("XDG_" + name): + return l.split('"')[1] + + return default def _get_home_dir(self): return expanduser('~')