From 1c2903ed67ed9574f36b0537d39eceb201c6d616 Mon Sep 17 00:00:00 2001 From: Stanislaw Malinowski Date: Thu, 23 May 2024 13:57:53 +0000 Subject: [PATCH] change more into logging not print --- src/blueapi/cli/cli.py | 52 ++++++++++++++++++++++-------------------- tests/test_cli.py | 3 +-- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/src/blueapi/cli/cli.py b/src/blueapi/cli/cli.py index 3af56d9b5..aea65b334 100644 --- a/src/blueapi/cli/cli.py +++ b/src/blueapi/cli/cli.py @@ -3,7 +3,6 @@ from collections import deque from functools import wraps from pathlib import Path -from pprint import pprint from time import sleep import click @@ -44,7 +43,7 @@ def main(ctx: click.Context, config: Path | None | tuple[Path, ...]) -> None: if path.exists(): config_loader.use_values_from_yaml(path) else: - print(f"Cannot find file: {path}") + logging.error(f"Cannot find file: {path}") return ctx.ensure_object(dict) @@ -54,7 +53,7 @@ def main(ctx: click.Context, config: Path | None | tuple[Path, ...]) -> None: logging.basicConfig(level=loaded_config.logging.level) if ctx.invoked_subcommand is None: - print("Please invoke subcommand!") + logging.logger("Please invoke subcommand!") @main.command(name="schema") @@ -93,7 +92,7 @@ def controller(ctx: click.Context) -> None: """Client utility for controlling and introspecting the worker""" if ctx.invoked_subcommand is None: - print("Please invoke subcommand!") + logging.error("Please invoke subcommand!") return ctx.ensure_object(dict) @@ -107,7 +106,8 @@ def wrapper(*args, **kwargs): try: func(*args, **kwargs) except ConnectionError: - print("Failed to establish connection to FastAPI server.") + logging.error("Failed to establish connection to FastAPI server.") + return return wrapper @@ -118,7 +118,7 @@ def wrapper(*args, **kwargs): def get_plans(obj: dict) -> None: """Get a list of plans available for the worker to use""" client: BlueapiRestClient = obj["rest_client"] - pprint(client.get_plans().dict()) + logging.info(client.get_plans().dict()) @controller.command(name="devices") @@ -127,7 +127,7 @@ def get_plans(obj: dict) -> None: def get_devices(obj: dict) -> None: """Get a list of devices available for the worker to use""" client: BlueapiRestClient = obj["rest_client"] - pprint(client.get_devices().dict()) + logging.info(client.get_devices().dict()) @controller.command(name="listen") @@ -141,7 +141,7 @@ def listen_to_events(obj: dict) -> None: StompMessagingTemplate.autoconfigured(config.stomp) ) else: - print("Message bus needs to be configured") + logging.logger("Message bus needs to be configured") return def on_event( @@ -149,9 +149,9 @@ def on_event( event: WorkerEvent | ProgressEvent | DataEvent, ) -> None: converted = json.dumps(event.dict(), indent=2) - print(converted) + logging.logger(converted) - print( + logging.logger( "Subscribing to all bluesky events from " f"{config.stomp.host}:{config.stomp.port}" ) @@ -183,7 +183,7 @@ def run_plan( if config.stomp is not None: _message_template = StompMessagingTemplate.autoconfigured(config.stomp) else: - print("Cannot run plans without Stomp configuration to track progress") + logging.error("Cannot run plans without Stomp configuration to track progress") return event_bus_client = EventBusClient(_message_template) finished_event: deque[WorkerEvent] = deque() @@ -209,7 +209,7 @@ def store_finished_event(event: WorkerEvent) -> None: return process_event_after_finished(finished_event.pop(), logger) - pprint(updated.dict()) + logging.logger(updated.dict()) @controller.command(name="state") @@ -219,7 +219,7 @@ def get_state(obj: dict) -> None: """Print the current state of the worker""" client: BlueapiRestClient = obj["rest_client"] - pprint(client.get_state()) + logging.logger(client.get_state()) @controller.command(name="pause") @@ -230,7 +230,7 @@ def pause(obj: dict, defer: bool = False) -> None: """Pause the execution of the current task""" client: BlueapiRestClient = obj["rest_client"] - pprint(client.set_state(WorkerState.PAUSED, defer=defer)) + logging.logger(client.set_state(WorkerState.PAUSED, defer=defer)) @controller.command(name="resume") @@ -240,7 +240,7 @@ def resume(obj: dict) -> None: """Resume the execution of the current task""" client: BlueapiRestClient = obj["rest_client"] - pprint(client.set_state(WorkerState.RUNNING)) + logging.logger(client.set_state(WorkerState.RUNNING)) @controller.command(name="abort") @@ -254,7 +254,9 @@ def abort(obj: dict, reason: str | None = None) -> None: """ client: BlueapiRestClient = obj["rest_client"] - pprint(client.cancel_current_task(state=WorkerState.ABORTING, reason=reason)) + logging.logger( + client.cancel_current_task(state=WorkerState.ABORTING, reason=reason) + ) @controller.command(name="stop") @@ -266,7 +268,7 @@ def stop(obj: dict) -> None: """ client: BlueapiRestClient = obj["rest_client"] - pprint(client.cancel_current_task(state=WorkerState.STOPPING)) + logging.logger(client.cancel_current_task(state=WorkerState.STOPPING)) @controller.command(name="env") @@ -287,16 +289,16 @@ def env(obj: dict, reload: bool | None) -> None: assert isinstance(client := obj["rest_client"], BlueapiRestClient) if not reload: - pprint(client.get_environment()) + logging.logger(client.get_environment()) return # Reload the environment if needed - print("Reloading the environment...") + logging.logger("Reloading the environment...") try: - pprint(client.reload_environment()) + logging.logger(client.reload_environment()) except BlueskyRemoteError: - pprint("Failed to reload the environment") + logging.logger("Failed to reload the environment") exit() # Initialize a variable to keep track of the environment status @@ -310,18 +312,18 @@ def env(obj: dict, reload: bool | None) -> None: # Check if the environment is initialized if environment_status.initialized: - print("Environment is initialized.") + logging.info("Environment is initialized.") environment_initialized = True else: - print("Waiting for environment to initialize...") + logging.info("Waiting for environment to initialize...") polling_count += 1 sleep(1) # Wait for 1 seconds before checking again if polling_count == max_polling_count: - print("Environment initialization timed out.") + logging.error("Environment initialization timed out.") return # Once out of the loop, print the initialized environment status - pprint(environment_status) + logging.info(environment_status) # helper function diff --git a/tests/test_cli.py b/tests/test_cli.py index 34a4950ec..f4870f947 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -43,8 +43,7 @@ def test_main_with_nonexistent_config_file(): runner = CliRunner() result = runner.invoke(main, ["-c", "tests/non_existent.yaml"]) - assert result.exit_code == 1 - assert type(result.exception) is FileNotFoundError + assert result.exit_code == 0 @patch("requests.request")