From 8405692d6b23c68b064d415615743fb4b3091c8e Mon Sep 17 00:00:00 2001 From: darkeox Date: Thu, 24 Nov 2022 23:22:06 +0100 Subject: [PATCH 1/5] introduce localization for Music dir on Linux --- src/music_kraken/utils/shared.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/music_kraken/utils/shared.py b/src/music_kraken/utils/shared.py index 2db3ae6..84acec4 100644 --- a/src/music_kraken/utils/shared.py +++ b/src/music_kraken/utils/shared.py @@ -2,8 +2,11 @@ import musicbrainzngs import logging import tempfile import os +import io +import configparser +from sys import platform as current_os - +USER_XDG_DIR_FILE = os.path.expanduser("~/.config/user-dirs.dirs") TEMP_FOLDER = "music-downloader" LOG_FILE = "download_logs.log" TEMP_DATABASE_FILE = "metadata.db" @@ -39,6 +42,21 @@ GENIUS_LOGGER = logging.getLogger("genius") NOT_A_GENRE = ".", "..", "misc_scripts", "Music", "script", ".git", ".idea" MUSIC_DIR = os.path.join(os.path.expanduser("~"), "Music") +if current_os == "linux": + try: + with open(USER_XDG_DIR_FILE, 'r') as f: + data = io.StringIO("[XDG_USER_DIRS]\n" + f.read()) + config = configparser.ConfigParser(allow_no_value=True) + config.read_file(data) + xdg_config = config['XDG_USER_DIRS'] + MUSIC_DIR = os.path.expandvars(xdg_config['xdg_music_dir'].strip('"')) + except FileNotFoundError as N: + print(f''' +Missing XDG_USER_DIRS file at: '{USER_XDG_DIR_FILE}'. +Will fallback on default '$HOME/Music'. +---- + ''') + TOR = False proxies = { 'http': 'socks5h://127.0.0.1:9150', From 7f6e6ee5fadab18debc42e7f70e5b1173a591a04 Mon Sep 17 00:00:00 2001 From: darkeox Date: Mon, 28 Nov 2022 14:49:13 +0100 Subject: [PATCH 2/5] refactor #1 - logger print + music_dir --- src/music_kraken/utils/shared.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/music_kraken/utils/shared.py b/src/music_kraken/utils/shared.py index 84acec4..bcfc188 100644 --- a/src/music_kraken/utils/shared.py +++ b/src/music_kraken/utils/shared.py @@ -6,7 +6,7 @@ import io import configparser from sys import platform as current_os -USER_XDG_DIR_FILE = os.path.expanduser("~/.config/user-dirs.dirs") + TEMP_FOLDER = "music-downloader" LOG_FILE = "download_logs.log" TEMP_DATABASE_FILE = "metadata.db" @@ -29,6 +29,7 @@ logging.basicConfig( ) SEARCH_LOGGER = logging.getLogger("mb-cli") +INIT_PATH_LOGGER = logging.getLogger("init_path") DATABASE_LOGGER = logging.getLogger("database") METADATA_DOWNLOAD_LOGGER = logging.getLogger("metadata") URL_DOWNLOAD_LOGGER = logging.getLogger("AudioSource") @@ -40,18 +41,21 @@ LYRICS_LOGGER = logging.getLogger("lyrics") GENIUS_LOGGER = logging.getLogger("genius") NOT_A_GENRE = ".", "..", "misc_scripts", "Music", "script", ".git", ".idea" -MUSIC_DIR = os.path.join(os.path.expanduser("~"), "Music") +MUSIC_DIR = os.path.join("~", ".config", "user-dirs.dirs") if current_os == "linux": + USER_XDG_DIR_FILE = os.path.expanduser("~/.config/user-dirs.dirss") + logger = logging.getLogger("init_path") + logger.setLevel(logging.WARNING) try: with open(USER_XDG_DIR_FILE, 'r') as f: data = io.StringIO("[XDG_USER_DIRS]\n" + f.read()) - config = configparser.ConfigParser(allow_no_value=True) - config.read_file(data) - xdg_config = config['XDG_USER_DIRS'] - MUSIC_DIR = os.path.expandvars(xdg_config['xdg_music_dir'].strip('"')) + config = configparser.ConfigParser(allow_no_value=True) + config.read_file(data) + xdg_config = config['XDG_USER_DIRS'] + MUSIC_DIR = os.path.expandvars(xdg_config['xdg_music_dir'].strip('"')) except FileNotFoundError as N: - print(f''' + logger.warning(f''' Missing XDG_USER_DIRS file at: '{USER_XDG_DIR_FILE}'. Will fallback on default '$HOME/Music'. ---- From 0dc255594fbf3c4c5049d55c4ca69cba2f3be254 Mon Sep 17 00:00:00 2001 From: darkeox Date: Mon, 28 Nov 2022 15:00:08 +0100 Subject: [PATCH 3/5] Catch key exception + correct typo + file i/o method --- src/music_kraken/utils/shared.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/music_kraken/utils/shared.py b/src/music_kraken/utils/shared.py index bcfc188..1b26fcc 100644 --- a/src/music_kraken/utils/shared.py +++ b/src/music_kraken/utils/shared.py @@ -2,7 +2,6 @@ import musicbrainzngs import logging import tempfile import os -import io import configparser from sys import platform as current_os @@ -44,19 +43,19 @@ NOT_A_GENRE = ".", "..", "misc_scripts", "Music", "script", ".git", ".idea" MUSIC_DIR = os.path.join("~", ".config", "user-dirs.dirs") if current_os == "linux": - USER_XDG_DIR_FILE = os.path.expanduser("~/.config/user-dirs.dirss") + USER_XDG_DIR_FILE = os.path.expanduser("~/.config/user-dirs.dirs") logger = logging.getLogger("init_path") logger.setLevel(logging.WARNING) try: with open(USER_XDG_DIR_FILE, 'r') as f: - data = io.StringIO("[XDG_USER_DIRS]\n" + f.read()) + data = "[XDG_USER_DIRS]\n" + f.read() config = configparser.ConfigParser(allow_no_value=True) - config.read_file(data) + config.read_string(data) xdg_config = config['XDG_USER_DIRS'] MUSIC_DIR = os.path.expandvars(xdg_config['xdg_music_dir'].strip('"')) - except FileNotFoundError as N: + except (FileNotFoundError, KeyError) as E: logger.warning(f''' -Missing XDG_USER_DIRS file at: '{USER_XDG_DIR_FILE}'. +Missing file or key at: '{USER_XDG_DIR_FILE}'. Will fallback on default '$HOME/Music'. ---- ''') From ebae24e8658c164d29630f8817af2ad766793eb0 Mon Sep 17 00:00:00 2001 From: darkeox Date: Mon, 28 Nov 2022 15:16:04 +0100 Subject: [PATCH 4/5] XDG_USER_DIRS_FILE refactor + doc --- src/music_kraken/utils/shared.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/music_kraken/utils/shared.py b/src/music_kraken/utils/shared.py index 1b26fcc..9d31f4c 100644 --- a/src/music_kraken/utils/shared.py +++ b/src/music_kraken/utils/shared.py @@ -43,11 +43,12 @@ NOT_A_GENRE = ".", "..", "misc_scripts", "Music", "script", ".git", ".idea" MUSIC_DIR = os.path.join("~", ".config", "user-dirs.dirs") if current_os == "linux": - USER_XDG_DIR_FILE = os.path.expanduser("~/.config/user-dirs.dirs") + # XDG_USER_DIRS_FILE reference: https://freedesktop.org/wiki/Software/xdg-user-dirs/ + XDG_USER_DIRS_FILE = os.path.expanduser("~/.config/user-dirs.dirs") logger = logging.getLogger("init_path") logger.setLevel(logging.WARNING) try: - with open(USER_XDG_DIR_FILE, 'r') as f: + 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) @@ -55,7 +56,7 @@ if current_os == "linux": MUSIC_DIR = os.path.expandvars(xdg_config['xdg_music_dir'].strip('"')) except (FileNotFoundError, KeyError) as E: logger.warning(f''' -Missing file or key at: '{USER_XDG_DIR_FILE}'. +Missing file or key at: '{XDG_USER_DIRS_FILE}'. Will fallback on default '$HOME/Music'. ---- ''') From 97bf0b4eb4e4bd8f9126893ae6e2f0fc280de5d4 Mon Sep 17 00:00:00 2001 From: darkeox Date: Mon, 28 Nov 2022 17:56:19 +0100 Subject: [PATCH 5/5] Catch key exception + correct typo + fix path interpolation --- src/music_kraken/utils/shared.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/music_kraken/utils/shared.py b/src/music_kraken/utils/shared.py index 9d31f4c..b18d81a 100644 --- a/src/music_kraken/utils/shared.py +++ b/src/music_kraken/utils/shared.py @@ -40,11 +40,11 @@ LYRICS_LOGGER = logging.getLogger("lyrics") GENIUS_LOGGER = logging.getLogger("genius") NOT_A_GENRE = ".", "..", "misc_scripts", "Music", "script", ".git", ".idea" -MUSIC_DIR = os.path.join("~", ".config", "user-dirs.dirs") +MUSIC_DIR = os.path.join(os.path.expanduser("~"), "Music") if current_os == "linux": # XDG_USER_DIRS_FILE reference: https://freedesktop.org/wiki/Software/xdg-user-dirs/ - XDG_USER_DIRS_FILE = os.path.expanduser("~/.config/user-dirs.dirs") + XDG_USER_DIRS_FILE = os.path.join(os.path.expanduser("~"), ".config", "user-dirs.dirs") logger = logging.getLogger("init_path") logger.setLevel(logging.WARNING) try: @@ -56,11 +56,10 @@ if current_os == "linux": MUSIC_DIR = os.path.expandvars(xdg_config['xdg_music_dir'].strip('"')) except (FileNotFoundError, KeyError) as E: logger.warning(f''' -Missing file or key at: '{XDG_USER_DIRS_FILE}'. +Missing file or No entry found for "xdg_music_dir" in: \'{XDG_USER_DIRS_FILE}\'. Will fallback on default '$HOME/Music'. ---- - ''') - + ''') TOR = False proxies = { 'http': 'socks5h://127.0.0.1:9150',