From d22367679e809756d954d626e575235fbcbfdf31 Mon Sep 17 00:00:00 2001 From: Zach Priddy Date: Mon, 20 Apr 2020 00:09:20 -0700 Subject: [PATCH] add has_route to plugin --- setup.cfg | 2 +- src/glados/plugin.py | 20 ++++++++++++++++++++ tests/test_glados_plugin.py | 2 ++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/setup.cfg b/setup.cfg index c5ff8d5..5e1fa09 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = glados -version = 0.0.1-dev22 +version = 0.0.1-dev23 description = A library to help with slackbot development long_description = file: README.md long_description_content_type = text/markdown diff --git a/src/glados/plugin.py b/src/glados/plugin.py index ce6abef..98b9770 100644 --- a/src/glados/plugin.py +++ b/src/glados/plugin.py @@ -326,6 +326,26 @@ def respond_to_url(self, request: GladosRequest, text: str, **kwargs): r = requests.post(request.response_url, json=kwargs) logging.info(f"slack response: {r}") + def has_route(self, route: str) -> bool: + """See if route exists. + + Parameters + ---------- + route : route to check + + Returns + ------- + True if route exists else false + """ + route_extended = f"{self.bot.name}_{route}" + for r_type, route_entry in self._routes.items(): + try: + if list(route_entry.keys())[0] in [route, route_extended]: + return True + except IndexError: + continue + return False + @property def routes(self) -> List[GladosRoute]: """List all routes for the plugin.""" diff --git a/tests/test_glados_plugin.py b/tests/test_glados_plugin.py index 1c4865c..fcaee03 100644 --- a/tests/test_glados_plugin.py +++ b/tests/test_glados_plugin.py @@ -60,7 +60,9 @@ def test_add_route(MockGladosPlugin): assert len(MockGladosPlugin.routes) == 1 route = MockGladosPlugin.routes[0] + print(MockGladosPlugin._routes) + assert MockGladosPlugin.has_route("send_message") == True assert route.route == "bot_send_message" assert route.function(None)