From 4a9e8b6219ae0d6624cd99c6ae4c72fb8e027a77 Mon Sep 17 00:00:00 2001 From: nelcel Date: Wed, 6 Apr 2022 16:07:55 -0700 Subject: [PATCH 1/2] refactoring --- cogs/search.py | 29 +---------------------------- utils/result.py | 38 +++++++++++++++++++++++--------------- utils/tools/chat.py | 2 ++ 3 files changed, 26 insertions(+), 43 deletions(-) diff --git a/cogs/search.py b/cogs/search.py index e6d9b84..ad42713 100644 --- a/cogs/search.py +++ b/cogs/search.py @@ -15,10 +15,6 @@ async def walpha(self, context: Context, *queryTerms): result: Result = WAlphaQuery(queryTerms).fulfill() - if result.success is False: - await result.showFail(context) - return - await initNav( bot = self.bot, result = result, @@ -32,10 +28,6 @@ async def duckInstantAnswer(self, context, *queryTerms): result: Result = DuckQuery(queryTerms).fulfill() - if result.success is False: - await result.showFail(context) - return - await context.send(embed = result.getPage(0)) @commands.command(name="kpanel", aliases=['knowledgegraph', 'kgraph', 'kg', 'kp'], brief="query Google's Knowledge Graph", pass_context=True) @@ -43,20 +35,12 @@ async def kgraph(self, context, *queryTerms): result: Result = KGraphQuery(queryTerms).fulfill() - if result.success is False: - await result.showFail(context) - return - await context.send(embed = result.getPage(0)) @commands.command(name='image', aliases=['findimage', 'i'], brief='query Google Images (100 per day)', pass_context=True) async def imageSearch(self, context, *queryTerms): result: Result = CSEImQuery(queryTerms).fulfill() - - if result.success is False: - await result.showFail(context) - return await initNav( bot = self.bot, @@ -71,10 +55,6 @@ async def findpage(self, context, *queryTerms): result: Result = CSEQuery(queryTerms).fulfill() - if result.success is False: - await result.showFail(context) - return - await context.send(embed = result.getPage(0)) @commands.command(name="jisho", aliases=['j'], brief="query jisho.org to get japanese results", pass_context=True) @@ -82,10 +62,6 @@ async def jisho(self, context, *queryTerms): result: Result = JishoQuery(queryTerms).fulfill() - if result.success is False: - await result.showFail(context) - return - await initNav( bot = self.bot, result = result, @@ -96,12 +72,9 @@ async def jisho(self, context, *queryTerms): @commands.command(name="urban", aliases=['ud'], brief="search urban dictionary for definitions", pass_context=True) async def urban(self, context, *queryTerms): + result: Result = UDictQuery(queryTerms).fulfill() - if result.success is False: - await result.showFail(context) - return - await initNav( bot = self.bot, result = result, diff --git a/utils/result.py b/utils/result.py index 4a61f14..5db1382 100644 --- a/utils/result.py +++ b/utils/result.py @@ -1,4 +1,5 @@ -from typing import List +from multiprocessing.sharedctypes import Value +from typing import List, Type from utils.tools.misc import log from discord import Embed, Color from dataclasses import dataclass, field @@ -55,14 +56,25 @@ def embed(self, number: int = None, total: int = None) -> Embed: class Result: - def __init__(self, success: bool, type: str = "unspecified", query: str = None, pages: List[Page] = [], failurePage: Page = defaultFail) -> None: - self.success: bool = success - assert query is not None and isinstance(query, str), "Query must be a string" - assert isinstance(pages, list), "Must provide a of s" - self.query: str = query - self.pages: List[Page] = pages - self.fail = failurePage - self.type = type + def __init__(self, success: bool, type: str = "unspecified", query: str = None, + pages: List[Page] = [], failurePage: Page = defaultFail) -> None: + primitive_args = { + 'success' : [success, bool], + 'type' : [type, str], + 'query' : [query, str], + 'pages' : [pages, list], + } + + for arg, value in primitive_args.items(): + if value[0] is None: + raise ValueError(f"Argument {arg} cannot be None") + if not isinstance(value[0], value[1]): + raise TypeError(f"Argument {arg} must be type {value[1]}. Got: {type(value[0])}") + setattr(self, arg, value[0]) + + if failurePage is None: + raise ValueError(f"Argument failurePage cannot be None") + self.fail: Page = failurePage def __repr__(self) -> str: rep = f"Result: \n" @@ -73,6 +85,7 @@ def __repr__(self) -> str: return rep def _getFailPage(self) -> Embed: + log(f"FAILED {self.type} request with query: `{self.query}`") return self.fail.embed() def addPage(self, page: Page) -> None: @@ -82,7 +95,7 @@ def getPage(self, index: int) -> Embed: # get the `index` page of the embed if not self.success: - return self.fail.embed() + return self._getFailPage() if index >= len(self.pages): raise IndexError( @@ -92,8 +105,3 @@ def getPage(self, index: int) -> Embed: page = self.pages[index] return page.embed(index + 1, len(self.pages)) - async def showFail(self, context: Context) -> None: - - log(f"FAILED {self.type} request with query: `{self.query}`") - - await context.send(embed = self._getFailPage()) diff --git a/utils/tools/chat.py b/utils/tools/chat.py index e660d3e..edb184b 100644 --- a/utils/tools/chat.py +++ b/utils/tools/chat.py @@ -10,6 +10,8 @@ async def initNav(bot: Bot, result: Result, context: Context, purpose: str = "", index = 0 message: Message = await context.send(embed=result.getPage(index)) + if not result.success: + return author: Union[User, Member] = context.author From 7294466f14879843fbcade0b7aa7a314fd7e15b4 Mon Sep 17 00:00:00 2001 From: nelcel Date: Wed, 6 Apr 2022 16:08:14 -0700 Subject: [PATCH 2/2] cleaned up --- tests/test_page.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_page.py b/tests/test_page.py index 109ba9e..c241a81 100644 --- a/tests/test_page.py +++ b/tests/test_page.py @@ -1,9 +1,9 @@ import dataclasses -from random import randint, randrange +from random import randint import unittest as ut from unittest.mock import PropertyMock, patch, MagicMock, call from utils.result import Field, Page -from discord import Embed, Color +from discord import Color class FieldTests(ut.TestCase):