From e9fec48ede4599d82ce064e5f57d0dfc4379b1c8 Mon Sep 17 00:00:00 2001 From: Szymon Borecki Date: Wed, 9 Aug 2023 23:51:12 +0200 Subject: [PATCH] Fix docstring for expect_response and rename the associated exception --- software/script/chameleon_cli_main.py | 2 +- software/script/chameleon_cmd.py | 2 +- software/script/chameleon_utils.py | 14 +++++++++----- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/software/script/chameleon_cli_main.py b/software/script/chameleon_cli_main.py index 082ad1d..aec9d08 100755 --- a/software/script/chameleon_cli_main.py +++ b/software/script/chameleon_cli_main.py @@ -217,7 +217,7 @@ class ChameleonCLI: continue # start process cmd unit.on_exec(args_parse_result) - except (chameleon_cmd.NegativeResponseError, chameleon_cli_unit.ArgsParserError) as e: + except (chameleon_cmd.UnexpectedResponseError, chameleon_cli_unit.ArgsParserError) as e: print(f"{colorama.Fore.RED}{str(e)}{colorama.Style.RESET_ALL}") except Exception: print(f"CLI exception: {colorama.Fore.RED}{traceback.format_exc()}{colorama.Style.RESET_ALL}") diff --git a/software/script/chameleon_cmd.py b/software/script/chameleon_cmd.py index 474cd34..f7ae20d 100644 --- a/software/script/chameleon_cmd.py +++ b/software/script/chameleon_cmd.py @@ -2,7 +2,7 @@ import enum import chameleon_com import chameleon_status -from chameleon_utils import NegativeResponseError, expect_response +from chameleon_utils import UnexpectedResponseError, expect_response DATA_CMD_GET_APP_VERSION = 1000 DATA_CMD_CHANGE_MODE = 1001 diff --git a/software/script/chameleon_utils.py b/software/script/chameleon_utils.py index 219f205..8cbfee2 100644 --- a/software/script/chameleon_utils.py +++ b/software/script/chameleon_utils.py @@ -4,12 +4,16 @@ from typing import Union import chameleon_status -class NegativeResponseError(Exception): +class UnexpectedResponseError(Exception): """ - Not positive response + Unexpected response exception """ -def expect_response(accepted_responses): +def expect_response(accepted_responses: Union[int, list[int]]): + """ + Decorator for wrapping a Chameleon CMD function to check its response + for expected return codes and throwing an exception otherwise + """ if isinstance(accepted_responses, int): accepted_responses = [accepted_responses] @@ -20,9 +24,9 @@ def expect_response(accepted_responses): if ret.status not in accepted_responses: if ret.status in chameleon_status.Device and ret.status in chameleon_status.message: - raise NegativeResponseError(chameleon_status.message[ret.status]) + raise UnexpectedResponseError(chameleon_status.message[ret.status]) else: - raise NegativeResponseError(f"Not positive response and unknown status {ret.status}") + raise UnexpectedResponseError(f"Unexpected response and unknown status {ret.status}") return ret