diff options
author | 2021-03-06 03:46:25 +0100 | |
---|---|---|
committer | 2021-03-06 03:46:25 +0100 | |
commit | 4f5f284d3eec46b9209d19142d5c21456c4c403a (patch) | |
tree | 3eee75950397697c8f7178367a127e5e18951565 | |
parent | Correct tests cases (diff) |
Abstract logic from create_symbol_embed into additional methods
The method was also renamed from get_symbol_embed to create_symbol_embed
-rw-r--r-- | bot/exts/info/doc/_cog.py | 54 |
1 files changed, 36 insertions, 18 deletions
diff --git a/bot/exts/info/doc/_cog.py b/bot/exts/info/doc/_cog.py index 60f6d8eea..64e204fad 100644 --- a/bot/exts/info/doc/_cog.py +++ b/bot/exts/info/doc/_cog.py @@ -229,19 +229,13 @@ class DocCog(commands.Cog): log.debug("Finished inventory refresh.") self.refresh_event.set() - async def get_symbol_embed(self, symbol_name: str) -> Optional[discord.Embed]: + def get_symbol_item(self, symbol_name: str) -> Optional[DocItem]: """ - Attempt to scrape and fetch the data for the given `symbol_name`, and build an embed from its contents. - - If the symbol is known, an Embed with documentation about it is returned. + Get the `DocItem` associated with `symbol_name` from the `doc_symbols` dict. - First check the DocRedisCache before querying the cog's `BatchParser`. + If the doc item is not found directly from the name and the name contains a space, + the first word of the name will be attempted to be used to get the item. """ - log.trace(f"Building embed for symbol `{symbol_name}`") - if not self.refresh_event.is_set(): - log.debug("Waiting for inventories to be refreshed before processing item.") - await self.refresh_event.wait() - doc_item = self.doc_symbols.get(symbol_name) if doc_item is None and " " in symbol_name: # If an invalid symbol contains a space, check if the command was invoked @@ -249,25 +243,49 @@ class DocCog(commands.Cog): symbol_name = symbol_name.split(" ", maxsplit=1)[0] doc_item = self.doc_symbols.get(symbol_name) - if doc_item is None: - log.debug("Symbol does not exist.") - return None + return doc_item - self.bot.stats.incr(f"doc_fetches.{doc_item.package}") + async def get_symbol_markdown(self, doc_item: DocItem) -> str: + """ + Get the Markdown from the symbol `doc_item` refers to. + First a redis lookup is attempted, if that fails the `item_fetcher` + is used to fetch the page and parse the HTML from it into Markdown. + """ with self.symbol_get_event: markdown = await doc_cache.get(doc_item) if markdown is None: - log.debug(f"Redis cache miss for symbol `{symbol_name}`.") + log.debug(f"Redis cache miss with {doc_item}.") markdown = await self.item_fetcher.get_markdown(doc_item) if markdown is None: - markdown = "Unable to parse the requested symbol." + return "Unable to parse the requested symbol." + return markdown + + async def create_symbol_embed(self, symbol_name: str) -> Optional[discord.Embed]: + """ + Attempt to scrape and fetch the data for the given `symbol_name`, and build an embed from its contents. + + If the symbol is known, an Embed with documentation about it is returned. + + First check the DocRedisCache before querying the cog's `BatchParser`. + """ + log.trace(f"Building embed for symbol `{symbol_name}`") + if not self.refresh_event.is_set(): + log.debug("Waiting for inventories to be refreshed before processing item.") + await self.refresh_event.wait() + + doc_item = self.get_symbol_item(symbol_name) + if doc_item is None: + log.debug("Symbol does not exist.") + return None + + self.bot.stats.incr(f"doc_fetches.{doc_item.package}") embed = discord.Embed( title=discord.utils.escape_markdown(symbol_name), url=f"{doc_item.url}#{doc_item.symbol_id}", - description=markdown + description=await self.get_symbol_markdown(doc_item) ) # Show all symbols with the same name that were renamed in the footer, # with a max of 100 chars. @@ -314,7 +332,7 @@ class DocCog(commands.Cog): else: symbol = symbol_name.strip("`") async with ctx.typing(): - doc_embed = await self.get_symbol_embed(symbol) + doc_embed = await self.create_symbol_embed(symbol) if doc_embed is None: error_message = await send_denial(ctx, "No documentation found for the requested symbol.") |