2024-01-17 11:01:50 +00:00
|
|
|
import configparser
|
2023-04-13 16:52:03 +00:00
|
|
|
from pathlib import Path
|
|
|
|
import os
|
2024-01-17 11:01:50 +00:00
|
|
|
from os.path import expandvars
|
|
|
|
import logging
|
|
|
|
from sys import platform
|
2023-04-13 16:52:03 +00:00
|
|
|
|
|
|
|
import tempfile
|
2024-01-17 11:01:50 +00:00
|
|
|
from typing import Optional
|
|
|
|
|
2023-06-21 09:53:59 +00:00
|
|
|
from pyffmpeg import FFmpeg
|
2023-04-13 16:52:03 +00:00
|
|
|
|
2024-01-17 11:01:50 +00:00
|
|
|
|
2023-04-13 16:52:03 +00:00
|
|
|
from .music_directory import get_music_directory
|
2023-04-13 17:22:54 +00:00
|
|
|
from .config_directory import get_config_directory
|
2023-04-13 16:52:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Locations:
|
2024-01-17 11:01:50 +00:00
|
|
|
@staticmethod
|
|
|
|
def _get_env(key: str, default: Path, default_for_windows: bool = True) -> Optional[Path]:
|
|
|
|
res = os.environ.get(key.upper())
|
|
|
|
if res is not None:
|
|
|
|
return res
|
|
|
|
|
|
|
|
xdg_user_dirs_file = os.environ.get("XDG_CONFIG_HOME") or Path(Path.home(), ".config", "user-dirs.dirs")
|
|
|
|
xdg_user_dirs_default_file = Path("/etc/xdg/user-dirs.defaults")
|
|
|
|
|
|
|
|
def get_dir_from_xdg_file(xdg_file_path: os.PathLike) -> Optional[Path]:
|
|
|
|
nonlocal key
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open(xdg_file_path, 'r') as f:
|
|
|
|
data = "[XDG_USER_DIRS]\n" + f.read()
|
|
|
|
config = configparser.ConfigParser(allow_no_value=True)
|
|
|
|
config.read_string(data)
|
|
|
|
xdg_config = config['XDG_USER_DIRS']
|
|
|
|
|
|
|
|
return Path(expandvars(xdg_config[key.lower()].strip('"')))
|
|
|
|
|
|
|
|
except (FileNotFoundError, KeyError) as e:
|
|
|
|
logging.warning(
|
|
|
|
f"Missing file or No entry found for \"{key}\" in: \"{xdg_file_path}\".\n"
|
|
|
|
)
|
|
|
|
logging.debug(str(e))
|
|
|
|
|
|
|
|
res = get_dir_from_xdg_file(xdg_user_dirs_file)
|
|
|
|
if res is not None:
|
|
|
|
return res
|
|
|
|
|
|
|
|
res = get_dir_from_xdg_file(xdg_user_dirs_default_file)
|
|
|
|
if res is not None:
|
|
|
|
return res
|
|
|
|
|
|
|
|
logging.warning(f"couldn't find a {key}, falling back to: {default}")
|
|
|
|
|
|
|
|
if not default_for_windows and platform == "linux":
|
|
|
|
return
|
|
|
|
|
|
|
|
return default
|
|
|
|
|
2023-04-13 17:22:54 +00:00
|
|
|
def __init__(self, application_name: os.PathLike = "music-kraken"):
|
2023-04-18 07:13:04 +00:00
|
|
|
self.FILE_ENCODING: str = "utf-8"
|
|
|
|
|
2023-04-13 17:22:54 +00:00
|
|
|
self.TEMP_DIRECTORY = Path(tempfile.gettempdir(), application_name)
|
2023-04-18 07:02:03 +00:00
|
|
|
self.TEMP_DIRECTORY.mkdir(exist_ok=True, parents=True)
|
2023-04-13 16:52:03 +00:00
|
|
|
|
|
|
|
self.MUSIC_DIRECTORY = get_music_directory()
|
|
|
|
|
2023-04-13 17:22:54 +00:00
|
|
|
self.CONFIG_DIRECTORY = get_config_directory(str(application_name))
|
2023-04-18 07:02:03 +00:00
|
|
|
self.CONFIG_DIRECTORY.mkdir(exist_ok=True, parents=True)
|
2023-08-28 19:08:42 +00:00
|
|
|
self.CONFIG_FILE = Path(self.CONFIG_DIRECTORY, f"{application_name}.conf")
|
2023-08-10 21:01:16 +00:00
|
|
|
self.LEGACY_CONFIG_FILE = Path(self.CONFIG_DIRECTORY, f"{application_name}.conf")
|
2024-01-17 11:01:50 +00:00
|
|
|
|
|
|
|
self.CACHE_DIRECTORY = self._get_env("XDG_CACHE_HOME", Path(Path.home(), ".cache"))
|
|
|
|
if self.CACHE_DIRECTORY is None:
|
|
|
|
logging.warning(f"Could not find a cache dir. Falling back to the temp dir: {self.TEMP_DIRECTORY}")
|
|
|
|
self.CACHE_DIRECTORY = self.TEMP_DIRECTORY
|
|
|
|
else:
|
|
|
|
self.CACHE_DIRECTORY = Path(self.CACHE_DIRECTORY, application_name)
|
|
|
|
self.CACHE_DIRECTORY.mkdir(parents=True, exist_ok=True)
|
2023-06-21 09:53:59 +00:00
|
|
|
|
|
|
|
self.FFMPEG_BIN = Path(FFmpeg(enable_log=False).get_ffmpeg_bin())
|
2023-04-13 17:22:54 +00:00
|
|
|
|
2023-09-10 14:27:09 +00:00
|
|
|
def get_config_file(self, config_name: str) -> Path:
|
|
|
|
return Path(self.CONFIG_DIRECTORY, f"{config_name}.toml")
|
|
|
|
|
2023-04-13 16:52:03 +00:00
|
|
|
def get_log_file(self, file_name: os.PathLike) -> Path:
|
|
|
|
return Path(self.TEMP_DIRECTORY, file_name)
|