From 54263e5d524364d7c91277ef17859f97239feb98 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Mon, 30 Oct 2023 10:39:27 -0700 Subject: [PATCH] ruff: Fix N818 Exception name should be named with an Error suffix. Signed-off-by: Anders Kaseorg --- .../bridge_with_matrix/matrix_bridge.py | 54 +++++++++---------- .../bots/baremetrics/test_baremetrics.py | 2 +- .../bots/beeminder/test_beeminder.py | 2 +- .../bots/connect_four/controller.py | 4 +- .../bots/connect_four/test_connect_four.py | 4 +- .../bots/game_handler_bot/game_handler_bot.py | 4 +- .../bots/game_of_fifteen/game_of_fifteen.py | 10 ++-- .../game_of_fifteen/test_game_of_fifteen.py | 12 ++--- zulip_bots/zulip_bots/bots/giphy/giphy.py | 6 +-- .../google_translate/test_google_translate.py | 4 +- .../zulip_bots/bots/idonethis/idonethis.py | 32 +++++------ .../zulip_bots/bots/incident/incident.py | 8 +-- .../link_shortener/test_link_shortener.py | 2 +- zulip_bots/zulip_bots/bots/mention/mention.py | 6 +-- .../zulip_bots/bots/mention/test_mention.py | 2 +- .../zulip_bots/bots/merels/libraries/game.py | 12 ++--- .../bots/merels/libraries/mechanics.py | 8 +-- zulip_bots/zulip_bots/bots/merels/merels.py | 4 +- .../zulip_bots/bots/merels/test/test_game.py | 14 ++--- .../bots/monkeytestit/monkeytestit.py | 4 +- .../bots/salesforce/test_salesforce.py | 2 +- .../zulip_bots/bots/tictactoe/tictactoe.py | 6 +-- .../zulip_bots/bots/trello/test_trello.py | 2 +- .../bots/trivia_quiz/trivia_quiz.py | 16 +++--- .../zulip_bots/bots/youtube/test_youtube.py | 2 +- zulip_bots/zulip_bots/finder.py | 4 +- zulip_bots/zulip_bots/game_handler.py | 8 +-- zulip_bots/zulip_bots/lib.py | 4 +- zulip_bots/zulip_bots/run.py | 6 +-- zulip_bots/zulip_bots/test_lib.py | 4 +- 30 files changed, 124 insertions(+), 124 deletions(-) diff --git a/zulip/integrations/bridge_with_matrix/matrix_bridge.py b/zulip/integrations/bridge_with_matrix/matrix_bridge.py index 152e1888..9ff08d27 100755 --- a/zulip/integrations/bridge_with_matrix/matrix_bridge.py +++ b/zulip/integrations/bridge_with_matrix/matrix_bridge.py @@ -35,15 +35,15 @@ ZULIP_MESSAGE_TEMPLATE: str = "**{username}** [{uid}]: {message}" MATRIX_MESSAGE_TEMPLATE: str = "<{username} ({uid})> {message}" -class BridgeConfigException(Exception): +class BridgeConfigError(Exception): pass -class BridgeFatalMatrixException(Exception): +class BridgeFatalMatrixError(Exception): pass -class BridgeFatalZulipException(Exception): +class BridgeFatalZulipError(Exception): pass @@ -86,7 +86,7 @@ class MatrixToZulip: # the new messages and not with all the old ones. sync_response: Union[SyncResponse, SyncError] = await matrix_client.sync() if isinstance(sync_response, nio.SyncError): - raise BridgeFatalMatrixException(sync_response.message) + raise BridgeFatalMatrixError(sync_response.message) return matrix_to_zulip @@ -117,10 +117,10 @@ class MatrixToZulip: ) except Exception as exception: # Generally raised when user is forbidden - raise BridgeFatalZulipException(exception) + raise BridgeFatalZulipError(exception) if result["result"] != "success": # Generally raised when API key is invalid - raise BridgeFatalZulipException(result["msg"]) + raise BridgeFatalZulipError(result["msg"]) # Update the bot's read marker in order to show the other users which # messages are already processed by the bot. @@ -194,14 +194,14 @@ class MatrixToZulip: for room_id in self.matrix_config["bridges"]: result: Union[JoinResponse, JoinError] = await self.matrix_client.join(room_id) if isinstance(result, nio.JoinError): - raise BridgeFatalMatrixException(str(result)) + raise BridgeFatalMatrixError(str(result)) async def matrix_login(self) -> None: result: Union[LoginResponse, LoginError] = await self.matrix_client.login( self.matrix_config["password"] ) if isinstance(result, nio.LoginError): - raise BridgeFatalMatrixException(str(result)) + raise BridgeFatalMatrixError(str(result)) async def run(self) -> None: print("Starting message handler on Matrix client") @@ -230,7 +230,7 @@ class ZulipToMatrix: # Precompute the url of the Zulip server, needed later. result: Dict[str, Any] = self.zulip_client.get_server_settings() if result["result"] != "success": - raise BridgeFatalZulipException("cannot get server settings") + raise BridgeFatalZulipError("cannot get server settings") self.server_url: str = result["realm_uri"] @classmethod @@ -250,7 +250,7 @@ class ZulipToMatrix: self.matrix_client.room_send(**kwargs), self.loop ).result() if isinstance(result, nio.RoomSendError): - raise BridgeFatalMatrixException(str(result)) + raise BridgeFatalMatrixError(str(result)) def _zulip_to_matrix(self, msg: Dict[str, Any]) -> None: logging.debug("_zulip_to_matrix; msg: %s", msg) @@ -303,12 +303,12 @@ class ZulipToMatrix: for stream, _ in self.zulip_config["bridges"]: result: Dict[str, Any] = self.zulip_client.get_stream_id(stream) if result["result"] == "error": - raise BridgeFatalZulipException(f"cannot access stream '{stream}': {result}") + raise BridgeFatalZulipError(f"cannot access stream '{stream}': {result}") if result["result"] != "success": - raise BridgeFatalZulipException(f"cannot checkout stream id for stream '{stream}'") + raise BridgeFatalZulipError(f"cannot checkout stream id for stream '{stream}'") result = self.zulip_client.add_subscriptions(streams=[{"name": stream}]) if result["result"] != "success": - raise BridgeFatalZulipException(f"cannot subscribe to stream '{stream}': {result}") + raise BridgeFatalZulipError(f"cannot subscribe to stream '{stream}': {result}") def get_matrix_room_for_zulip_message(self, msg: Dict[str, Any]) -> Optional[str]: """Check whether we want to process the given message. @@ -459,10 +459,10 @@ def read_configuration(config_file: str) -> Dict[str, Dict[str, Any]]: try: config.read(config_file) except configparser.Error as exception: - raise BridgeConfigException(str(exception)) + raise BridgeConfigError(str(exception)) if set(config.sections()) < {"matrix", "zulip"}: - raise BridgeConfigException("Please ensure the configuration has zulip & matrix sections.") + raise BridgeConfigError("Please ensure the configuration has zulip & matrix sections.") result: Dict[str, Dict[str, Any]] = {"matrix": {}, "zulip": {}} # For Matrix: create a mapping with the Matrix room_ids as keys and @@ -484,7 +484,7 @@ def read_configuration(config_file: str) -> Dict[str, Dict[str, Any]]: if section.startswith("additional_bridge"): if section_keys != bridge_key_set: - raise BridgeConfigException( + raise BridgeConfigError( f"Please ensure the bridge configuration section {section} contain the following keys: {bridge_key_set}." ) @@ -493,7 +493,7 @@ def read_configuration(config_file: str) -> Dict[str, Dict[str, Any]]: result["matrix"]["bridges"][section_config["room_id"]] = zulip_target elif section == "matrix": if section_keys != matrix_full_key_set: - raise BridgeConfigException( + raise BridgeConfigError( "Please ensure the matrix configuration section contains the following keys: %s." % str(matrix_full_key_set) ) @@ -505,10 +505,10 @@ def read_configuration(config_file: str) -> Dict[str, Dict[str, Any]]: # Verify the format of the Matrix user ID. if re.fullmatch(r"@[^:]+:.+", result["matrix"]["mxid"]) is None: - raise BridgeConfigException("Malformatted mxid.") + raise BridgeConfigError("Malformatted mxid.") elif section == "zulip": if section_keys != zulip_full_key_set: - raise BridgeConfigException( + raise BridgeConfigError( "Please ensure the zulip configuration section contains the following keys: %s." % str(zulip_full_key_set) ) @@ -555,9 +555,9 @@ async def run(zulip_config: Dict[str, Any], matrix_config: Dict[str, Any], no_no await asyncio.gather(matrix_to_zulip.run(), zulip_to_matrix.run()) - except BridgeFatalMatrixException as exception: + except BridgeFatalMatrixError as exception: sys.exit(f"Matrix bridge error: {exception}") - except BridgeFatalZulipException as exception: + except BridgeFatalZulipError as exception: sys.exit(f"Zulip bridge error: {exception}") except zulip.ZulipError as exception: sys.exit(f"Zulip error: {exception}") @@ -571,7 +571,7 @@ async def run(zulip_config: Dict[str, Any], matrix_config: Dict[str, Any], no_no def write_sample_config(target_path: str, zuliprc: Optional[str]) -> None: if os.path.exists(target_path): - raise BridgeConfigException(f"Path '{target_path}' exists; not overwriting existing file.") + raise BridgeConfigError(f"Path '{target_path}' exists; not overwriting existing file.") sample_dict: OrderedDict[str, OrderedDict[str, str]] = OrderedDict( ( @@ -613,20 +613,20 @@ def write_sample_config(target_path: str, zuliprc: Optional[str]) -> None: if zuliprc is not None: if not os.path.exists(zuliprc): - raise BridgeConfigException(f"Zuliprc file '{zuliprc}' does not exist.") + raise BridgeConfigError(f"Zuliprc file '{zuliprc}' does not exist.") zuliprc_config: configparser.ConfigParser = configparser.ConfigParser() try: zuliprc_config.read(zuliprc) except configparser.Error as exception: - raise BridgeConfigException(str(exception)) + raise BridgeConfigError(str(exception)) try: sample_dict["zulip"]["email"] = zuliprc_config["api"]["email"] sample_dict["zulip"]["site"] = zuliprc_config["api"]["site"] sample_dict["zulip"]["api_key"] = zuliprc_config["api"]["key"] except KeyError as exception: - raise BridgeConfigException("You provided an invalid zuliprc file: " + str(exception)) + raise BridgeConfigError("You provided an invalid zuliprc file: " + str(exception)) sample: configparser.ConfigParser = configparser.ConfigParser() sample.read_dict(sample_dict) @@ -648,7 +648,7 @@ def main() -> None: if options.sample_config: try: write_sample_config(options.sample_config, options.zuliprc) - except BridgeConfigException as exception: + except BridgeConfigError as exception: print(f"Could not write sample config: {exception}") sys.exit(1) if options.zuliprc is None: @@ -667,7 +667,7 @@ def main() -> None: try: config: Dict[str, Dict[str, Any]] = read_configuration(options.config) - except BridgeConfigException as exception: + except BridgeConfigError as exception: print(f"Could not parse config file: {exception}") sys.exit(1) diff --git a/zulip_bots/zulip_bots/bots/baremetrics/test_baremetrics.py b/zulip_bots/zulip_bots/bots/baremetrics/test_baremetrics.py index 792a6fa9..ad1a242d 100644 --- a/zulip_bots/zulip_bots/bots/baremetrics/test_baremetrics.py +++ b/zulip_bots/zulip_bots/bots/baremetrics/test_baremetrics.py @@ -98,7 +98,7 @@ class TestBaremetricsBot(BotTestCase, DefaultTests): with self.mock_config_info({"api_key": "TEST"}): with self.mock_http_conversation("invalid_api_key"): - with self.assertRaises(StubBotHandler.BotQuitException): + with self.assertRaises(StubBotHandler.BotQuitError): bot_test_instance.initialize(StubBotHandler()) def test_invalid_command(self) -> None: diff --git a/zulip_bots/zulip_bots/bots/beeminder/test_beeminder.py b/zulip_bots/zulip_bots/bots/beeminder/test_beeminder.py index f8af54a1..6d4b6a17 100644 --- a/zulip_bots/zulip_bots/bots/beeminder/test_beeminder.py +++ b/zulip_bots/zulip_bots/bots/beeminder/test_beeminder.py @@ -97,7 +97,7 @@ right now.\nPlease try again later", with self.mock_config_info( {"auth_token": "someInvalidKey", "username": "aaron", "goalname": "goal"} ), self.mock_http_conversation("test_invalid_when_initialize"), self.assertRaises( - bot_handler.BotQuitException + bot_handler.BotQuitError ): bot.initialize(bot_handler) diff --git a/zulip_bots/zulip_bots/bots/connect_four/controller.py b/zulip_bots/zulip_bots/bots/connect_four/controller.py index fca98dbd..b8e49a04 100644 --- a/zulip_bots/zulip_bots/bots/connect_four/controller.py +++ b/zulip_bots/zulip_bots/bots/connect_four/controller.py @@ -2,7 +2,7 @@ from copy import deepcopy from functools import reduce from typing import List -from zulip_bots.game_handler import BadMoveException +from zulip_bots.game_handler import BadMoveError class ConnectFourModel: @@ -61,7 +61,7 @@ class ConnectFourModel: while finding_move: if row < 0: - raise BadMoveException("Make sure your move is in a column with free space.") + raise BadMoveError("Make sure your move is in a column with free space.") if self.current_board[row][column] == 0: self.current_board[row][column] = token_number finding_move = False diff --git a/zulip_bots/zulip_bots/bots/connect_four/test_connect_four.py b/zulip_bots/zulip_bots/bots/connect_four/test_connect_four.py index fbc2e396..d1a7ed2c 100644 --- a/zulip_bots/zulip_bots/bots/connect_four/test_connect_four.py +++ b/zulip_bots/zulip_bots/bots/connect_four/test_connect_four.py @@ -3,7 +3,7 @@ from typing import Dict, List from typing_extensions import override from zulip_bots.bots.connect_four.controller import ConnectFourModel -from zulip_bots.game_handler import BadMoveException +from zulip_bots.game_handler import BadMoveError from zulip_bots.test_lib import BotTestCase, DefaultTests @@ -583,5 +583,5 @@ The first player to get 4 in a row wins!\n Good Luck!", self.assertEqual(model.get_column(col), [0, -1, -1, -1, 1, 1]) model.make_move(move, player_number=0) self.assertEqual(model.get_column(col), [1, -1, -1, -1, 1, 1]) - with self.assertRaises(BadMoveException): + with self.assertRaises(BadMoveError): model.make_move(move, player_number=0) diff --git a/zulip_bots/zulip_bots/bots/game_handler_bot/game_handler_bot.py b/zulip_bots/zulip_bots/bots/game_handler_bot/game_handler_bot.py index c8c0b7f2..d9c6538e 100644 --- a/zulip_bots/zulip_bots/bots/game_handler_bot/game_handler_bot.py +++ b/zulip_bots/zulip_bots/bots/game_handler_bot/game_handler_bot.py @@ -1,6 +1,6 @@ from typing import Any, List -from zulip_bots.game_handler import BadMoveException, GameAdapter +from zulip_bots.game_handler import BadMoveError, GameAdapter class GameHandlerBotMessageHandler: @@ -31,7 +31,7 @@ class MockModel: if int(move.replace("move ", "")) < 9: return "mock board" else: - raise BadMoveException("Invalid Move.") + raise BadMoveError("Invalid Move.") return "mock board" def determine_game_over(self, players: List[str]) -> None: diff --git a/zulip_bots/zulip_bots/bots/game_of_fifteen/game_of_fifteen.py b/zulip_bots/zulip_bots/bots/game_of_fifteen/game_of_fifteen.py index a370e5d4..864217e3 100644 --- a/zulip_bots/zulip_bots/bots/game_of_fifteen/game_of_fifteen.py +++ b/zulip_bots/zulip_bots/bots/game_of_fifteen/game_of_fifteen.py @@ -1,7 +1,7 @@ import copy from typing import Any, Dict, List, Tuple -from zulip_bots.game_handler import BadMoveException, GameAdapter +from zulip_bots.game_handler import BadMoveError, GameAdapter class GameOfFifteenModel: @@ -54,13 +54,13 @@ class GameOfFifteenModel: move = move.split(" ") if "" in move: - raise BadMoveException("You should enter space separated digits.") + raise BadMoveError("You should enter space separated digits.") moves = len(move) for m in range(1, moves): tile = int(move[m]) coordinates = self.get_coordinates(board) if tile not in coordinates: - raise BadMoveException("You can only move tiles which exist in the board.") + raise BadMoveError("You can only move tiles which exist in the board.") i, j = coordinates[tile] if (j - 1) > -1 and board[i][j - 1] == 0: board[i][j - 1] = tile @@ -75,9 +75,7 @@ class GameOfFifteenModel: board[i + 1][j] = tile board[i][j] = 0 else: - raise BadMoveException( - "You can only move tiles which are adjacent to :grey_question:." - ) + raise BadMoveError("You can only move tiles which are adjacent to :grey_question:.") if m == moves - 1: return board diff --git a/zulip_bots/zulip_bots/bots/game_of_fifteen/test_game_of_fifteen.py b/zulip_bots/zulip_bots/bots/game_of_fifteen/test_game_of_fifteen.py index 541601f3..c4ac3c6b 100644 --- a/zulip_bots/zulip_bots/bots/game_of_fifteen/test_game_of_fifteen.py +++ b/zulip_bots/zulip_bots/bots/game_of_fifteen/test_game_of_fifteen.py @@ -1,7 +1,7 @@ from typing import Dict, List, Tuple from zulip_bots.bots.game_of_fifteen.game_of_fifteen import GameOfFifteenModel -from zulip_bots.game_handler import BadMoveException +from zulip_bots.game_handler import BadMoveError from zulip_bots.test_lib import BotTestCase, DefaultTests @@ -158,13 +158,13 @@ class TestGameOfFifteenBot(BotTestCase, DefaultTests): initial_board = [[8, 7, 6], [5, 4, 3], [2, 1, 0]] model.update_board(initial_board) - with self.assertRaises(BadMoveException): + with self.assertRaises(BadMoveError): model.make_move(move1, player_number=0) - with self.assertRaises(BadMoveException): + with self.assertRaises(BadMoveError): model.make_move(move2, player_number=0) - with self.assertRaises(BadMoveException): + with self.assertRaises(BadMoveError): model.make_move(move3, player_number=0) - with self.assertRaises(BadMoveException): + with self.assertRaises(BadMoveError): model.make_move(move4, player_number=0) - with self.assertRaises(BadMoveException): + with self.assertRaises(BadMoveError): model.make_move(move5, player_number=0) diff --git a/zulip_bots/zulip_bots/bots/giphy/giphy.py b/zulip_bots/zulip_bots/bots/giphy/giphy.py index f5eb3599..9669341c 100644 --- a/zulip_bots/zulip_bots/bots/giphy/giphy.py +++ b/zulip_bots/zulip_bots/bots/giphy/giphy.py @@ -52,7 +52,7 @@ class GiphyHandler: bot_handler.send_reply(message, bot_response) -class GiphyNoResultException(Exception): +class GiphyNoResultError(Exception): pass @@ -77,7 +77,7 @@ def get_url_gif_giphy(keyword: str, api_key: str) -> Union[int, str]: try: gif_url = data.json()["data"]["images"]["original"]["url"] except (TypeError, KeyError): # Usually triggered by no result in Giphy. - raise GiphyNoResultException + raise GiphyNoResultError return gif_url @@ -95,7 +95,7 @@ def get_bot_giphy_response( "cannot process your request right now. But, " "let's try again later! :grin:" ) - except GiphyNoResultException: + except GiphyNoResultError: return f'Sorry, I don\'t have a GIF for "{keyword}"! :astonished:' return ( f"[Click to enlarge]({gif_url})" diff --git a/zulip_bots/zulip_bots/bots/google_translate/test_google_translate.py b/zulip_bots/zulip_bots/bots/google_translate/test_google_translate.py index f40ef6f5..3369d50f 100644 --- a/zulip_bots/zulip_bots/bots/google_translate/test_google_translate.py +++ b/zulip_bots/zulip_bots/bots/google_translate/test_google_translate.py @@ -84,11 +84,11 @@ class TestGoogleTranslateBot(BotTestCase, DefaultTests): self._test('"hello" de', "Error. .", "test_languages") def test_invalid_api_key(self): - with self.assertRaises(StubBotHandler.BotQuitException): + with self.assertRaises(StubBotHandler.BotQuitError): self._test(None, None, "test_invalid_api_key") def test_api_access_not_configured(self): - with self.assertRaises(StubBotHandler.BotQuitException): + with self.assertRaises(StubBotHandler.BotQuitError): self._test(None, None, "test_api_access_not_configured") def test_connection_error(self): diff --git a/zulip_bots/zulip_bots/bots/idonethis/idonethis.py b/zulip_bots/zulip_bots/bots/idonethis/idonethis.py index 4575b7be..12bf60c0 100644 --- a/zulip_bots/zulip_bots/bots/idonethis/idonethis.py +++ b/zulip_bots/zulip_bots/bots/idonethis/idonethis.py @@ -12,21 +12,21 @@ api_key = "" default_team = "" -class AuthenticationException(Exception): +class AuthenticationError(Exception): pass -class TeamNotFoundException(Exception): +class TeamNotFoundError(Exception): def __init__(self, team: str) -> None: self.team = team -class UnknownCommandSyntax(Exception): +class UnknownCommandSyntaxError(Exception): def __init__(self, detail: str) -> None: self.detail = detail -class UnspecifiedProblemException(Exception): +class UnspecifiedProblemError(Exception): pass @@ -49,10 +49,10 @@ def make_api_request( and r.json()["error"] == "Invalid API Authentication" ): logging.error("Error authenticating, please check key %s", r.url) - raise AuthenticationException + raise AuthenticationError else: logging.error("Error make API request, code %s. json: %s", r.status_code, r.json()) - raise UnspecifiedProblemException + raise UnspecifiedProblemError def api_noop() -> None: @@ -92,7 +92,7 @@ def get_team_hash(team_name: str) -> str: for team in api_list_team(): if team["name"].lower() == team_name.lower() or team["hash_id"] == team_name: return team["hash_id"] - raise TeamNotFoundException(team_name) + raise TeamNotFoundError(team_name) def team_info(team_name: str) -> str: @@ -133,7 +133,7 @@ def create_entry(message: str) -> str: team = default_team new_message = message else: - raise UnknownCommandSyntax( + raise UnknownCommandSyntaxError( """I don't know which team you meant for me to create an entry under. Either set a default team or pass the `--team` flag. More information in my help""" @@ -166,12 +166,12 @@ class IDoneThisHandler: try: api_noop() - except AuthenticationException: + except AuthenticationError: logging.error( "Authentication exception with idonethis. Can you check that your API keys are correct? " ) bot_handler.quit() - except UnspecifiedProblemException: + except UnspecifiedProblemError: logging.error("Problem connecting to idonethis. Please check connection") bot_handler.quit() @@ -219,7 +219,7 @@ Below are some of the commands you can use, and what they do. if len(message_content) > 2: reply = team_info(" ".join(message_content[2:])) else: - raise UnknownCommandSyntax( + raise UnknownCommandSyntaxError( "You must specify the team in which you request information from." ) elif command in ["entries list", "list entries"]: @@ -229,15 +229,17 @@ Below are some of the commands you can use, and what they do. elif command in ["help"]: reply = self.usage() else: - raise UnknownCommandSyntax("I can't understand the command you sent me :confused: ") - except TeamNotFoundException as e: + raise UnknownCommandSyntaxError( + "I can't understand the command you sent me :confused: " + ) + except TeamNotFoundError as e: reply = ( "Sorry, it doesn't seem as if I can find a team named `" + e.team + "` :frowning:." ) - except AuthenticationException: + except AuthenticationError: reply = "I can't currently authenticate with idonethis. " reply += "Can you check that your API key is correct? For more information see my documentation." - except UnknownCommandSyntax as e: + except UnknownCommandSyntaxError as e: reply = ( "Sorry, I don't understand what your trying to say. Use `@mention help` to see my help. " + e.detail diff --git a/zulip_bots/zulip_bots/bots/incident/incident.py b/zulip_bots/zulip_bots/bots/incident/incident.py index 66dca582..9bfa3fca 100644 --- a/zulip_bots/zulip_bots/bots/incident/incident.py +++ b/zulip_bots/zulip_bots/bots/incident/incident.py @@ -14,7 +14,7 @@ ANSWERS = { } -class InvalidAnswerException(Exception): +class InvalidAnswerError(Exception): pass @@ -35,7 +35,7 @@ class IncidentHandler: elif query.startswith("answer "): try: (ticket_id, answer) = parse_answer(query) - except InvalidAnswerException: + except InvalidAnswerError: bot_response = "Invalid answer format" bot_handler.send_reply(message, bot_response) return @@ -63,7 +63,7 @@ def start_new_incident(query: str, message: Dict[str, Any], bot_handler: BotHand def parse_answer(query: str) -> Tuple[str, str]: m = re.match(r"answer\s+(TICKET....)\s+(.)", query) if not m: - raise InvalidAnswerException + raise InvalidAnswerError ticket_id = m.group(1) @@ -74,7 +74,7 @@ def parse_answer(query: str) -> Tuple[str, str]: answer = m.group(2).upper() if answer not in "1234": - raise InvalidAnswerException + raise InvalidAnswerError return (ticket_id, ANSWERS[answer]) diff --git a/zulip_bots/zulip_bots/bots/link_shortener/test_link_shortener.py b/zulip_bots/zulip_bots/bots/link_shortener/test_link_shortener.py index ea72bf05..2b792a76 100644 --- a/zulip_bots/zulip_bots/bots/link_shortener/test_link_shortener.py +++ b/zulip_bots/zulip_bots/bots/link_shortener/test_link_shortener.py @@ -65,5 +65,5 @@ class TestLinkShortenerBot(BotTestCase, DefaultTests): bot_test_instance = LinkShortenerHandler() with self.mock_config_info({"key": "qwertyuiopx"}): with self.mock_http_conversation("test_invalid_access_token"): - with self.assertRaises(StubBotHandler.BotQuitException): + with self.assertRaises(StubBotHandler.BotQuitError): bot_test_instance.initialize(StubBotHandler()) diff --git a/zulip_bots/zulip_bots/bots/mention/mention.py b/zulip_bots/zulip_bots/bots/mention/mention.py index 98668d17..09275c49 100644 --- a/zulip_bots/zulip_bots/bots/mention/mention.py +++ b/zulip_bots/zulip_bots/bots/mention/mention.py @@ -116,13 +116,13 @@ class MentionHandler: alert_id = self.get_alert_id(keyword) except (TypeError, KeyError): # Usually triggered by invalid token or json parse error when account quote is finished. - raise MentionNoResponseException + raise MentionNoResponseError try: mentions = self.get_mentions(alert_id) except (TypeError, KeyError): # Usually triggered by no response or json parse error when account quota is finished. - raise MentionNoResponseException + raise MentionNoResponseError reply = "The most recent mentions of `" + keyword + "` on the web are: \n" for mention in mentions: @@ -133,5 +133,5 @@ class MentionHandler: handler_class = MentionHandler -class MentionNoResponseException(Exception): +class MentionNoResponseError(Exception): pass diff --git a/zulip_bots/zulip_bots/bots/mention/test_mention.py b/zulip_bots/zulip_bots/bots/mention/test_mention.py index 16c799b8..c4b601a0 100644 --- a/zulip_bots/zulip_bots/bots/mention/test_mention.py +++ b/zulip_bots/zulip_bots/bots/mention/test_mention.py @@ -55,5 +55,5 @@ class TestMentionBot(BotTestCase, DefaultTests): with self.mock_config_info({"access_token": "TEST"}): with self.mock_http_conversation("invalid_api_key"): - with self.assertRaises(StubBotHandler.BotQuitException): + with self.assertRaises(StubBotHandler.BotQuitError): bot_test_instance.initialize(StubBotHandler()) diff --git a/zulip_bots/zulip_bots/bots/merels/libraries/game.py b/zulip_bots/zulip_bots/bots/merels/libraries/game.py index 0d654817..ce21a851 100644 --- a/zulip_bots/zulip_bots/bots/merels/libraries/game.py +++ b/zulip_bots/zulip_bots/bots/merels/libraries/game.py @@ -7,7 +7,7 @@ freely import another modules. import re -from zulip_bots.game_handler import BadMoveException +from zulip_bots.game_handler import BadMoveError from . import database, mechanics @@ -43,7 +43,7 @@ def unknown_command(): :return: A string containing info about available commands """ message = "Unknown command. Available commands: put (v,h), take (v,h), move (v,h) -> (v,h)" - raise BadMoveException(message) + raise BadMoveError(message) def beat(message, topic_name, merels_storage): @@ -70,7 +70,7 @@ def beat(message, topic_name, merels_storage): p2 = [int(x) for x in match.group(3).split(",")] if mechanics.get_take_status(topic_name, merels_storage) == 1: - raise BadMoveException("Take is required to proceed. Please try again.\n") + raise BadMoveError("Take is required to proceed. Please try again.\n") responses += mechanics.move_man(topic_name, p1, p2, merels_storage) + "\n" no_moves = after_event_checkup(responses, topic_name, merels_storage) @@ -98,7 +98,7 @@ def beat(message, topic_name, merels_storage): responses = "" if mechanics.get_take_status(topic_name, merels_storage) == 1: - raise BadMoveException("Take is required to proceed. Please try again.\n") + raise BadMoveError("Take is required to proceed. Please try again.\n") responses += mechanics.put_man(topic_name, p1[0], p1[1], merels_storage) + "\n" no_moves = after_event_checkup(responses, topic_name, merels_storage) @@ -117,7 +117,7 @@ def beat(message, topic_name, merels_storage): if mechanics.get_take_status(topic_name, merels_storage) == 1: responses += mechanics.take_man(topic_name, p1[0], p1[1], merels_storage) + "\n" if "Failed" in responses: - raise BadMoveException(responses) + raise BadMoveError(responses) mechanics.update_toggle_take_mode(topic_name, merels_storage) no_moves = after_event_checkup(responses, topic_name, merels_storage) @@ -130,7 +130,7 @@ def beat(message, topic_name, merels_storage): same_player_move = no_moves return responses, same_player_move else: - raise BadMoveException("Taking is not possible.") + raise BadMoveError("Taking is not possible.") else: return unknown_command() diff --git a/zulip_bots/zulip_bots/bots/merels/libraries/mechanics.py b/zulip_bots/zulip_bots/bots/merels/libraries/mechanics.py index e880c3b3..343f5c0a 100644 --- a/zulip_bots/zulip_bots/bots/merels/libraries/mechanics.py +++ b/zulip_bots/zulip_bots/bots/merels/libraries/mechanics.py @@ -5,7 +5,7 @@ mechanisms as well as some functions for accessing the database. from collections import Counter from math import sqrt -from zulip_bots.game_handler import BadMoveException +from zulip_bots.game_handler import BadMoveError from . import constants, database, game_data, interface @@ -361,7 +361,7 @@ def move_man(topic_name, p1, p2, merels_storage): ) return f"Moved a man from ({p1[0]}, {p1[1]}) -> ({p2[0]}, {p2[1]}) for {data.turn}." else: - raise BadMoveException("Failed: That's not a legal move. Please try again.") + raise BadMoveError("Failed: That's not a legal move. Please try again.") def put_man(topic_name, v, h, merels_storage): @@ -399,7 +399,7 @@ def put_man(topic_name, v, h, merels_storage): ) return f"Put a man to ({v}, {h}) for {data.turn}." else: - raise BadMoveException("Failed: That's not a legal put. Please try again.") + raise BadMoveError("Failed: That's not a legal put. Please try again.") def take_man(topic_name, v, h, merels_storage): @@ -443,7 +443,7 @@ def take_man(topic_name, v, h, merels_storage): ) return f"Taken a man from ({v}, {h}) for {data.turn}." else: - raise BadMoveException("Failed: That's not a legal take. Please try again.") + raise BadMoveError("Failed: That's not a legal take. Please try again.") def update_hill_uid(topic_name, merels_storage): diff --git a/zulip_bots/zulip_bots/bots/merels/merels.py b/zulip_bots/zulip_bots/bots/merels/merels.py index 946c2abc..ca9860e6 100644 --- a/zulip_bots/zulip_bots/bots/merels/merels.py +++ b/zulip_bots/zulip_bots/bots/merels/merels.py @@ -1,6 +1,6 @@ from typing import Any, List -from zulip_bots.game_handler import GameAdapter, SamePlayerMove +from zulip_bots.game_handler import GameAdapter, SamePlayerMoveError from .libraries import database, game, game_data, mechanics @@ -47,7 +47,7 @@ class MerelsModel: ) self.current_board, same_player_move = game.beat(move, self.topic, self.storage) if same_player_move != "": - raise SamePlayerMove(same_player_move) + raise SamePlayerMoveError(same_player_move) return self.current_board diff --git a/zulip_bots/zulip_bots/bots/merels/test/test_game.py b/zulip_bots/zulip_bots/bots/merels/test/test_game.py index 6d1f4ca1..e3f27b18 100644 --- a/zulip_bots/zulip_bots/bots/merels/test/test_game.py +++ b/zulip_bots/zulip_bots/bots/merels/test/test_game.py @@ -1,6 +1,6 @@ import unittest -from zulip_bots.game_handler import BadMoveException +from zulip_bots.game_handler import BadMoveError from zulip_bots.simple_lib import SimpleStorage from ..libraries import database, game @@ -26,7 +26,7 @@ class GameTest(unittest.TestCase): def test_not_possible_put_piece_output(self): merels = database.MerelsStorage(self.topic_name, self.storage) merels.update_game(self.topic_name, "X", 0, 0, "NNNNNNNNNNNNNNNNNNNNNNNN", "", 0) - with self.assertRaises(BadMoveException) as warning: + with self.assertRaises(BadMoveError) as warning: game.beat("put 0,1", self.topic_name, self.storage) self.assertTrue("Failed" in str(warning)) @@ -34,7 +34,7 @@ class GameTest(unittest.TestCase): merels = database.MerelsStorage(self.topic_name, self.storage) merels.update_game(self.topic_name, "X", 0, 0, "NNNNNNNNNNNNNNNNNNNNNNNN", "", 0) merels.update_game(self.topic_name, "X", 0, 0, "XXXNNNOOOXXXNNNOOOXXXNNN", "", 1) - with self.assertRaises(BadMoveException) as warning: + with self.assertRaises(BadMoveError) as warning: game.beat("put 1,1", self.topic_name, self.storage) self.assertTrue("Take is required" in str(warning)) @@ -49,7 +49,7 @@ class GameTest(unittest.TestCase): merels = database.MerelsStorage(self.topic_name, self.storage) merels.update_game(self.topic_name, "X", 0, 0, "NNNNNNNNNNNNNNNNNNNNNNNN", "", 0) merels.update_game(self.topic_name, "X", 0, 0, "XXXNNNOOOXXXNNNOOOXXXOOO", "", 0) - with self.assertRaises(BadMoveException) as warning: + with self.assertRaises(BadMoveError) as warning: game.beat("move 0,3 1,2", self.topic_name, self.storage) self.assertTrue("Failed" in str(warning)) @@ -64,7 +64,7 @@ class GameTest(unittest.TestCase): merels = database.MerelsStorage(self.topic_name, self.storage) merels.update_game(self.topic_name, "X", 0, 0, "NNNNNNNNNNNNNNNNNNNNNNNN", "", 0) merels.update_game(self.topic_name, "X", 6, 6, "XXXNNNOOONNNNNNNNNNNNNNN", "", 1) - with self.assertRaises(BadMoveException) as warning: + with self.assertRaises(BadMoveError) as warning: game.beat("move 0,1 1,3", self.topic_name, self.storage) self.assertTrue("Take is required" in str(warning)) @@ -72,7 +72,7 @@ class GameTest(unittest.TestCase): merels = database.MerelsStorage(self.topic_name, self.storage) merels.update_game(self.topic_name, "X", 0, 0, "NNNNNNNNNNNNNNNNNNNNNNNN", "", 0) merels.update_game(self.topic_name, "X", 6, 6, "XXXNNNOOONNNNNNNNNNNNNNN", "", 1) - with self.assertRaises(BadMoveException) as warning: + with self.assertRaises(BadMoveError) as warning: game.beat("magic 2,2", self.topic_name, self.storage) self.assertTrue("Unknown command" in str(warning)) @@ -87,7 +87,7 @@ class GameTest(unittest.TestCase): merels = database.MerelsStorage(self.topic_name, self.storage) merels.update_game(self.topic_name, "X", 0, 0, "NNNNNNNNNNNNNNNNNNNNNNNN", "", 0) merels.update_game(self.topic_name, "X", 6, 6, "XXXNNNOOOXXXNNNOOOXXXOOO", "", 0) - with self.assertRaises(BadMoveException) as warning: + with self.assertRaises(BadMoveError) as warning: game.beat("take 2,2", self.topic_name, self.storage) self.assertTrue("Taking is not possible" in str(warning)) diff --git a/zulip_bots/zulip_bots/bots/monkeytestit/monkeytestit.py b/zulip_bots/zulip_bots/bots/monkeytestit/monkeytestit.py index 0aab1884..f5b9b499 100644 --- a/zulip_bots/zulip_bots/bots/monkeytestit/monkeytestit.py +++ b/zulip_bots/zulip_bots/bots/monkeytestit/monkeytestit.py @@ -2,7 +2,7 @@ import logging from typing import Dict from zulip_bots.bots.monkeytestit.lib import parse -from zulip_bots.lib import BotHandler, NoBotConfigException +from zulip_bots.lib import BotHandler, NoBotConfigError class MonkeyTestitBot: @@ -20,7 +20,7 @@ class MonkeyTestitBot: def initialize(self, bot_handler: BotHandler) -> None: try: self.config = bot_handler.get_config_info("monkeytestit") - except NoBotConfigException: + except NoBotConfigError: bot_handler.quit( "Quitting because there's no config file " "supplied. See doc.md for a guide on setting up " diff --git a/zulip_bots/zulip_bots/bots/salesforce/test_salesforce.py b/zulip_bots/zulip_bots/bots/salesforce/test_salesforce.py index 9e3b30be..0b0b09a9 100644 --- a/zulip_bots/zulip_bots/bots/salesforce/test_salesforce.py +++ b/zulip_bots/zulip_bots/bots/salesforce/test_salesforce.py @@ -163,7 +163,7 @@ class TestSalesforceBot(BotTestCase, DefaultTests): self._test("test_one_result", "find contact", "Usage: find contact [arguments]") def test_bad_auth(self) -> None: - with self.assertRaises(StubBotHandler.BotQuitException): + with self.assertRaises(StubBotHandler.BotQuitError): self._test_initialize(auth_success=False) def test_callback(self) -> None: diff --git a/zulip_bots/zulip_bots/bots/tictactoe/tictactoe.py b/zulip_bots/zulip_bots/bots/tictactoe/tictactoe.py index 0a498f15..625402b0 100644 --- a/zulip_bots/zulip_bots/bots/tictactoe/tictactoe.py +++ b/zulip_bots/zulip_bots/bots/tictactoe/tictactoe.py @@ -4,7 +4,7 @@ from typing import Any, List, Tuple from typing_extensions import override -from zulip_bots.game_handler import BadMoveException, GameAdapter +from zulip_bots.game_handler import BadMoveError, GameAdapter # ------------------------------------- @@ -203,7 +203,7 @@ class TicTacToeModel: return self.computer_move(self.current_board, player_number + 1) move_coords_str = coords_from_command(move) if not self.is_valid_move(move_coords_str): - raise BadMoveException("Make sure your move is from 0-9") + raise BadMoveError("Make sure your move is from 0-9") board = self.current_board move_coords = move_coords_str.split(",") # Subtraction must be done to convert to the right indices, @@ -211,7 +211,7 @@ class TicTacToeModel: row = (int(move_coords[1])) - 1 column = (int(move_coords[0])) - 1 if board[row][column] != 0: - raise BadMoveException("Make sure your space hasn't already been filled.") + raise BadMoveError("Make sure your space hasn't already been filled.") board[row][column] = player_number + 1 return board diff --git a/zulip_bots/zulip_bots/bots/trello/test_trello.py b/zulip_bots/zulip_bots/bots/trello/test_trello.py index 4cb0832c..a53ea67d 100644 --- a/zulip_bots/zulip_bots/bots/trello/test_trello.py +++ b/zulip_bots/zulip_bots/bots/trello/test_trello.py @@ -29,7 +29,7 @@ class TestTrelloBot(BotTestCase, DefaultTests): ) def test_bot_quit_with_invalid_config(self) -> None: - with self.mock_config_info(mock_config), self.assertRaises(StubBotHandler.BotQuitException): + with self.mock_config_info(mock_config), self.assertRaises(StubBotHandler.BotQuitError): with self.mock_http_conversation("invalid_key"): TrelloHandler().initialize(StubBotHandler()) diff --git a/zulip_bots/zulip_bots/bots/trivia_quiz/trivia_quiz.py b/zulip_bots/zulip_bots/bots/trivia_quiz/trivia_quiz.py index 8fb4463b..139f96c3 100644 --- a/zulip_bots/zulip_bots/bots/trivia_quiz/trivia_quiz.py +++ b/zulip_bots/zulip_bots/bots/trivia_quiz/trivia_quiz.py @@ -9,11 +9,11 @@ import requests from zulip_bots.lib import BotHandler -class NotAvailableException(Exception): +class NotAvailableError(Exception): pass -class InvalidAnswerException(Exception): +class InvalidAnswerError(Exception): pass @@ -29,14 +29,14 @@ class TriviaQuizHandler: try: start_new_quiz(message, bot_handler) return - except NotAvailableException: + except NotAvailableError: bot_response = "Uh-Oh! Trivia service is down." bot_handler.send_reply(message, bot_response) return elif query.startswith("answer"): try: (quiz_id, answer) = parse_answer(query) - except InvalidAnswerException: + except InvalidAnswerError: bot_response = "Invalid answer format" bot_handler.send_reply(message, bot_response) return @@ -75,12 +75,12 @@ def start_new_quiz(message: Dict[str, Any], bot_handler: BotHandler) -> None: def parse_answer(query: str) -> Tuple[str, str]: m = re.match(r"answer\s+(Q...)\s+(.)", query) if not m: - raise InvalidAnswerException + raise InvalidAnswerError quiz_id = m.group(1) answer = m.group(2).upper() if answer not in "ABCD": - raise InvalidAnswerException + raise InvalidAnswerError return (quiz_id, answer) @@ -98,10 +98,10 @@ def get_trivia_payload() -> Dict[str, Any]: data = requests.get(url) except requests.exceptions.RequestException: - raise NotAvailableException + raise NotAvailableError if data.status_code != 200: - raise NotAvailableException + raise NotAvailableError payload = data.json() return payload diff --git a/zulip_bots/zulip_bots/bots/youtube/test_youtube.py b/zulip_bots/zulip_bots/bots/youtube/test_youtube.py index 4bfcee0a..22922a8f 100644 --- a/zulip_bots/zulip_bots/bots/youtube/test_youtube.py +++ b/zulip_bots/zulip_bots/bots/youtube/test_youtube.py @@ -50,7 +50,7 @@ class TestYoutubeBot(BotTestCase, DefaultTests): with self.mock_config_info( {"key": "somethinginvalid", "number_of_results": "5", "video_region": "US"} ), self.mock_http_conversation("test_invalid_key"), self.assertRaises( - bot_handler.BotQuitException + bot_handler.BotQuitError ): bot.initialize(bot_handler) diff --git a/zulip_bots/zulip_bots/finder.py b/zulip_bots/zulip_bots/finder.py index 8462a1cc..8fbcc495 100644 --- a/zulip_bots/zulip_bots/finder.py +++ b/zulip_bots/zulip_bots/finder.py @@ -30,7 +30,7 @@ def import_module_by_name(name: str) -> Any: return None -class DuplicateRegisteredBotName(Exception): +class DuplicateRegisteredBotNameError(Exception): pass @@ -73,7 +73,7 @@ def import_module_from_zulip_bot_registry(name: str) -> Tuple[str, Optional[Modu return f"editable package: {bot_name}", bot_module if len(matching_bots) > 1: - raise DuplicateRegisteredBotName(name) + raise DuplicateRegisteredBotNameError(name) return "", None # no matches in registry diff --git a/zulip_bots/zulip_bots/game_handler.py b/zulip_bots/zulip_bots/game_handler.py index ddd04ca3..98b6b8aa 100644 --- a/zulip_bots/zulip_bots/game_handler.py +++ b/zulip_bots/zulip_bots/game_handler.py @@ -10,7 +10,7 @@ from typing_extensions import override from zulip_bots.lib import BotHandler -class BadMoveException(Exception): +class BadMoveError(Exception): def __init__(self, message: str) -> None: self.message = message @@ -19,7 +19,7 @@ class BadMoveException(Exception): return self.message -class SamePlayerMove(Exception): +class SamePlayerMoveError(Exception): def __init__(self, message: str) -> None: self.message = message @@ -925,10 +925,10 @@ class GameInstance: try: self.model.make_move(content, self.turn, is_computer) # Keep the turn of the same player - except SamePlayerMove as smp: + except SamePlayerMoveError as smp: self.same_player_turn(content, smp.message, is_computer) return - except BadMoveException as e: + except BadMoveError as e: self.broadcast(e.message) self.broadcast(self.parse_current_board()) return diff --git a/zulip_bots/zulip_bots/lib.py b/zulip_bots/zulip_bots/lib.py index e7238843..63ac9a69 100644 --- a/zulip_bots/zulip_bots/lib.py +++ b/zulip_bots/zulip_bots/lib.py @@ -14,7 +14,7 @@ from typing_extensions import Protocol from zulip import Client, ZulipError -class NoBotConfigException(Exception): +class NoBotConfigError(Exception): pass @@ -319,7 +319,7 @@ class ExternalBotHandler: # on setting up the configuration specfic to this bot. # And then `run.py` should also catch exceptions on how # to specify the file in the command line. - raise NoBotConfigException(bot_name) + raise NoBotConfigError(bot_name) if bot_name not in self.bot_config_file: print( diff --git a/zulip_bots/zulip_bots/run.py b/zulip_bots/zulip_bots/run.py index d5856321..b5a24db0 100755 --- a/zulip_bots/zulip_bots/run.py +++ b/zulip_bots/zulip_bots/run.py @@ -8,7 +8,7 @@ from typing import Optional from zulip_bots import finder from zulip_bots.lib import ( - NoBotConfigException, + NoBotConfigError, run_message_handler_for_bot, zulip_env_vars_are_present, ) @@ -118,7 +118,7 @@ def main() -> None: if args.registry: try: bot_source, lib_module = finder.import_module_from_zulip_bot_registry(args.bot) - except finder.DuplicateRegisteredBotName as error: + except finder.DuplicateRegisteredBotNameError as error: print( f'ERROR: Found duplicate entries for "{error}" in zulip bots registry.\n' "Make sure that you don't install bots using the same entry point. Exiting now." @@ -182,7 +182,7 @@ def main() -> None: bot_name=bot_name, bot_source=bot_source, ) - except NoBotConfigException: + except NoBotConfigError: print( """ ERROR: Your bot requires you to specify a third party diff --git a/zulip_bots/zulip_bots/test_lib.py b/zulip_bots/zulip_bots/test_lib.py index 58a8228a..236f690e 100644 --- a/zulip_bots/zulip_bots/test_lib.py +++ b/zulip_bots/zulip_bots/test_lib.py @@ -47,11 +47,11 @@ class StubBotHandler: def upload_file(self, file: IO[Any]) -> Dict[str, Any]: return self.message_server.upload_file(file) - class BotQuitException(Exception): + class BotQuitError(Exception): pass def quit(self, message: str = "") -> None: - raise self.BotQuitException + raise self.BotQuitError def get_config_info(self, bot_name: str, optional: bool = False) -> Dict[str, str]: return {}