Skip to content

Commit 5f26cba

Browse files
Improve startup logging (#13)
* Improved logging on script startup * Removed logging when running in input_file mode
1 parent 58ab2a9 commit 5f26cba

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

main.py

+27-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
from colorama import Fore
23

34
from src.api import RedAPI, OpsAPI
@@ -10,9 +11,15 @@
1011

1112
def cli_entrypoint(args):
1213
try:
13-
config = Config().load(args.config_file)
14-
red_api, ops_api = __verify_api_keys(config)
15-
injector = Injection(config).setup() if config.inject_torrents else None
14+
# using input_file means this is probably running as a script and extra printing wouldn't be appreciated
15+
should_print = args.input_directory or args.server
16+
config = command_log_wrapper("Reading config file:", should_print, lambda: Config().load(args.config_file))
17+
red_api, ops_api = command_log_wrapper("Verifying API keys:", should_print, lambda: __verify_api_keys(config))
18+
19+
if config.inject_torrents:
20+
injector = command_log_wrapper("Connecting to torrent client:", should_print, lambda: Injection(config).setup())
21+
else:
22+
injector = None
1623

1724
if args.server:
1825
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):
3744
return red_api, ops_api
3845

3946

47+
def command_log_wrapper(label, should_print, func):
48+
def maybe_print(str, *args, **kwargs):
49+
if should_print:
50+
print(str, *args, **kwargs)
51+
sys.stdout.flush()
52+
53+
maybe_print(f"{label} ", end="")
54+
55+
try:
56+
result = func()
57+
maybe_print(f"{Fore.GREEN}Success{Fore.RESET}")
58+
return result
59+
except Exception as e:
60+
maybe_print(f"{Fore.RED}Error{Fore.RESET}")
61+
raise e
62+
63+
4064
if __name__ == "__main__":
4165
args = parse_args()
4266

src/api.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,13 @@ def __get(self, action, **params):
8181
else:
8282
sleep(0.2)
8383

84-
handle_error(description="Maximum number of retries reached", should_exit=True)
84+
handle_error(description="Maximum number of retries reached", should_raise=True)
8585

8686
def __get_announce_url(self):
8787
try:
8888
account_info = self.get_account_info()
8989
except AuthenticationError as e:
90-
handle_error(description=f"Authentication to {self.sitename} failed", exception_details=e, should_exit=True)
90+
handle_error(description=f"Authentication to {self.sitename} failed", exception_details=e, should_raise=True)
9191

9292
passkey = account_info["response"]["passkey"]
9393
return f"{self.tracker_url}/{passkey}/announce"

src/errors.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import sys
21
from time import sleep
32

43
from colorama import Fore
@@ -9,17 +8,17 @@ def handle_error(
98
exception_details: (str | None) = None,
109
wait_time: int = 0,
1110
extra_description: str = "",
12-
should_exit: bool = False,
11+
should_raise: bool = False,
1312
) -> None:
14-
action = "Exiting" if should_exit else "Retrying"
15-
action += f" in {wait_time} seconds..." if wait_time else "..."
13+
action = "" if should_raise else "Retrying"
14+
action += f" in {wait_time} seconds..." if wait_time else ""
1615
exception_message = f"\n{Fore.LIGHTBLACK_EX}{exception_details}" if exception_details is not None else ""
1716

18-
print(f"{Fore.RED}Error: {description}{extra_description}. {action}{exception_message}{Fore.RESET}")
19-
sleep(wait_time)
20-
21-
if should_exit:
22-
sys.exit(1)
17+
if should_raise:
18+
raise Exception(f"{description}{extra_description}. {action}{exception_message}{Fore.RESET}")
19+
else:
20+
print(f"{Fore.RED}Error: {description}{extra_description}. {action}{exception_message}{Fore.RESET}")
21+
sleep(wait_time)
2322

2423

2524
class AuthenticationError(Exception):

0 commit comments

Comments
 (0)