Skip to content

Commit

Permalink
Merge branch 'main' into 14-results
Browse files Browse the repository at this point in the history
  • Loading branch information
nelson-lojo committed Apr 6, 2022
2 parents b6ffb54 + 7294466 commit caa3e59
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 45 deletions.
29 changes: 1 addition & 28 deletions cogs/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -32,31 +28,19 @@ 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)
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,
Expand All @@ -71,21 +55,13 @@ 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)
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,
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions tests/test_page.py
Original file line number Diff line number Diff line change
@@ -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):

Expand Down
38 changes: 23 additions & 15 deletions utils/result.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 <list> of <Page>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"
Expand All @@ -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:
Expand All @@ -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(
Expand All @@ -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())
2 changes: 2 additions & 0 deletions utils/tools/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit caa3e59

Please sign in to comment.