aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Shakya Majumdar <[email protected]>2021-04-11 21:03:48 +0530
committerGravatar Shakya Majumdar <[email protected]>2021-04-11 21:05:02 +0530
commit7f6fa93dc43a1885c3ea0dfd695dec639822647c (patch)
treea7758810acb80a5eb15a61ebe0b89f6a9df0dd77
parentadd caching (diff)
add max_concurrency, move file-saving to _render
-rw-r--r--bot/exts/evergreen/latex.py21
1 files changed, 13 insertions, 8 deletions
diff --git a/bot/exts/evergreen/latex.py b/bot/exts/evergreen/latex.py
index 0c29f958..c4a8597c 100644
--- a/bot/exts/evergreen/latex.py
+++ b/bot/exts/evergreen/latex.py
@@ -31,16 +31,19 @@ FORMATTED_CODE_REGEX = re.compile(
)
CACHE_DIRECTORY = pathlib.Path("_latex_cache")
-if not CACHE_DIRECTORY.exists():
- CACHE_DIRECTORY.mkdir()
+CACHE_DIRECTORY.mkdir(exist_ok=True)
class Latex(commands.Cog):
"""Renders latex."""
@staticmethod
- def _render(text: str) -> BytesIO:
- """Return the rendered image if latex compiles without errors, otherwise raise a BadArgument Exception."""
+ def _render(text: str, filepath: pathlib.Path) -> BytesIO:
+ """
+ Return the rendered image if latex compiles without errors, otherwise raise a BadArgument Exception.
+
+ Saves rendered image to cache.
+ """
fig = plt.figure()
rendered_image = BytesIO()
fig.text(0, 1, text, horizontalalignment="left", verticalalignment="top")
@@ -51,6 +54,10 @@ class Latex(commands.Cog):
raise commands.BadArgument(str(e))
rendered_image.seek(0)
+
+ with open(filepath, "wb") as f:
+ f.write(rendered_image.getbuffer())
+
return rendered_image
@staticmethod
@@ -63,6 +70,7 @@ class Latex(commands.Cog):
return text
@commands.command()
+ @commands.max_concurrency(1, commands.BucketType.guild, wait=True)
async def latex(self, ctx: commands.Context, *, text: str) -> None:
"""Renders the text in latex and sends the image."""
text = self._prepare_input(text)
@@ -75,14 +83,11 @@ class Latex(commands.Cog):
with ThreadPoolExecutor() as pool:
image = await asyncio.get_running_loop().run_in_executor(
- pool, self._render, text
+ pool, self._render, text, image_path
)
await ctx.send(file=discord.File(image, "latex.png"))
- with open(image_path, "wb") as f:
- f.write(image.getbuffer())
-
def setup(bot: commands.Bot) -> None:
"""Load the Latex Cog."""