This repository has been archived on 2024-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
Bebop/bebop/fs.py

48 lines
1.4 KiB
Python
Raw Normal View History

"""Retrieve some paths from filesystem.
A lot of logic comes from `appdirs`:
https://github.com/ActiveState/appdirs/blob/master/appdirs.py
"""
2021-04-17 22:59:54 +02:00
from functools import lru_cache
from os import getenv
2021-03-13 20:38:19 +01:00
from os.path import expanduser
from pathlib import Path
APP_NAME = "bebop"
@lru_cache(None)
def get_config_path() -> Path:
"""Return the user config file path."""
config_dir = Path(getenv("XDG_CONFIG_HOME", expanduser("~/.config")))
return config_dir / (APP_NAME + ".json")
2021-04-17 22:59:54 +02:00
@lru_cache(None)
def get_user_data_path() -> Path:
"""Return the user data directory path."""
path = Path(getenv("XDG_DATA_HOME", expanduser("~/.local/share")))
return path / APP_NAME
2021-04-17 22:59:54 +02:00
@lru_cache(None)
def get_downloads_path() -> Path:
"""Return the user downloads directory path."""
xdg_config_path = Path(getenv("XDG_CONFIG_HOME", expanduser("~/.config")))
download_path = ""
try:
with open(xdg_config_path / "user-dirs.dirs", "rt") as user_dirs_file:
for line in user_dirs_file:
if line.startswith("XDG_DOWNLOAD_DIR="):
download_path = line.rstrip().split("=", maxsplit=1)[1]
download_path = download_path.strip('"')
download_path = download_path.replace("$HOME", expanduser("~"))
break
except OSError:
pass
if download_path:
return Path(download_path)
return Path.home()