implemented config cli

This commit is contained in:
Hellow 2023-04-15 11:54:17 +02:00
parent 5f99e4e41b
commit 9331c39028
5 changed files with 78 additions and 2 deletions

View File

@ -7,6 +7,7 @@ from pathlib import Path
from typing import List from typing import List
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.string_processing import fit_to_file_system
from .utils.shared import MUSIC_DIR, MODIFY_GC, NOT_A_GENRE_REGEX, get_random_message from .utils.shared import MUSIC_DIR, MODIFY_GC, NOT_A_GENRE_REGEX, get_random_message
@ -57,6 +58,64 @@ def exit_message():
print("See you soon! :3") print("See you soon! :3")
def settings(
name: str = None,
value: str = None,
):
def modify_setting(_name: str, _value: str, invalid_ok: bool = True) -> bool:
try:
config.set_name_to_value(_name, _value)
except ValueError as e:
if invalid_ok:
print()
print(e.args[0])
return False
else:
raise e
except KeyError as e:
if invalid_ok:
print(f"There is no such setting as: {_name}")
return False
else:
raise e
write()
return True
def print_settings():
for i, attribute in enumerate(config):
print(f"{i:0>2}: {attribute.name}={attribute.value}")
def modify_setting_by_index(index: int, recursive: bool = True) -> bool:
attribute = list(config)[index]
print()
print(attribute)
input__ = input("New value: ")
if not modify_setting(attribute.name, input__.strip()):
if recursive:
return modify_setting_by_index(index)
else:
return False
return True
if name is not None and value is not None:
modify_setting(name, value, invalid_ok=False)
while True:
print_settings()
input_ = input("Id of setting to modify: ")
print()
if input_.isdigit() and int(input_) < len(config):
if modify_setting_by_index(int(input_)):
return
else:
print("Please input a valid ID.")
print()
def cli( def cli(
genre: str = None, genre: str = None,
download_all: bool = False, download_all: bool = False,

View File

@ -40,6 +40,12 @@ if __name__ == "__main__":
help="Downloads the content of given url." help="Downloads the content of given url."
) )
parser.add_argument(
'--settings',
help="Opens a menu to modify the settings",
action="store_true"
)
arguments = parser.parse_args() arguments = parser.parse_args()
if arguments.verbose or arguments.test: if arguments.verbose or arguments.test:
@ -49,6 +55,10 @@ if __name__ == "__main__":
import music_kraken import music_kraken
if arguments.settings:
music_kraken.settings()
exit()
# 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,4 @@
from .config import config, read, write
# tells what exists # tells what exists
__all__ = ["shared", "object_handeling", "phonetic_compares", "functions"] __all__ = ["shared", "object_handeling", "phonetic_compares", "functions"]

View File

@ -1,6 +1,6 @@
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 from .config import read, write, config
read() read()

View File

@ -1,4 +1,4 @@
from typing import Union, Tuple, Dict, Iterable from typing import Union, Tuple, Dict, Iterable, List
import logging import logging
import os import os
@ -25,6 +25,7 @@ class Config:
Description("🏳️‍⚧️🏳️‍⚧️ Protect trans youth. 🏳️‍⚧️🏳️‍⚧️\n"), Description("🏳️‍⚧️🏳️‍⚧️ Protect trans youth. 🏳️‍⚧️🏳️‍⚧️\n"),
) )
self._length = 0
self._section_list: List[Section] = [] self._section_list: List[Section] = []
self._name_section_map: Dict[str, Section] = dict() self._name_section_map: Dict[str, Section] = dict()
@ -40,6 +41,7 @@ class Config:
f"{element.__class__.__name__} {self._name_section_map[name].__class__.__name__}") f"{element.__class__.__name__} {self._name_section_map[name].__class__.__name__}")
self._name_section_map[name] = element self._name_section_map[name] = element
self._length += 1
def set_name_to_value(self, name: str, value: str): def set_name_to_value(self, name: str, value: str):
if name not in self._name_section_map: if name not in self._name_section_map:
@ -47,6 +49,9 @@ class Config:
self._name_section_map[name][name] = value self._name_section_map[name][name] = value
def __len__(self):
return self._length
@property @property
def config_string(self) -> str: def config_string(self) -> str:
return "\n\n".join(str(element) for element in self.config_elements) return "\n\n".join(str(element) for element in self.config_elements)