- Improve error handling during configuration verification

- Wrap long sentences
pull/1805/head
Sandra Serrano 2 years ago
parent bd4b4c1394
commit 3ed2750794

@ -82,7 +82,8 @@ def shutdown() -> NoReturn:
if __name__ == "__main__": if __name__ == "__main__":
if sys.version_info.major != 3 or sys.version_info.minor != 10: if sys.version_info.major != 3 or sys.version_info.minor != 10:
print( 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() sys.exit()
ffmpeg_install() ffmpeg_install()
@ -90,7 +91,6 @@ if __name__ == "__main__":
config = settings.check_toml( config = settings.check_toml(
f"{directory}/utils/.config.template.toml", f"{directory}/config.toml" f"{directory}/utils/.config.template.toml", f"{directory}/config.toml"
) )
config is False and sys.exit()
if ( if (
not settings.config["settings"]["tts"]["tiktok_sessionid"] not settings.config["settings"]["tts"]["tiktok_sessionid"]

@ -1,7 +1,6 @@
import re import re
from typing import Tuple, Dict
from pathlib import Path
import toml import toml
from pathlib import Path
from rich.console import Console from rich.console import Console
from utils.console import handle_input from utils.console import handle_input
@ -106,37 +105,36 @@ def check_vars(path, checks):
crawl_and_check(config, 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 global config
config = None config = None
try: try:
template = toml.load(template_file) template = toml.load(template_file)
except Exception as error: except Exception as error:
console.print(f"[red bold]Encountered error when trying to to load {template_file}: {error}") console.print(f"[red bold]Encountered error when trying to to load {template_file}: {error}")
return False raise
try: try:
config = toml.load(config_file) config = toml.load(config_file)
except toml.TomlDecodeError: except toml.TomlDecodeError:
console.print( console.print(
f"""[blue]Couldn't read {config_file}. f"""[blue]Couldn't read {config_file}. Overwrite it?(y/n)"""
Overwrite it?(y/n)"""
) )
if not input().startswith("y"): if not input().startswith("y"):
print("Unable to read config, and not allowed to overwrite it. Giving up.") print("Unable to read config, and not allowed to overwrite it. Giving up.")
return False raise
else: else:
try: try:
with open(config_file, "w") as f: with open(config_file, "w") as f:
f.write("") f.write("")
except: except:
console.print( 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: except FileNotFoundError:
console.print( console.print(
f"""[blue]Couldn't find {config_file} f"""[blue]Couldn't find {config_file} Creating it now."""
Creating it now."""
) )
try: try:
with open(config_file, "x") as f: with open(config_file, "x") as f:
@ -144,9 +142,10 @@ Creating it now."""
config = {} config = {}
except: except:
console.print( 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( console.print(
"""\ """\

Loading…
Cancel
Save