From df2f8f57d269835dbea54b8f53fc4f7d58f3bf0f Mon Sep 17 00:00:00 2001 From: Hellow Date: Fri, 14 Apr 2023 11:22:47 +0200 Subject: [PATCH] started the config with loging --- src/music_kraken/__init__.py | 2 +- src/music_kraken/__main__.py | 14 ++++++--- src/music_kraken/utils/config/__init__.py | 1 + src/music_kraken/utils/config/config.py | 38 +++++++++++++++++++++-- src/music_kraken/utils/config/logging.py | 21 ++++++++++++- src/music_kraken/utils/shared.py | 23 +++++++------- 6 files changed, 79 insertions(+), 20 deletions(-) diff --git a/src/music_kraken/__init__.py b/src/music_kraken/__init__.py index 4e19530..cd53c8c 100644 --- a/src/music_kraken/__init__.py +++ b/src/music_kraken/__init__.py @@ -54,7 +54,7 @@ def print_cute_message(): def exit_message(): print() print_cute_message() - print("Have fun with your music. :3") + print("See you soon! :3") def cli( diff --git a/src/music_kraken/__main__.py b/src/music_kraken/__main__.py index 1306471..2d611ea 100644 --- a/src/music_kraken/__main__.py +++ b/src/music_kraken/__main__.py @@ -55,8 +55,12 @@ if __name__ == "__main__": if arguments.test: genre = "test" - music_kraken.cli( - genre=genre, - download_all=arguments.all, - direct_download_url=arguments.url - ) + try: + music_kraken.cli( + genre=genre, + download_all=arguments.all, + direct_download_url=arguments.url + ) + except KeyboardInterrupt: + print("\n\nRaise an issue if I fucked up:\nhttps://github.com/HeIIow2/music-downloader/issues") + music_kraken.exit_message() diff --git a/src/music_kraken/utils/config/__init__.py b/src/music_kraken/utils/config/__init__.py index e69de29..53afcec 100644 --- a/src/music_kraken/utils/config/__init__.py +++ b/src/music_kraken/utils/config/__init__.py @@ -0,0 +1 @@ +from .logging import LOGGING_SECTION diff --git a/src/music_kraken/utils/config/config.py b/src/music_kraken/utils/config/config.py index 3c424d6..6169d9a 100644 --- a/src/music_kraken/utils/config/config.py +++ b/src/music_kraken/utils/config/config.py @@ -1,6 +1,8 @@ from dataclasses import dataclass from typing import Optional, List, Union +COMMENT_PREFIX = "// " + @dataclass class Attribute: @@ -8,16 +10,25 @@ class Attribute: description: Optional[str] value: Union[str, List[str]] + @property + def description_as_comment(self): + lines = self.description.split("\n") + + return "\n".join(f"{COMMENT_PREFIX}{line}" for line in lines) + @property def object_from_value(self): return self.value + def __str__(self): + return f"{self.description_as_comment}\n{self.name}={self.value}" + class SingleAttribute(Attribute): value: str -class StringAttribute(Attribute): +class StringAttribute(SingleAttribute): @property def object_from_value(self) -> str: return self.value @@ -26,9 +37,32 @@ class StringAttribute(Attribute): class ListAttribute(Attribute): value: List[str] + def __str__(self): + return f"{self.description_as_comment}\n" + \ + "\n".join(f"{self.name}={element}" for element in self.value) + + +@dataclass +class Description: + description: str + + def __str__(self): + return f"\n{self.description}" + + +class EmptyLine(Description): + def __init__(self): + self.description = "\n" + class Section: """ A placeholder class """ - pass + attribute_list: List[Union[ + Attribute, + Description + ]] + + def __str__(self): + return "\n".join(attribute.__str__() for attribute in self.attribute_list) diff --git a/src/music_kraken/utils/config/logging.py b/src/music_kraken/utils/config/logging.py index 53d6874..ac31ba4 100644 --- a/src/music_kraken/utils/config/logging.py +++ b/src/music_kraken/utils/config/logging.py @@ -1,7 +1,7 @@ import logging from typing import Callable -from .config import SingleAttribute, ListAttribute, StringAttribute, Section +from .config import SingleAttribute, StringAttribute, Section, Description, EmptyLine LOG_LEVELS = { "CRITICAL": 50, @@ -99,3 +99,22 @@ class LoggingSection(Section): description="The logger for the genius scraper", value="genius" ) + + self.attribute_list = [ + Description("Logging settings for the actual logging:"), + self.FORMAT, + self.LOG_LEVEL, + Description("Just the names for different logger, for different parts of the programm:"), + self.DOWNLOAD_LOGGER, + self.TAGGING_LOGGER, + self.CODEX_LOGGER, + self.OBJECT_LOGGER, + self.DATABASE_LOGGER, + self.MUSIFY_LOGGER, + self.YOUTUBE_LOGGER, + self.ENCYCLOPAEDIA_METALLUM_LOGGER, + self.GENIUS_LOGGER + ] + + +LOGGING_SECTION = LoggingSection() diff --git a/src/music_kraken/utils/shared.py b/src/music_kraken/utils/shared.py index e296ea5..69b2924 100644 --- a/src/music_kraken/utils/shared.py +++ b/src/music_kraken/utils/shared.py @@ -4,6 +4,7 @@ from pathlib import Path from typing import List, Set, Tuple from .path_manager import LOCATIONS +from .config import LOGGING_SECTION # modifies the garbage collector to speed up the program # https://mkennedy.codes/posts/python-gc-settings-change-this-and-make-your-app-go-20pc-faster/ @@ -44,25 +45,25 @@ MUSIC_DIR: Path = LOCATIONS.MUSIC_DIRECTORY # configure logger default logging.basicConfig( - level=logging.INFO, - format=logging.BASIC_FORMAT, + level=LOGGING_SECTION.LOG_LEVEL.object_from_value, + format=LOGGING_SECTION.FORMAT.object_from_value, handlers=[ logging.FileHandler(LOG_PATH), logging.StreamHandler() ] ) -OBJECT_LOGGER = logging.getLogger("objects") -DATABASE_LOGGER = logging.getLogger("database") +OBJECT_LOGGER = LOGGING_SECTION.OBJECT_LOGGER.object_from_value +DATABASE_LOGGER = LOGGING_SECTION.DATABASE_LOGGER.object_from_value -YOUTUBE_LOGGER = logging.getLogger("Youtube") -MUSIFY_LOGGER = logging.getLogger("Musify") -GENIUS_LOGGER = logging.getLogger("genius") -ENCYCLOPAEDIA_METALLUM_LOGGER = logging.getLogger("ma") +YOUTUBE_LOGGER = LOGGING_SECTION.YOUTUBE_LOGGER.object_from_value +MUSIFY_LOGGER = LOGGING_SECTION.MUSIFY_LOGGER.object_from_value +GENIUS_LOGGER = LOGGING_SECTION.GENIUS_LOGGER +ENCYCLOPAEDIA_METALLUM_LOGGER = LOGGING_SECTION.ENCYCLOPAEDIA_METALLUM_LOGGER.object_from_value -DOWNLOAD_LOGGER = logging.getLogger("download") -TAGGING_LOGGER = logging.getLogger("tagging") -CODEX_LOGGER = logging.getLogger("codex") +DOWNLOAD_LOGGER = LOGGING_SECTION.DOWNLOAD_LOGGER.object_from_value +TAGGING_LOGGER = LOGGING_SECTION.TAGGING_LOGGER.object_from_value +CODEX_LOGGER = LOGGING_SECTION.CODEX_LOGGER.object_from_value NOT_A_GENRE_REGEX: Tuple[str] = ( r'^\.', # is hidden/starts with a "."