From ba3deb24e3dbffcd9891da807bc18f5beef0dda5 Mon Sep 17 00:00:00 2001 From: Hellow Date: Thu, 13 Apr 2023 18:52:03 +0200 Subject: [PATCH] layed out config structure and started implementing the path manager with the temp path and music path. --- src/music_kraken/utils/config/__init__.py | 0 .../utils/path_manager/__init__.py | 3 ++ .../utils/path_manager/locations.py | 17 +++++++++ .../utils/path_manager/music_directory.py | 37 +++++++++++++++++++ src/music_kraken/utils/shared.py | 7 ++-- 5 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 src/music_kraken/utils/config/__init__.py create mode 100644 src/music_kraken/utils/path_manager/__init__.py create mode 100644 src/music_kraken/utils/path_manager/locations.py create mode 100644 src/music_kraken/utils/path_manager/music_directory.py diff --git a/src/music_kraken/utils/config/__init__.py b/src/music_kraken/utils/config/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/music_kraken/utils/path_manager/__init__.py b/src/music_kraken/utils/path_manager/__init__.py new file mode 100644 index 0000000..f169e52 --- /dev/null +++ b/src/music_kraken/utils/path_manager/__init__.py @@ -0,0 +1,3 @@ +from .locations import Locations + +LOCATIONS = Locations() diff --git a/src/music_kraken/utils/path_manager/locations.py b/src/music_kraken/utils/path_manager/locations.py new file mode 100644 index 0000000..7afaa42 --- /dev/null +++ b/src/music_kraken/utils/path_manager/locations.py @@ -0,0 +1,17 @@ +from pathlib import Path +import os + +import tempfile + +from .music_directory import get_music_directory + + +class Locations: + def __init__(self, temp_folder_name: os.PathLike = "music-downloader"): + self.TEMP_DIRECTORY = Path(tempfile.gettempdir(), temp_folder_name) + self.TEMP_DIRECTORY.mkdir(exist_ok=True) + + self.MUSIC_DIRECTORY = get_music_directory() + + def get_log_file(self, file_name: os.PathLike) -> Path: + return Path(self.TEMP_DIRECTORY, file_name) diff --git a/src/music_kraken/utils/path_manager/music_directory.py b/src/music_kraken/utils/path_manager/music_directory.py new file mode 100644 index 0000000..8680830 --- /dev/null +++ b/src/music_kraken/utils/path_manager/music_directory.py @@ -0,0 +1,37 @@ +from pathlib import Path +from sys import platform +import logging +from os.path import expandvars + +import configparser + +DEFAULT_MUSIC_DIRECTORY = Path(Path.home(), "Music") + + +def get_xdg_music_directory() -> Path: + # XDG_USER_DIRS_FILE reference: https://freedesktop.org/wiki/Software/xdg-user-dirs/ + xdg_user_dirs_file = Path(Path.home(), ".config", "user-dirs.dirs") + + try: + with open(xdg_user_dirs_file, '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['xdg_music_dir'].strip('"'))) + + except (FileNotFoundError, KeyError) as e: + logging.warning( + f"Missing file or No entry found for \"xdg_music_dir\" in: \"{xdg_user_dirs_file}\".\n" + f"Will fallback on default \"$HOME/Music\"." + ) + logging.debug(str(e)) + + return DEFAULT_MUSIC_DIRECTORY + + +def get_music_directory() -> Path: + if platform != "linux": + return DEFAULT_MUSIC_DIRECTORY + + return get_xdg_music_directory() diff --git a/src/music_kraken/utils/shared.py b/src/music_kraken/utils/shared.py index c1a117e..78ea6d7 100644 --- a/src/music_kraken/utils/shared.py +++ b/src/music_kraken/utils/shared.py @@ -37,12 +37,14 @@ def get_random_message() -> str: ID_BITS: int = 64 -ID_RANGE: Tuple[int] = 0, 2**ID_BITS +ID_RANGE: Tuple[int, int] = (0, int(2**ID_BITS)) TEMP_DIR = Path(tempfile.gettempdir(), "music-downloader") TEMP_DIR.mkdir(exist_ok=True) LOG_PATH = Path(TEMP_DIR, "download_logs.log") +MUSIC_DIR: Path = Path(os.path.expanduser("~"), "Music") + # configure logger default logging.basicConfig( @@ -67,8 +69,7 @@ DOWNLOAD_LOGGER = logging.getLogger("download") TAGGING_LOGGER = logging.getLogger("tagging") CODEX_LOGGER = logging.getLogger("codex") -MUSIC_DIR: Path = Path(os.path.expanduser("~"), "Music") -NOT_A_GENRE_REGEX: List[str] = ( +NOT_A_GENRE_REGEX: Tuple[str] = ( r'^\.', # is hidden/starts with a "." )