From 0ece262e3d65d1568e05fb553af32ef5640ff583 Mon Sep 17 00:00:00 2001 From: Hellow Date: Sat, 15 Apr 2023 17:17:33 +0200 Subject: [PATCH] implemented misc settings --- src/music_kraken/__init__.py | 12 ++--- src/music_kraken/__main__.py | 2 + src/music_kraken/utils/config/__init__.py | 2 + src/music_kraken/utils/config/base_classes.py | 20 ++++++-- src/music_kraken/utils/config/config.py | 15 ++++++ src/music_kraken/utils/config/connection.py | 29 +++++++++-- src/music_kraken/utils/config/misc.py | 48 +++++++++++++++++++ src/music_kraken/utils/shared.py | 44 ++++++++--------- 8 files changed, 135 insertions(+), 37 deletions(-) diff --git a/src/music_kraken/__init__.py b/src/music_kraken/__init__.py index 63a9ba9..c45e74e 100644 --- a/src/music_kraken/__init__.py +++ b/src/music_kraken/__init__.py @@ -1,16 +1,16 @@ -import gc -import musicbrainzngs import logging import re -import os from pathlib import Path from typing import List +import gc +import musicbrainzngs + from . import objects, pages -from .utils.config import config, read, write -from .utils.string_processing import fit_to_file_system -from .utils.shared import MUSIC_DIR, MODIFY_GC, NOT_A_GENRE_REGEX, get_random_message from .utils import exception +from .utils.config import config, read, write +from .utils.shared import MUSIC_DIR, MODIFY_GC, NOT_A_GENRE_REGEX, get_random_message +from .utils.string_processing import fit_to_file_system if MODIFY_GC: """ diff --git a/src/music_kraken/__main__.py b/src/music_kraken/__main__.py index 6f09314..b0a6f83 100644 --- a/src/music_kraken/__main__.py +++ b/src/music_kraken/__main__.py @@ -55,6 +55,8 @@ if __name__ == "__main__": import music_kraken + music_kraken.read() + if arguments.settings: music_kraken.settings() exit() diff --git a/src/music_kraken/utils/config/__init__.py b/src/music_kraken/utils/config/__init__.py index 6be6b0a..59f8f5c 100644 --- a/src/music_kraken/utils/config/__init__.py +++ b/src/music_kraken/utils/config/__init__.py @@ -1,7 +1,9 @@ from .logging import LOGGING_SECTION from .audio import AUDIO_SECTION from .connection import CONNECTION_SECTION +from .misc import MISC_SECTION from .config import read, write, config + read() diff --git a/src/music_kraken/utils/config/base_classes.py b/src/music_kraken/utils/config/base_classes.py index 08342c0..2faa9e4 100644 --- a/src/music_kraken/utils/config/base_classes.py +++ b/src/music_kraken/utils/config/base_classes.py @@ -4,6 +4,9 @@ from typing import Optional, List, Union, Dict from ..exception.config import SettingNotFound, SettingValueError + +LOGGER = logging.getLogger("config") + COMMENT_PREFIX = "#" @@ -83,7 +86,7 @@ class IntAttribute(SingleAttribute): @property def object_from_value(self) -> int: - if not self.value.isdigit(): + if self.value.isdigit(): return int(self.value) @@ -123,6 +126,9 @@ class ListAttribute(Attribute): has_default_values: bool = True + def __len__(self): + return len(self.value) + def set_value(self, value: str): """ Due to lists being represented as multiple lines with the same key, @@ -137,6 +143,7 @@ class ListAttribute(Attribute): # resetting the list to an empty list, if this is the first config line to load if self.has_default_values: self.value = [] + self.has_default_values = False self.value.append(value) @@ -150,8 +157,8 @@ class ListAttribute(Attribute): @property def object_from_value(self) -> list: """ - THIS IS NOT THE PROPERTY TO OVERRIDE WHEN INHERETING ListAttribute - + THIS IS NOT THE PROPERTY TO OVERRIDE WHEN INHERITING ListAttribute + single_object_from_element :return: """ @@ -216,3 +223,10 @@ class Section: ) self.name_attribute_map[setting_name].set_value(new_value) + + def reset_list_attribute(self): + for attribute in self.attribute_list: + if not isinstance(attribute, ListAttribute): + continue + + attribute.has_default_values = True diff --git a/src/music_kraken/utils/config/config.py b/src/music_kraken/utils/config/config.py index 67b083f..2ebb965 100644 --- a/src/music_kraken/utils/config/config.py +++ b/src/music_kraken/utils/config/config.py @@ -8,6 +8,10 @@ from .base_classes import Description, Attribute, Section, EmptyLine, COMMENT_PR from .audio import AUDIO_SECTION from .logging import LOGGING_SECTION from .connection import CONNECTION_SECTION +from .misc import MISC_SECTION + + +LOGGER = logging.getLogger("config") class Config: @@ -26,6 +30,8 @@ class Config: "If you found a bug, and wan't to report it, please set the Logging level to 0,\n" "reproduce the bug, and attach the logfile in the bugreport. ^w^"), LOGGING_SECTION, + Description("If there are stupid settings, they are here."), + MISC_SECTION, Description("🏳️‍⚧️🏳️‍⚧️ Protect trans youth. 🏳️‍⚧️🏳️‍⚧️\n"), ) @@ -57,6 +63,8 @@ class Config: if name not in self._name_section_map: raise SettingNotFound(setting_name=name) + print(f"setting: {name} value: {value}") + self._name_section_map[name].modify_setting(setting_name=name, new_value=value) def __len__(self): @@ -81,6 +89,10 @@ class Config: return if "=" not in line: + """ + TODO + No value error but custom conf error + """ raise ValueError(f"Couldn't find the '=' in line {index}.") line_segments = line.split("=") @@ -91,6 +103,9 @@ class Config: def read_from_config_file(self, path: os.PathLike): with open(path, "r") as conf_file: + for section in self._section_list: + section.reset_list_attribute() + for i, line in enumerate(conf_file): self._parse_conf_line(line, i+1) diff --git a/src/music_kraken/utils/config/connection.py b/src/music_kraken/utils/config/connection.py index 3067d6e..086d927 100644 --- a/src/music_kraken/utils/config/connection.py +++ b/src/music_kraken/utils/config/connection.py @@ -1,16 +1,36 @@ -import logging -from typing import Callable +from .base_classes import Section, FloatAttribute, IntAttribute, BoolAttribute, ListAttribute -from .base_classes import SingleAttribute, StringAttribute, Section, FloatAttribute, Description, IntAttribute, EmptyLine, BoolAttribute + +class ProxAttribute(ListAttribute): + def single_object_from_element(self, value) -> dict: + return { + 'http': value, + 'https': value, + 'ftp': value + } class ConnectionSection(Section): def __init__(self): + self.PROXIES = ProxAttribute( + name="proxies", + description="Set your proxies.\n" + "Must be valid for http, as well as https.", + value=[] + ) + self.USE_TOR = BoolAttribute( name="tor", - description="Route ALL traffic through Tor.\nNo guarantee though!", + description="Route ALL traffic through Tor.\n" + "If you use Tor, make sure the Tor browser is installed, and running." + "I can't guarantee maximum security though!", value="false" ) + self.TOR_PORT = IntAttribute( + name="tor_port", + description="The port, tor is listening. If tor is already working, don't change it.", + value="9150" + ) self.CHUNK_SIZE = IntAttribute( name="chunk_size", description="Size of the chunks that are streamed.", @@ -25,6 +45,7 @@ class ConnectionSection(Section): self.attribute_list = [ self.USE_TOR, + self.TOR_PORT, self.CHUNK_SIZE, self.SHOW_DOWNLOAD_ERRORS_THRESHOLD ] diff --git a/src/music_kraken/utils/config/misc.py b/src/music_kraken/utils/config/misc.py index e69de29..469c0af 100644 --- a/src/music_kraken/utils/config/misc.py +++ b/src/music_kraken/utils/config/misc.py @@ -0,0 +1,48 @@ +from .base_classes import Section, IntAttribute, ListAttribute, BoolAttribute + + +class MiscSection(Section): + def __init__(self): + self.HAPPY_MESSAGES = ListAttribute( + name="happy_messages", + description="Just some nice and wholesome messages.\n" + "If your mindset has traits of a [file corruption], you might not agree.\n" + "But anyways... Freedom of thought, so go ahead and change the messages.", + value=[ + "Support the artist.", + "Star Me: https://github.com/HeIIow2/music-downloader", + "🏳️‍⚧️🏳️‍⚧️ Trans rights are human rights. 🏳️‍⚧️🏳️‍⚧️", + "🏳️‍⚧️🏳️‍⚧️ Trans women are women, trans men are men. 🏳️‍⚧️🏳️‍⚧️", + "🏴‍☠️🏴‍☠️ Unite under one flag, fuck borders. 🏴‍☠️🏴‍☠️", + "Join my Matrix Space: https://matrix.to/#/#music-kraken:matrix.org", + "Gotta love the BPJM!! >:(", + "🏳️‍⚧️🏳️‍⚧️ Protect trans youth. 🏳️‍⚧️🏳️‍⚧️" + ] + ) + + self.MODIFY_GC = BoolAttribute( + name="modify_gc", + description="If set to true, it will modify the gc for the sake of performance.\n" + "This should not drive up ram usage, but if it is, then turn it of.\n" + "Here a blog post about that matter:\n" + "https://mkennedy.codes/posts/python-gc-settings-change-this-and-make-your-app-go-20pc-faster/\n" + "https://web.archive.org/web/20221124122222/https://mkennedy.codes/posts/python-gc-settings-change-this-and-make-your-app-go-20pc-faster/", + value="true" + ) + + self.ID_BITS = IntAttribute( + name="id_bits", + description="I really dunno why I even made this a setting.. Modifying this is a REALLY dumb idea.", + value="64" + ) + + self.attribute_list = [ + self.HAPPY_MESSAGES, + self.MODIFY_GC, + self.ID_BITS + ] + + super().__init__() + + +MISC_SECTION = MiscSection() diff --git a/src/music_kraken/utils/shared.py b/src/music_kraken/utils/shared.py index 3163556..ce4b929 100644 --- a/src/music_kraken/utils/shared.py +++ b/src/music_kraken/utils/shared.py @@ -1,18 +1,18 @@ import logging import random from pathlib import Path -from typing import List, Set, Tuple +from typing import List, Tuple from .path_manager import LOCATIONS -from .config import LOGGING_SECTION, AUDIO_SECTION, CONNECTION_SECTION +from .config import LOGGING_SECTION, AUDIO_SECTION, CONNECTION_SECTION, MISC_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/ # https://web.archive.org/web/20221124122222/https://mkennedy.codes/posts/python-gc-settings-change-this-and-make-your-app-go-20pc-faster/ -MODIFY_GC: bool = True +MODIFY_GC: bool = MISC_SECTION.MODIFY_GC.object_from_value -ID_BITS: int = 64 -ID_RANGE: Tuple[int, int] = (0, int(2**ID_BITS)) +ID_BITS: int = MISC_SECTION.ID_BITS.object_from_value +ID_RANGE: Tuple[int, int] = (0, int(2 ** ID_BITS)) """ I will now and then use those messages in the programm. @@ -20,18 +20,9 @@ But I won't overuse them dw. I will keep those messages, if you disagree with me on the messages, feel free to fork the programm and edit them, or just edit them in the config -file once I implemented it. +file once I implemented it. (I did it is in ~/.config/music-kraken/music-kraken.conf) """ -HAPPY_MESSAGES: List[str] = [ - "Support the artist.", - "Star Me: https://github.com/HeIIow2/music-downloader", - "🏳️‍⚧️🏳️‍⚧️ Trans rights are human rights. 🏳️‍⚧️🏳️‍⚧️", - "🏳️‍⚧️🏳️‍⚧️ Trans women are women, trans men are men. 🏳️‍⚧️🏳️‍⚧️", - "🏴‍☠️🏴‍☠️ Unite under one flag, fuck borders. 🏴‍☠️🏴‍☠️", - "Join my Matrix Space: https://matrix.to/#/#music-kraken:matrix.org", - "Gotta love the BPJM!! >:(", - "🏳️‍⚧️🏳️‍⚧️ Protect trans youth. 🏳️‍⚧️🏳️‍⚧️" -] +HAPPY_MESSAGES: List[str] = MISC_SECTION.HAPPY_MESSAGES.object_from_value def get_random_message() -> str: @@ -43,10 +34,9 @@ LOG_PATH = LOCATIONS.get_log_file("download_logs.log") MUSIC_DIR: Path = LOCATIONS.MUSIC_DIRECTORY NOT_A_GENRE_REGEX: Tuple[str] = ( - r'^\.', # is hidden/starts with a "." + r'^\.', # is hidden/starts with a "." ) - # configure logger default logging.basicConfig( level=LOGGING_SECTION.LOG_LEVEL.object_from_value, @@ -73,7 +63,6 @@ CODEX_LOGGER = LOGGING_SECTION.CODEX_LOGGER.object_from_value BITRATE = AUDIO_SECTION.BITRATE.object_from_value AUDIO_FORMAT = AUDIO_SECTION.AUDIO_FORMAT.object_from_value - DOWNLOAD_PATH = AUDIO_SECTION.DOWNLOAD_PATH.object_from_value DOWNLOAD_FILE = AUDIO_SECTION.DOWNLOAD_FILE.object_from_value DEFAULT_VALUES = { @@ -86,12 +75,19 @@ DEFAULT_VALUES = { "audio_format": AUDIO_FORMAT } - TOR: bool = CONNECTION_SECTION.USE_TOR.object_from_value -proxies = { - 'http': 'socks5h://127.0.0.1:9150', - 'https': 'socks5h://127.0.0.1:9150' -} if TOR else {} +proxies = {} +if len(CONNECTION_SECTION.PROXIES) > 0: + """ + TODO + rotating proxies + """ + proxies = CONNECTION_SECTION.PROXIES.object_from_value[0] +if TOR: + proxies = { + 'http': f'socks5h://127.0.0.1:{CONNECTION_SECTION.TOR_PORT.object_from_value}', + 'https': f'socks5h://127.0.0.1:{CONNECTION_SECTION.TOR_PORT.object_from_value}' + } # size of the chunks that are streamed CHUNK_SIZE = CONNECTION_SECTION.CHUNK_SIZE.object_from_value