aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar kwzrd <[email protected]>2020-03-23 20:17:12 +0100
committerGravatar kwzrd <[email protected]>2020-03-23 20:17:12 +0100
commit1a326904f1bfead7c2b25839910fbeb4c70d84fb (patch)
treee69d63e8aade432e239f435a7f88b0c338488221
parentDeseasonify: abdicate responsibility to wait until bot is ready (diff)
Deseasonify: add `mock_in_debug` decorator
This should be very useful for testing. See docstring.
-rw-r--r--bot/decorators.py22
1 files changed, 21 insertions, 1 deletions
diff --git a/bot/decorators.py b/bot/decorators.py
index 400f1bbb..efd43da0 100644
--- a/bot/decorators.py
+++ b/bot/decorators.py
@@ -12,7 +12,7 @@ from discord import Colour, Embed
from discord.ext import commands
from discord.ext.commands import CheckFailure, Context
-from bot.constants import ERROR_REPLIES, Month
+from bot.constants import Client, ERROR_REPLIES, Month
ONE_DAY = 24 * 60 * 60
@@ -261,3 +261,23 @@ def locked() -> typing.Union[typing.Callable, None]:
return await func(self, ctx, *args, **kwargs)
return inner
return wrap
+
+
+def mock_in_debug(return_value: typing.Any) -> typing.Callable:
+ """
+ Short-circuit function execution if in debug mode and return `return_value`.
+
+ The original function name, and the incoming args and kwargs are DEBUG level logged
+ upon each call. This is useful for expensive operations, i.e. media asset uploads
+ that are prone to rate-limits but need to be tested extensively.
+ """
+ def decorator(func: typing.Callable) -> typing.Callable:
+ @functools.wraps(func)
+ async def wrapped(*args, **kwargs) -> typing.Any:
+ """Short-circuit and log if in debug mode."""
+ if Client.debug:
+ log.debug(f"Function {func.__name__} called with args: {args}, kwargs: {kwargs}")
+ return return_value
+ return await func(*args, **kwargs)
+ return wrapped
+ return decorator