Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve startup logging #13

Merged
merged 2 commits into from
Aug 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -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()

4 changes: 2 additions & 2 deletions src/api.py
Original file line number Diff line number Diff line change
@@ -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"
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
@@ -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):