2021-03-11 19:16:36 +01:00
|
|
|
"""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
|
2021-03-11 19:16:36 +01:00
|
|
|
from os import getenv
|
2021-03-13 20:38:19 +01:00
|
|
|
from os.path import expanduser
|
2021-03-13 16:31:11 +01:00
|
|
|
from pathlib import Path
|
2021-05-12 22:29:03 +02:00
|
|
|
from typing import Optional
|
2021-03-11 19:16:36 +01:00
|
|
|
|
|
|
|
|
|
|
|
APP_NAME = "bebop"
|
|
|
|
|
|
|
|
|
2021-04-18 02:27:05 +02:00
|
|
|
@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)
|
2021-03-13 16:31:11 +01:00
|
|
|
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:
|
2021-05-09 23:02:56 +02:00
|
|
|
"""Return the user downloads directory path (fallbacks to home dir)."""
|
2021-04-17 22:59:54 +02:00
|
|
|
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('"')
|
2021-05-24 21:11:58 +02:00
|
|
|
home = expanduser("~")
|
|
|
|
download_path = download_path.replace("$HOME", home)
|
|
|
|
return Path(download_path)
|
2021-04-17 22:59:54 +02:00
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
return Path.home()
|
2021-04-19 02:04:18 +02:00
|
|
|
|
|
|
|
|
2021-05-12 22:29:03 +02:00
|
|
|
@lru_cache(None)
|
2021-06-03 16:45:35 +02:00
|
|
|
def get_identities_list_path() -> Path:
|
2021-05-12 22:29:03 +02:00
|
|
|
"""Return the identities JSON file path."""
|
|
|
|
return get_user_data_path() / "identities.json"
|
|
|
|
|
|
|
|
|
|
|
|
@lru_cache(None)
|
2021-06-03 16:45:35 +02:00
|
|
|
def get_identities_path() -> Path:
|
2021-05-12 22:29:03 +02:00
|
|
|
"""Return the directory where identities are stored."""
|
|
|
|
return get_user_data_path() / "identities"
|
|
|
|
|
|
|
|
|
2021-05-29 01:13:43 +02:00
|
|
|
@lru_cache(None)
|
2021-06-03 16:45:35 +02:00
|
|
|
def get_capsule_prefs_path() -> Path:
|
2021-05-29 01:13:43 +02:00
|
|
|
"""Return the directory where identities are stored."""
|
|
|
|
return get_user_data_path() / "capsule_prefs.json"
|
|
|
|
|
|
|
|
|
2021-06-03 16:45:35 +02:00
|
|
|
@lru_cache(None)
|
|
|
|
def get_history_path() -> Path:
|
|
|
|
"""Return the saved history path."""
|
|
|
|
return get_user_data_path() / "history.txt"
|
|
|
|
|
|
|
|
|
2021-05-12 22:29:03 +02:00
|
|
|
def ensure_bebop_files_exist() -> Optional[str]:
|
|
|
|
"""Ensure various Bebop's files or directories are present.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
None if all files and directories are present, an error string otherwise.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
# Ensure the user data directory exists.
|
|
|
|
user_data_path = get_user_data_path()
|
|
|
|
if not user_data_path.exists():
|
|
|
|
user_data_path.mkdir(parents=True)
|
|
|
|
# Ensure the identities file and directory exists.
|
|
|
|
identities_file_path = get_identities_list_path()
|
|
|
|
if not identities_file_path.exists():
|
|
|
|
with open(identities_file_path, "wt") as identities_file:
|
|
|
|
identities_file.write("{}")
|
|
|
|
identities_path = get_identities_path()
|
|
|
|
if not identities_path.exists():
|
|
|
|
identities_path.mkdir(parents=True)
|
2021-05-29 01:13:43 +02:00
|
|
|
# Ensure the capsule preferences file exists.
|
|
|
|
capsule_prefs_path = get_capsule_prefs_path()
|
|
|
|
if not capsule_prefs_path.exists():
|
|
|
|
with open(capsule_prefs_path, "wt") as prefs_file:
|
|
|
|
prefs_file.write("{}")
|
2021-05-12 22:29:03 +02:00
|
|
|
except OSError as exc:
|
|
|
|
return str(exc)
|