From 2e5a46c3f6713db60d7b91a84cf92d6f01dc2066 Mon Sep 17 00:00:00 2001 From: Lars Noack Date: Tue, 9 Apr 2024 12:55:35 +0200 Subject: [PATCH] feat: cleaned up --- music_kraken/pages/bandcamp.py | 2 +- music_kraken/pages/encyclopaedia_metallum.py | 3 +- .../pages/youtube_music/youtube_music.py | 3 +- music_kraken/utils/__init__.py | 35 +++++++++++++++++-- music_kraken/utils/debug_utils.py | 21 ----------- 5 files changed, 36 insertions(+), 28 deletions(-) delete mode 100644 music_kraken/utils/debug_utils.py diff --git a/music_kraken/pages/bandcamp.py b/music_kraken/pages/bandcamp.py index 6f65d6c..f9a3a45 100644 --- a/music_kraken/pages/bandcamp.py +++ b/music_kraken/pages/bandcamp.py @@ -26,7 +26,7 @@ from ..utils.config import main_settings, logging_settings from ..utils.shared import DEBUG if DEBUG: - from ..utils.debug_utils import dump_to_file + from ..utils import dump_to_file def _parse_artist_url(url: str) -> str: diff --git a/music_kraken/pages/encyclopaedia_metallum.py b/music_kraken/pages/encyclopaedia_metallum.py index 38a9b24..d9ce0ca 100644 --- a/music_kraken/pages/encyclopaedia_metallum.py +++ b/music_kraken/pages/encyclopaedia_metallum.py @@ -23,8 +23,7 @@ from ..objects import ( DatabaseObject ) from ..utils.shared import DEBUG -if DEBUG: - from ..utils.debug_utils import dump_to_file +from ..utils import dump_to_file diff --git a/music_kraken/pages/youtube_music/youtube_music.py b/music_kraken/pages/youtube_music/youtube_music.py index 8ba1a5f..a946a59 100644 --- a/music_kraken/pages/youtube_music/youtube_music.py +++ b/music_kraken/pages/youtube_music/youtube_music.py @@ -17,8 +17,7 @@ from ...utils.config import main_settings, youtube_settings, logging_settings from ...utils.shared import DEBUG, DEBUG_YOUTUBE_INITIALIZING from ...utils import get_current_millis -if DEBUG: - from ...utils.debug_utils import dump_to_file +from ...utils import dump_to_file from ...objects import Source, DatabaseObject from ..abstract import Page diff --git a/music_kraken/utils/__init__.py b/music_kraken/utils/__init__.py index 3f6b07e..9d2eb5b 100644 --- a/music_kraken/utils/__init__.py +++ b/music_kraken/utils/__init__.py @@ -1,13 +1,19 @@ from datetime import datetime +from pathlib import Path +import json +import logging +from .shared import DEBUG, DEBUG_LOGGING, DEBUG_PAGES from .config import config, read_config, write_config from .enums.colors import BColors +from .path_manager import LOCATIONS + +DEBUG = False """ -Here are all global important functions. +IO functions """ - def _apply_color(msg: str, color: BColors) -> str: if color is BColors.ENDC: return msg @@ -22,6 +28,31 @@ def user_input(msg: str, color: BColors = BColors.ENDC): return input(_apply_color(msg, color)).strip() +def dump_to_file(file_name: str, payload: str, is_json: bool = False, exit_after_dump: bool = False): + if not DEBUG_PAGES: + return + + path = Path(LOCATIONS.TEMP_DIRECTORY, file_name) + logging.warning(f"dumping {file_name} to: \"{path}\"") + + if is_json: + payload = json.dumps(json.loads(payload), indent=4) + + if isinstance(payload, dict): + payload = json.dumps(payload, indent=4) + + with path.open("w") as f: + f.write(payload) + + if exit_after_dump: + exit() + + + +""" +misc functions +""" + def get_current_millis() -> int: dt = datetime.now() return int(dt.microsecond / 1_000) diff --git a/music_kraken/utils/debug_utils.py b/music_kraken/utils/debug_utils.py deleted file mode 100644 index 457081b..0000000 --- a/music_kraken/utils/debug_utils.py +++ /dev/null @@ -1,21 +0,0 @@ -from pathlib import Path -import json - -from .path_manager import LOCATIONS - - -def dump_to_file(file_name: str, payload: str, is_json: bool = False, exit_after_dump: bool = True): - path = Path(LOCATIONS.TEMP_DIRECTORY, file_name) - print(f"Dumping payload to: \"{path}\"") - - if is_json: - payload = json.dumps(json.loads(payload), indent=4) - - if isinstance(payload, dict): - payload = json.dumps(payload, indent=4) - - with path.open("w") as f: - f.write(payload) - - if exit_after_dump: - exit()