implemented misc settings

This commit is contained in:
Hellow 2023-04-15 17:17:33 +02:00
parent 4f12dcbc78
commit 0ece262e3d
8 changed files with 135 additions and 37 deletions

View File

@ -1,16 +1,16 @@
import gc
import musicbrainzngs
import logging import logging
import re import re
import os
from pathlib import Path from pathlib import Path
from typing import List from typing import List
import gc
import musicbrainzngs
from . import objects, pages 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 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: if MODIFY_GC:
""" """

View File

@ -55,6 +55,8 @@ if __name__ == "__main__":
import music_kraken import music_kraken
music_kraken.read()
if arguments.settings: if arguments.settings:
music_kraken.settings() music_kraken.settings()
exit() exit()

View File

@ -1,7 +1,9 @@
from .logging import LOGGING_SECTION from .logging import LOGGING_SECTION
from .audio import AUDIO_SECTION from .audio import AUDIO_SECTION
from .connection import CONNECTION_SECTION from .connection import CONNECTION_SECTION
from .misc import MISC_SECTION
from .config import read, write, config from .config import read, write, config
read() read()

View File

@ -4,6 +4,9 @@ from typing import Optional, List, Union, Dict
from ..exception.config import SettingNotFound, SettingValueError from ..exception.config import SettingNotFound, SettingValueError
LOGGER = logging.getLogger("config")
COMMENT_PREFIX = "#" COMMENT_PREFIX = "#"
@ -83,7 +86,7 @@ class IntAttribute(SingleAttribute):
@property @property
def object_from_value(self) -> int: def object_from_value(self) -> int:
if not self.value.isdigit(): if self.value.isdigit():
return int(self.value) return int(self.value)
@ -123,6 +126,9 @@ class ListAttribute(Attribute):
has_default_values: bool = True has_default_values: bool = True
def __len__(self):
return len(self.value)
def set_value(self, value: str): def set_value(self, value: str):
""" """
Due to lists being represented as multiple lines with the same key, 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 # resetting the list to an empty list, if this is the first config line to load
if self.has_default_values: if self.has_default_values:
self.value = [] self.value = []
self.has_default_values = False
self.value.append(value) self.value.append(value)
@ -150,8 +157,8 @@ class ListAttribute(Attribute):
@property @property
def object_from_value(self) -> list: 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: :return:
""" """
@ -216,3 +223,10 @@ class Section:
) )
self.name_attribute_map[setting_name].set_value(new_value) 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

View File

@ -8,6 +8,10 @@ from .base_classes import Description, Attribute, Section, EmptyLine, COMMENT_PR
from .audio import AUDIO_SECTION from .audio import AUDIO_SECTION
from .logging import LOGGING_SECTION from .logging import LOGGING_SECTION
from .connection import CONNECTION_SECTION from .connection import CONNECTION_SECTION
from .misc import MISC_SECTION
LOGGER = logging.getLogger("config")
class 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" "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^"), "reproduce the bug, and attach the logfile in the bugreport. ^w^"),
LOGGING_SECTION, LOGGING_SECTION,
Description("If there are stupid settings, they are here."),
MISC_SECTION,
Description("🏳️‍⚧️🏳️‍⚧️ Protect trans youth. 🏳️‍⚧️🏳️‍⚧️\n"), Description("🏳️‍⚧️🏳️‍⚧️ Protect trans youth. 🏳️‍⚧️🏳️‍⚧️\n"),
) )
@ -57,6 +63,8 @@ class Config:
if name not in self._name_section_map: if name not in self._name_section_map:
raise SettingNotFound(setting_name=name) raise SettingNotFound(setting_name=name)
print(f"setting: {name} value: {value}")
self._name_section_map[name].modify_setting(setting_name=name, new_value=value) self._name_section_map[name].modify_setting(setting_name=name, new_value=value)
def __len__(self): def __len__(self):
@ -81,6 +89,10 @@ class Config:
return return
if "=" not in line: if "=" not in line:
"""
TODO
No value error but custom conf error
"""
raise ValueError(f"Couldn't find the '=' in line {index}.") raise ValueError(f"Couldn't find the '=' in line {index}.")
line_segments = line.split("=") line_segments = line.split("=")
@ -91,6 +103,9 @@ class Config:
def read_from_config_file(self, path: os.PathLike): def read_from_config_file(self, path: os.PathLike):
with open(path, "r") as conf_file: with open(path, "r") as conf_file:
for section in self._section_list:
section.reset_list_attribute()
for i, line in enumerate(conf_file): for i, line in enumerate(conf_file):
self._parse_conf_line(line, i+1) self._parse_conf_line(line, i+1)

View File

@ -1,16 +1,36 @@
import logging from .base_classes import Section, FloatAttribute, IntAttribute, BoolAttribute, ListAttribute
from typing import Callable
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): class ConnectionSection(Section):
def __init__(self): 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( self.USE_TOR = BoolAttribute(
name="tor", 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" 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( self.CHUNK_SIZE = IntAttribute(
name="chunk_size", name="chunk_size",
description="Size of the chunks that are streamed.", description="Size of the chunks that are streamed.",
@ -25,6 +45,7 @@ class ConnectionSection(Section):
self.attribute_list = [ self.attribute_list = [
self.USE_TOR, self.USE_TOR,
self.TOR_PORT,
self.CHUNK_SIZE, self.CHUNK_SIZE,
self.SHOW_DOWNLOAD_ERRORS_THRESHOLD self.SHOW_DOWNLOAD_ERRORS_THRESHOLD
] ]

View File

@ -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()

View File

@ -1,18 +1,18 @@
import logging import logging
import random import random
from pathlib import Path from pathlib import Path
from typing import List, Set, Tuple from typing import List, Tuple
from .path_manager import LOCATIONS 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 # 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://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/ # 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_BITS: int = MISC_SECTION.ID_BITS.object_from_value
ID_RANGE: Tuple[int, int] = (0, int(2**ID_BITS)) ID_RANGE: Tuple[int, int] = (0, int(2 ** ID_BITS))
""" """
I will now and then use those messages in the programm. 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, 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 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] = [ HAPPY_MESSAGES: List[str] = MISC_SECTION.HAPPY_MESSAGES.object_from_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. 🏳️‍⚧️🏳️‍⚧️"
]
def get_random_message() -> str: def get_random_message() -> str:
@ -46,7 +37,6 @@ NOT_A_GENRE_REGEX: Tuple[str] = (
r'^\.', # is hidden/starts with a "." r'^\.', # is hidden/starts with a "."
) )
# configure logger default # configure logger default
logging.basicConfig( logging.basicConfig(
level=LOGGING_SECTION.LOG_LEVEL.object_from_value, 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 BITRATE = AUDIO_SECTION.BITRATE.object_from_value
AUDIO_FORMAT = AUDIO_SECTION.AUDIO_FORMAT.object_from_value AUDIO_FORMAT = AUDIO_SECTION.AUDIO_FORMAT.object_from_value
DOWNLOAD_PATH = AUDIO_SECTION.DOWNLOAD_PATH.object_from_value DOWNLOAD_PATH = AUDIO_SECTION.DOWNLOAD_PATH.object_from_value
DOWNLOAD_FILE = AUDIO_SECTION.DOWNLOAD_FILE.object_from_value DOWNLOAD_FILE = AUDIO_SECTION.DOWNLOAD_FILE.object_from_value
DEFAULT_VALUES = { DEFAULT_VALUES = {
@ -86,12 +75,19 @@ DEFAULT_VALUES = {
"audio_format": AUDIO_FORMAT "audio_format": AUDIO_FORMAT
} }
TOR: bool = CONNECTION_SECTION.USE_TOR.object_from_value TOR: bool = CONNECTION_SECTION.USE_TOR.object_from_value
proxies = { proxies = {}
'http': 'socks5h://127.0.0.1:9150', if len(CONNECTION_SECTION.PROXIES) > 0:
'https': 'socks5h://127.0.0.1:9150' """
} if TOR else {} 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 # size of the chunks that are streamed
CHUNK_SIZE = CONNECTION_SECTION.CHUNK_SIZE.object_from_value CHUNK_SIZE = CONNECTION_SECTION.CHUNK_SIZE.object_from_value