diff --git a/chatmastermind/main.py b/chatmastermind/main.py index 1e41784..a57a8e2 100755 --- a/chatmastermind/main.py +++ b/chatmastermind/main.py @@ -3,6 +3,7 @@ # vim: set fileencoding=utf-8 : import sys +import os import argcomplete import argparse from pathlib import Path @@ -156,6 +157,46 @@ def create_parser() -> argparse.ArgumentParser: return parser +def create_directories(config: Config) -> None: # noqa: 11 + """ + Create the directories in the given configuration if they don't exist. + """ + def make_dir(path: Path) -> None: + try: + os.makedirs(path.absolute()) + except Exception as e: + print(f"Creating directory '{path.absolute()}' failed with: {e}") + sys.exit(1) + # Cache + cache_path = Path(config.cache) + if not cache_path.exists(): + answer = input(f"Cache directory '{cache_path}' does not exist. Create it? [y/n]") + if answer.lower() in ['y', 'yes']: + make_dir(cache_path.absolute()) + else: + print("Can't continue without a valid cache directory!") + sys.exit(1) + # DB + db_path = Path(config.db) + if not db_path.exists(): + answer = input(f"DB directory '{db_path}' does not exist. Create it? [y/n]") + if answer.lower() in ['y', 'yes']: + make_dir(db_path.absolute()) + else: + print("Can't continue without a valid DB directory!") + sys.exit(1) + # Glossaries + if config.glossaries: + glossaries_path = Path(config.glossaries) + if not glossaries_path.exists(): + answer = input(f"Glossaries directory '{glossaries_path}' does not exist. Create it? [y/n]") + if answer.lower() in ['y', 'yes']: + make_dir(glossaries_path.absolute()) + else: + print("Can't continue without a valid glossaries directory. Create it or remove it from the configuration.") + sys.exit(1) + + def main() -> int: parser = create_parser() args = parser.parse_args() @@ -165,6 +206,7 @@ def main() -> int: command.func(command) else: config = Config.from_file(args.config) + create_directories(config) command.func(command, config) return 0