From 3ed27507945f8989368937363a717037c8cb25eb Mon Sep 17 00:00:00 2001 From: Sandra Serrano Date: Tue, 22 Aug 2023 16:43:51 +0200 Subject: [PATCH] - Improve error handling during configuration verification - Wrap long sentences --- main.py | 4 ++-- utils/settings.py | 25 ++++++++++++------------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/main.py b/main.py index 0b48405..ea0a0cb 100755 --- a/main.py +++ b/main.py @@ -82,7 +82,8 @@ def shutdown() -> NoReturn: if __name__ == "__main__": if sys.version_info.major != 3 or sys.version_info.minor != 10: print( - "Hey! Congratulations, you've made it so far (which is pretty rare with no Python 3.10). Unfortunately, this program only works on Python 3.10. Please install Python 3.10 and try again." + "Hey! Congratulations, you've made it so far (which is pretty rare with no Python 3.10). " + "Unfortunately, this program only works on Python 3.10. Please install Python 3.10 and try again." ) sys.exit() ffmpeg_install() @@ -90,7 +91,6 @@ if __name__ == "__main__": config = settings.check_toml( f"{directory}/utils/.config.template.toml", f"{directory}/config.toml" ) - config is False and sys.exit() if ( not settings.config["settings"]["tts"]["tiktok_sessionid"] diff --git a/utils/settings.py b/utils/settings.py index 60efedb..3987557 100755 --- a/utils/settings.py +++ b/utils/settings.py @@ -1,7 +1,6 @@ import re -from typing import Tuple, Dict -from pathlib import Path import toml +from pathlib import Path from rich.console import Console from utils.console import handle_input @@ -106,37 +105,36 @@ def check_vars(path, checks): crawl_and_check(config, path, checks) -def check_toml(template_file, config_file) -> Tuple[bool, Dict]: +def check_toml(template_file, config_file) -> dict: global config config = None try: template = toml.load(template_file) except Exception as error: console.print(f"[red bold]Encountered error when trying to to load {template_file}: {error}") - return False + raise try: config = toml.load(config_file) except toml.TomlDecodeError: console.print( - f"""[blue]Couldn't read {config_file}. -Overwrite it?(y/n)""" + f"""[blue]Couldn't read {config_file}. Overwrite it?(y/n)""" ) if not input().startswith("y"): print("Unable to read config, and not allowed to overwrite it. Giving up.") - return False + raise else: try: with open(config_file, "w") as f: f.write("") except: console.print( - f"[red bold]Failed to overwrite {config_file}. Giving up.\nSuggestion: check {config_file} permissions for the user." + f"[red bold]Failed to overwrite {config_file}. Giving up." + f"\nSuggestion: check {config_file} permissions for the user." ) - return False + raise except FileNotFoundError: console.print( - f"""[blue]Couldn't find {config_file} -Creating it now.""" + f"""[blue]Couldn't find {config_file} Creating it now.""" ) try: with open(config_file, "x") as f: @@ -144,9 +142,10 @@ Creating it now.""" config = {} except: console.print( - f"[red bold]Failed to write to {config_file}. Giving up.\nSuggestion: check the folder's permissions for the user." + f"[red bold]Failed to write to {config_file}. Giving up." + f"\nSuggestion: check the folder's permissions for the user." ) - return False + raise console.print( """\