Skip to content

Commit

Permalink
Improve startup logging (#13)
Browse files Browse the repository at this point in the history
* Improved logging on script startup

* Removed logging when running in input_file mode
  • Loading branch information
moleculekayak authored Aug 8, 2024
1 parent 58ab2a9 commit 5f26cba
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
30 changes: 27 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from colorama import Fore

from src.api import RedAPI, OpsAPI
Expand All @@ -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)
Expand All @@ -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()

Expand Down
4 changes: 2 additions & 2 deletions src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
17 changes: 8 additions & 9 deletions src/errors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import sys
from time import sleep

from colorama import Fore
Expand All @@ -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):
Expand Down

0 comments on commit 5f26cba

Please sign in to comment.