finished the easy part of migration

This commit is contained in:
Hellow
2023-09-10 16:54:06 +02:00
parent d2dd015817
commit a81a4a05a7
10 changed files with 59 additions and 159 deletions

View File

@@ -1,127 +0,0 @@
from typing import Union, Tuple, Dict, Iterable, List
from datetime import datetime
import logging
import os
from ..exception.config import SettingNotFound, SettingValueError
from ..path_manager import LOCATIONS
from .base_classes import Description, Attribute, Section, EmptyLine, COMMENT_PREFIX
from .sections.audio import AUDIO_SECTION
from .sections.logging import LOGGING_SECTION
from .sections.connection import CONNECTION_SECTION
from .sections.misc import MISC_SECTION
from .sections.paths import PATHS_SECTION
LOGGER = logging.getLogger("config")
class Config:
def __init__(self):
self.config_elements: Tuple[Union[Description, Attribute, Section], ...] = (
Description("IMPORTANT: If you modify this file, the changes for the actual setting, will be kept as is.\n"
"The changes you make to the comments, will be discarded, next time you run music-kraken. "
"Have fun!"),
Description(f"Latest reset: {datetime.now()}"),
Description("Those are all Settings for the audio codec.\n"
"If you, for some reason wanna fill your drive real quickly, I mean enjoy HIFI music,\n"
"feel free to tinker with the Bitrate or smth. :)"),
AUDIO_SECTION,
Description("Modify how Music-Kraken connects to the internet:"),
CONNECTION_SECTION,
Description("Modify all your paths, except your config file..."),
PATHS_SECTION,
Description("For all your Logging needs.\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^"),
LOGGING_SECTION,
Description("If there are stupid settings, they are here."),
MISC_SECTION,
Description("🏳️‍⚧️🏳️‍⚧️ Protect trans youth. 🏳️‍⚧️🏳️‍⚧️\n"),
)
self._length = 0
self._section_list: List[Section] = []
self.name_section_map: Dict[str, Section] = dict()
for element in self.config_elements:
if not isinstance(element, Section):
continue
self._section_list.append(element)
for name in element.name_attribute_map:
if name in self.name_section_map:
raise ValueError(f"Two sections have the same name: "
f"{name}: "
f"{element.__class__.__name__} {self.name_section_map[name].__class__.__name__}")
self.name_section_map[name] = element
self._length += 1
def set_name_to_value(self, name: str, value: str, silent: bool = True):
"""
:raises SettingValueError, SettingNotFound:
:param name:
:param value:
:return:
"""
if name not in self.name_section_map:
if silent:
LOGGER.warning(f"The setting \"{name}\" is either deprecated, or doesn't exist.")
return
raise SettingNotFound(setting_name=name)
LOGGER.debug(f"setting: {name} value: {value}")
self.name_section_map[name].modify_setting(setting_name=name, new_value=value)
def __len__(self):
return self._length
@property
def config_string(self) -> str:
return "\n\n".join(str(element) for element in self.config_elements)
def _parse_conf_line(self, line: str, index: int):
"""
:raises SettingValueError, SettingNotFound:
:param line:
:param index:
:return:
"""
line = line.strip()
if line.startswith(COMMENT_PREFIX):
return
if line == "":
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("=")
name = line_segments[0]
value = "=".join(line_segments[1:])
self.set_name_to_value(name, value)
def read_from_config_file(self, path: os.PathLike):
with open(path, "r", encoding=LOCATIONS.FILE_ENCODING) 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)
def write_to_config_file(self, path: os.PathLike):
with open(path, "w", encoding=LOCATIONS.FILE_ENCODING) as conf_file:
conf_file.write(self.config_string)
def __iter__(self) -> Iterable[Attribute]:
for section in self._section_list:
for name, attribute in section.name_attribute_map.items():
yield attribute

View File

@@ -143,7 +143,7 @@ class AudioFormatAttribute(Attribute):
class LoggerAttribute(Attribute):
def parse_simple_value(self, value: str) -> logging.Logger:
return logging.getLogger(self.value)
return logging.getLogger(value)
def unparse_simple_value(self, value: logging.Logger) -> any:
return value.name

View File

@@ -1,4 +1,4 @@
from typing import Tuple, Union
from typing import Any, Tuple, Union
from pathlib import Path
import logging
@@ -7,6 +7,23 @@ import toml
from .attributes.attribute import Attribute, Description, EmptyLine
class ConfigDict(dict):
def __init__(self, config_reference: "Config", *args, **kwargs):
self.config_reference: Config = config_reference
super().__init__(*args, **kwargs)
def __getattribute__(self, __name: str) -> Any:
return super().__getattribute__(__name)
def __setitem__(self, __key: Any, __value: Any) -> None:
attribute: Attribute = self.config_reference.attribute_map[__key]
attribute.load_toml({attribute.name: __value})
self.config_reference.write()
return super().__setitem__(__key, attribute.value)
class Config:
def __init__(self, componet_list: Tuple[Union[Attribute, Description, EmptyLine]], config_file: Path) -> None:
self.config_file: Path = config_file
@@ -14,6 +31,14 @@ class Config:
self.component_list: Tuple[Union[Attribute, Description, EmptyLine]] = componet_list
self.loaded_settings: dict = {}
self.attribute_map = {}
for component in self.component_list:
if not isinstance(component, Attribute):
continue
self.attribute_map[component.name] = component
self.loaded_settings[component.name] = component.value
@property
def toml_string(self):
"\n\n".join(component.toml_string for component in self.component_list)

View File

@@ -18,7 +18,7 @@ config = Config([
Reference for the logging formats: https://docs.python.org/3/library/logging.html#logrecord-attributes"""),
IntegerSelect(
name="log_level",
default_value=str(logging.INFO),
default_value=logging.INFO,
options={
"CRITICAL": 50,
"ERROR": 40,

View File

@@ -103,6 +103,7 @@ all the error messages are shown."""),
], description="""Just some nice and wholesome messages.
If your mindset has traits of a [file corruption], you might not agree.
But anyways... Freedom of thought, so go ahead and change the messages."""),
Attribute(name="modify_gc", default_value=True),
Attribute(name="id_bits", default_value=64, description="I really dunno why I even made this a setting.. Modifying this is a REALLY dumb idea."),
Description("🏳️‍⚧️🏳️‍⚧️ Protect trans youth. 🏳️‍⚧️🏳️‍⚧️\n"),