config works

This commit is contained in:
Hellow 2023-09-10 19:00:51 +02:00
parent b6eb1cbec6
commit 0ce6be5d2f
5 changed files with 43 additions and 34 deletions

View File

@ -235,6 +235,5 @@ class YoutubeMusic(SuperYouTube):
return Album()
def fetch_artist(self, source: Source, stop_at_level: int = 1) -> Artist:
print("fuck you")
print(source)
return Artist()

View File

@ -3,7 +3,8 @@ from typing import Optional, List, Union, Iterable, Callable
from dataclasses import dataclass
import logging
import toml
from copy import deepcopy
from copy import deepcopy, copy
from urllib.parse import urlparse, urlunparse, ParseResult
from ...exception.config import SettingValueError
from ..utils import comment
@ -20,7 +21,6 @@ def comment_string(uncommented: str) -> str:
processed_lines: List[str] = []
for line in unprocessed_lines:
line: str = line.strip()
if line.startswith(COMMENT_PREFIX) or line == "":
processed_lines.append(line)
continue
@ -56,17 +56,14 @@ class Attribute:
self.name = name
self.raw_data = {name: default_value}
self.value = None
self.value = self._recursive_parse_object(default_value, self.parse_simple_value)
self.description: Optional[str] = description
self.loaded_settings: dict = None
def initialize_from_config(self, loaded_settings: dict):
self.loaded_settings = loaded_settings
if not self.load_toml(self.raw_data):
logging.warning(f"Couldn't load the initial value of {self.name}: {self.raw_data[self.name]}")
self.loaded_settings.__setitem__(self.name, self.value, True)
def unparse_simple_value(self, value: any) -> any:
return value
@ -75,19 +72,28 @@ class Attribute:
return value
def _recursive_parse_object(self, __object, callback: Callable):
__object = copy(__object)
if isinstance(__object, dict):
for key, value in __object.items():
__object[key] = self._recursive_parse_object(value, callback)
return __object
if isinstance(__object, Union[list, tuple]):
if isinstance(__object, list) or (isinstance(__object, tuple) and not isinstance(__object, ParseResult)):
for i, item in enumerate(__object):
__object[i] = self._recursive_parse_object(item, callback)
return __object
return callback(__object)
def parse(self, unparsed_value):
self.value = self._recursive_parse_object(unparsed_value, self.parse_simple_value)
return self.value
def unparse(self, parsed_value):
return self._recursive_parse_object(parsed_value, self.unparse_simple_value)
def load_toml(self, loaded_toml: dict) -> bool:
"""
returns true if succesfull
@ -98,16 +104,12 @@ class Attribute:
self.loaded_settings.__setitem__(self.name, self.value, True)
return
self.raw_data = loaded_toml[self.name]
_object = deepcopy(loaded_toml[self.name])
try:
parsed_object = self._recursive_parse_object(_object, self.parse_simple_value)
self.parse(loaded_toml[self.name])
except SettingValueError as settings_error:
logging.warning(settings_error)
return False
self.value = parsed_object
self.loaded_settings.__setitem__(self.name, self.value, True)
return True
@ -120,8 +122,9 @@ class Attribute:
if self.description is not None:
string += comment(self.description) + "\n"
string += toml.dumps(self.raw_data)
string += toml.dumps({self.name: self.unparse(self.value)})
# print(string)
return string
def __str__(self):

View File

@ -1,4 +1,4 @@
from pathlib import Path
from pathlib import Path, PosixPath
from typing import Optional, Dict, Set
from urllib.parse import urlparse, urlunparse
import logging
@ -16,11 +16,13 @@ class UrlAttribute(Attribute):
class PathAttribute(Attribute):
def parse_simple_value(self, value: any) -> any:
def parse_simple_value(self, value: any) -> Path:
if isinstance(value, Path) or isinstance(value, PosixPath):
return value
return Path(value)
def unparse_simple_value(self, value: any) -> any:
return value.resolve()
def unparse_simple_value(self, value: Path) -> any:
return str(value.resolve())

View File

@ -16,10 +16,13 @@ class ConfigDict(dict):
def __getattribute__(self, __name: str) -> Any:
return super().__getattribute__(__name)
def __setitem__(self, __key: Any, __value: Any, from_attribute: bool = False) -> None:
def __setitem__(self, __key: Any, __value: Any, from_attribute: bool = False, is_parsed: bool = False) -> None:
if not from_attribute:
attribute: Attribute = self.config_reference.attribute_map[__key]
attribute.load_toml({attribute.name: __value})
if is_parsed:
attribute.value = __value
else:
attribute.parse(__value)
self.config_reference.write()
__value = attribute.value
@ -44,7 +47,7 @@ class Config:
@property
def toml_string(self):
return "\n\n".join(component.toml_string for component in self.component_list)
return "\n".join(component.toml_string for component in self.component_list)
def write(self):
with self.config_file.open("w") as conf_file:

View File

@ -19,12 +19,14 @@ The changes you make to the comments, will be discarded, next time you run music
Latest reset: {datetime.now()}
_________ __
\\_ ___ \\ __ __ _/ |_ ____
/ \\ \\/ | | \\\\ __\\_/ __ \\
\\ \\____| | / | | \\ ___/
\\______ /|____/ |__| \\___ >
\\/ \\/
_____
/ ____|
| | __ __ _ _ _
| | |_ | / _` || | | |
| |__| || (_| || |_| |
\_____| \__,_| \__, |
__/ |
|___/
"""),
Attribute(name="hasnt_yet_started", default_value=False, description="This will be set automatically, to look if it needs to run the scripts that run on start."),
@ -74,10 +76,10 @@ all the error messages are shown."""),
EmptyLine(),
PathAttribute(name="music_directory", default_value=LOCATIONS.MUSIC_DIRECTORY, description="The directory, all the music will be downloaded to."),
PathAttribute(name="temp_directory", default_value=LOCATIONS.TEMP_DIRECTORY, description="All temporary stuff is gonna be dumped in this directory."),
PathAttribute(name="log_file", default_value=LOCATIONS.get_log_file("download_logs.log")),
PathAttribute(name="ffmpeg_binary", default_value=LOCATIONS.FFMPEG_BIN, description="Set the path to the ffmpeg binary."),
PathAttribute(name="music_directory", default_value=LOCATIONS.MUSIC_DIRECTORY.resolve(), description="The directory, all the music will be downloaded to."),
PathAttribute(name="temp_directory", default_value=LOCATIONS.TEMP_DIRECTORY.resolve(), description="All temporary stuff is gonna be dumped in this directory."),
PathAttribute(name="log_file", default_value=LOCATIONS.get_log_file("download_logs.log").resolve()),
PathAttribute(name="ffmpeg_binary", default_value=LOCATIONS.FFMPEG_BIN.resolve(), description="Set the path to the ffmpeg binary."),
Attribute(
name="not_a_genre_regex",
description="These regular expressions tell music-kraken, which sub-folders of the music-directory\n"