Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
Redo some common check as a decorator, add a new one, change tests accordingly
  • Loading branch information
Petricpwnz committed Nov 12, 2018
1 parent f8a213e commit 65e81af
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 85 deletions.
56 changes: 56 additions & 0 deletions qai/decorators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from functools import wraps
import asyncio


def nickserv_identified(func):
@wraps(func)
async def wrapper(*args, **kwargs):
try:
self, mask = args[0], args[1]
if not (await self._Plugin__is_nick_serv_identified(mask.nick)):
return
except Exception:
pass
return func(*args, **kwargs)

@wraps(func)
async def async_wrapper(*args, **kwargs):
try:
self, mask = args[0], args[1]
if not (await self._Plugin__is_nick_serv_identified(mask.nick)):
return
except Exception:
pass
return await func(*args, **kwargs)

if asyncio.iscoroutinefunction(func):
return async_wrapper
else:
return wrapper


def channel_only(func):
@wraps(func)
def wrapper(*args, **kwargs):
try:
self, target = args[0], args[2]
if not self._is_a_channel(target):
return 'You can only use this command in channels.'
except Exception:
pass
return func(*args, **kwargs)

@wraps(func)
async def async_wrapper(*args, **kwargs):
try:
self, target = args[0], args[2]
if not self._is_a_channel(target):
return 'You can only use this command in channels.'
except Exception:
pass
return await func(*args, **kwargs)

if asyncio.iscoroutinefunction(func):
return async_wrapper
else:
return wrapper
Loading

0 comments on commit 65e81af

Please sign in to comment.