finished writing of config file

This commit is contained in:
Hellow 2023-04-14 17:01:32 +02:00
parent b07ea4aff0
commit 7c24707650
7 changed files with 153 additions and 90 deletions

View File

@ -1,6 +1,3 @@
import music_kraken
if __name__ == "__main__": if __name__ == "__main__":
import argparse import argparse
@ -50,6 +47,8 @@ if __name__ == "__main__":
print("Setting logging-level to DEBUG") print("Setting logging-level to DEBUG")
logging.getLogger().setLevel(logging.DEBUG) logging.getLogger().setLevel(logging.DEBUG)
import music_kraken
# getting the genre # getting the genre
genre: str = arguments.genre genre: str = arguments.genre
if arguments.test: if arguments.test:

View File

@ -1,2 +1,7 @@
from .logging import LOGGING_SECTION from .logging import LOGGING_SECTION
from .audio import AUDIO_SECTION from .audio import AUDIO_SECTION
from .config import read, write
read()
write()

View File

@ -1,6 +1,6 @@
import logging import logging
from .config import SingleAttribute, FloatAttribute, StringAttribute, IntAttribute, Section, Description, EmptyLine from .base_classes import SingleAttribute, FloatAttribute, StringAttribute, IntAttribute, Section, Description, EmptyLine
# Only the formats with id3 metadata can be used # Only the formats with id3 metadata can be used
# https://www.audioranger.com/audio-formats.php # https://www.audioranger.com/audio-formats.php
@ -40,8 +40,8 @@ class AudioSection(Section):
def __init__(self): def __init__(self):
self.BITRATE = FloatAttribute( self.BITRATE = FloatAttribute(
name="bitrate", name="bitrate",
description="Streams the audio with this bitrate (kB/s). Can't get more bitrate than the audio source " description="Streams the audio with given bitrate [kB/s]. "
"though.", "Can't stream with a higher Bitrate, than the audio source provides.",
value="125" value="125"
) )
@ -102,9 +102,9 @@ ID3.1: {', '.join(_sorted_id3_1_formats)}
) )
self.attribute_list = [ self.attribute_list = [
EmptyLine(),
self.BITRATE, self.BITRATE,
self.AUDIO_FORMAT, self.AUDIO_FORMAT,
EmptyLine(),
Description(""" Description("""
There are multiple fields, you can use for the path and file name: There are multiple fields, you can use for the path and file name:
- genre - genre

View File

@ -0,0 +1,101 @@
from dataclasses import dataclass
from typing import Optional, List, Union
COMMENT_PREFIX = "#"
def comment_string(uncommented: str) -> str:
unprocessed_lines = uncommented.split("\n")
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
line = COMMENT_PREFIX + " " + line
processed_lines.append(line)
return "\n".join(processed_lines)
@dataclass
class Attribute:
name: str
description: Optional[str]
value: Union[str, List[str]]
@property
def description_as_comment(self):
return comment_string(self.description)
@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(SingleAttribute):
@property
def object_from_value(self) -> str:
return self.value.strip()
class IntAttribute(SingleAttribute):
@property
def object_from_value(self) -> int:
if not self.value.isdigit():
raise ValueError(f"The value of {self.name} needs to be an integer, not {self.value}")
return int(self.value)
class FloatAttribute(SingleAttribute):
@property
def object_from_value(self) -> float:
if not self.value.isnumeric():
raise ValueError(f"The value of {self.name} needs to be a number, not {self.value}")
return float(self.value)
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 comment_string(self.description)
class EmptyLine(Description):
def __init__(self):
self.description = ""
class Section:
"""
A placeholder class
"""
attribute_list: List[Union[
Attribute,
Description
]]
def __str__(self):
return "\n".join(attribute.__str__() for attribute in self.attribute_list)

View File

@ -1,90 +1,46 @@
from dataclasses import dataclass from typing import Union, Tuple
from typing import Optional, List, Union import logging
import os
COMMENT_PREFIX = "# " from ..path_manager import LOCATIONS
from .base_classes import Description, Attribute, Section, EmptyLine
from .audio import AUDIO_SECTION
from .logging import LOGGING_SECTION
@dataclass class Config:
class Attribute:
name: str
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(SingleAttribute):
@property
def object_from_value(self) -> str:
return self.value.strip()
class IntAttribute(SingleAttribute):
@property
def object_from_value(self) -> int:
if not self.value.isdigit():
raise ValueError(f"The value of {self.name} needs to be an integer, not {self.value}")
return int(self.value)
class FloatAttribute(SingleAttribute):
@property
def object_from_value(self) -> float:
if not self.value.isnumeric():
raise ValueError(f"The value of {self.name} needs to be a number, not {self.value}")
return float(self.value)
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):
lines = self.description.split("\n")
return "\n".join(f"{COMMENT_PREFIX}{line}" for line in lines)
class EmptyLine(Description):
def __init__(self): def __init__(self):
self.description = "\n" 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("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("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("🏳️‍⚧️🏳️‍⚧️ Protect trans youth. 🏳️‍⚧️🏳️‍⚧️"),
EmptyLine()
)
def __str__(self): @property
return self.description def config_string(self) -> str:
return "\n\n".join(str(element) for element in self.config_elements)
def write_to_config_file(self, path: os.PathLike):
with open(path, "w") as conf_file:
conf_file.write(self.config_string)
class Section: config = Config()
"""
A placeholder class
"""
attribute_list: List[Union[
Attribute,
Description
]]
def __str__(self):
return "\n".join(attribute.__str__() for attribute in self.attribute_list) def read():
pass
def write():
config.write_to_config_file(LOCATIONS.CONFIG_FILE)

View File

@ -1,7 +1,7 @@
import logging import logging
from typing import Callable from typing import Callable
from .config import SingleAttribute, StringAttribute, Section, Description, EmptyLine from .base_classes import SingleAttribute, StringAttribute, Section, Description, EmptyLine
LOG_LEVELS = { LOG_LEVELS = {
"CRITICAL": 50, "CRITICAL": 50,
@ -104,6 +104,7 @@ class LoggingSection(Section):
Description("Logging settings for the actual logging:"), Description("Logging settings for the actual logging:"),
self.FORMAT, self.FORMAT,
self.LOG_LEVEL, self.LOG_LEVEL,
EmptyLine(),
Description("Just the names for different logger, for different parts of the programm:"), Description("Just the names for different logger, for different parts of the programm:"),
self.DOWNLOAD_LOGGER, self.DOWNLOAD_LOGGER,
self.TAGGING_LOGGER, self.TAGGING_LOGGER,

View File

@ -16,6 +16,7 @@ class Locations:
self.CONFIG_DIRECTORY = get_config_directory(str(application_name)) self.CONFIG_DIRECTORY = get_config_directory(str(application_name))
self.CONFIG_DIRECTORY.mkdir(exist_ok=True) self.CONFIG_DIRECTORY.mkdir(exist_ok=True)
self.CONFIG_FILE = Path(self.CONFIG_DIRECTORY, f"{application_name}.conf")
def get_log_file(self, file_name: os.PathLike) -> Path: def get_log_file(self, file_name: os.PathLike) -> Path:
return Path(self.TEMP_DIRECTORY, file_name) return Path(self.TEMP_DIRECTORY, file_name)