Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linux/storagepath: fixup a host of issues #646

Merged
merged 2 commits into from
Jul 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 20 additions & 19 deletions plyer/platforms/linux/storagepath.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,36 @@
'''

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"

PATHS = {
"DESKTOP": "/Desktop",
"DOCUMENTS": "/Documents",
"DOWNLOAD": "/Downloads",
"MUSIC": "/Music",
"PICTURES": "/Pictures",
"VIDEOS": "/Videos"
"DESKTOP": "Desktop",
"DOCUMENTS": "Documents",
"DOWNLOAD": "Downloads",
"MUSIC": "Music",
"PICTURES": "Pictures",
"VIDEOS": "Videos"
}


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_<name> 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('~')
Expand All @@ -44,7 +45,7 @@ def _get_root_dir(self):
return "/"

def _get_documents_dir(self):
directory = self._get_from_user_dirs("DOCUMENT")
directory = self._get_from_user_dirs("DOCUMENTS")
return directory.replace("$HOME", self._get_home_dir())

def _get_downloads_dir(self):
Expand Down