command.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:endrebot0 作者: endreman0 项目源码 文件源码
def command(fn, name=None):
    """Decorator for functions that should be exposed as commands."""
    module = sys.modules[fn.__module__]
    if name is None:
        name = fn.__name__
    if asyncio.iscoroutinefunction(fn if inspect.isfunction(fn) else fn.__func__ if inspect.ismethod(fn) else fn.__call__): # Get the actual function for coroutine check
        @functools.wraps(fn)
        async def wrapper(*args, **kwargs):
            try:
                frame = inspect.currentframe()
                ctx = frame.f_back.f_locals['ctx']
                return await fn(ctx, *args, **kwargs)
            finally:
                del frame
    else:
        @functools.wraps(fn)
        def wrapper(*args, **kwargs):
            try:
                frame = inspect.currentframe()
                ctx = frame.f_back.f_locals['ctx']
                return fn(ctx, *args, **kwargs)
            finally:
                del frame
    vars(module).setdefault('commands', {})[fn.__name__] = wrapper
    return wrapper
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号