From 5f26cba34e68f9c3391af94906583157d68479b3 Mon Sep 17 00:00:00 2001 From: moleculekayak Date: Thu, 8 Aug 2024 15:21:33 -0700 Subject: [PATCH] Improve startup logging (#13) * Improved logging on script startup * Removed logging when running in input_file mode --- main.py | 30 +++++++++++++++++++++++++++--- src/api.py | 4 ++-- src/errors.py | 17 ++++++++--------- 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/main.py b/main.py index 2f3e8c6..5a7e45a 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,4 @@ +import sys from colorama import Fore from src.api import RedAPI, OpsAPI @@ -10,9 +11,15 @@ def cli_entrypoint(args): try: - config = Config().load(args.config_file) - red_api, ops_api = __verify_api_keys(config) - injector = Injection(config).setup() if config.inject_torrents else None + # using input_file means this is probably running as a script and extra printing wouldn't be appreciated + should_print = args.input_directory or args.server + config = command_log_wrapper("Reading config file:", should_print, lambda: Config().load(args.config_file)) + red_api, ops_api = command_log_wrapper("Verifying API keys:", should_print, lambda: __verify_api_keys(config)) + + if config.inject_torrents: + injector = command_log_wrapper("Connecting to torrent client:", should_print, lambda: Injection(config).setup()) + else: + injector = None if args.server: run_webserver(args.input_directory, args.output_directory, red_api, ops_api, injector, port=config.server_port) @@ -37,6 +44,23 @@ def __verify_api_keys(config): return red_api, ops_api +def command_log_wrapper(label, should_print, func): + def maybe_print(str, *args, **kwargs): + if should_print: + print(str, *args, **kwargs) + sys.stdout.flush() + + maybe_print(f"{label} ", end="") + + try: + result = func() + maybe_print(f"{Fore.GREEN}Success{Fore.RESET}") + return result + except Exception as e: + maybe_print(f"{Fore.RED}Error{Fore.RESET}") + raise e + + if __name__ == "__main__": args = parse_args() diff --git a/src/api.py b/src/api.py index 90e7e5e..cd38890 100644 --- a/src/api.py +++ b/src/api.py @@ -81,13 +81,13 @@ def __get(self, action, **params): else: sleep(0.2) - handle_error(description="Maximum number of retries reached", should_exit=True) + handle_error(description="Maximum number of retries reached", should_raise=True) def __get_announce_url(self): try: account_info = self.get_account_info() except AuthenticationError as e: - handle_error(description=f"Authentication to {self.sitename} failed", exception_details=e, should_exit=True) + handle_error(description=f"Authentication to {self.sitename} failed", exception_details=e, should_raise=True) passkey = account_info["response"]["passkey"] return f"{self.tracker_url}/{passkey}/announce" diff --git a/src/errors.py b/src/errors.py index 94e2a2e..0e64c3f 100644 --- a/src/errors.py +++ b/src/errors.py @@ -1,4 +1,3 @@ -import sys from time import sleep from colorama import Fore @@ -9,17 +8,17 @@ def handle_error( exception_details: (str | None) = None, wait_time: int = 0, extra_description: str = "", - should_exit: bool = False, + should_raise: bool = False, ) -> None: - action = "Exiting" if should_exit else "Retrying" - action += f" in {wait_time} seconds..." if wait_time else "..." + action = "" if should_raise else "Retrying" + action += f" in {wait_time} seconds..." if wait_time else "" exception_message = f"\n{Fore.LIGHTBLACK_EX}{exception_details}" if exception_details is not None else "" - print(f"{Fore.RED}Error: {description}{extra_description}. {action}{exception_message}{Fore.RESET}") - sleep(wait_time) - - if should_exit: - sys.exit(1) + if should_raise: + raise Exception(f"{description}{extra_description}. {action}{exception_message}{Fore.RESET}") + else: + print(f"{Fore.RED}Error: {description}{extra_description}. {action}{exception_message}{Fore.RESET}") + sleep(wait_time) class AuthenticationError(Exception):