Esempio n. 1
0
            def wrapper(*inner_args, **inner_kwargs):
                logger.debug(
                    f'Command called: handler={handler}, names={names}, async={is_async}, kwargs={kwargs}, inner_args={inner_args}, inner_kargs={inner_kwargs}'
                )
                method_class = get_class_that_defined_method(func)

                if (inner_args and isinstance(inner_args[0], method_class)) \
                        or not (len(inner_args) > 1
                                and isinstance(inner_args[0], Bot)
                                and isinstance(inner_args[1], Update)):
                    return func(*inner_args, **inner_kwargs)

                _args, _kwargs = inner_args, inner_kwargs
                if method_class and BaseCommand in method_class.__bases__:
                    try:
                        instance = method_class(*inner_args, **inner_kwargs)
                    except CancelOperation:
                        return
                    _args = [instance]
                    _kwargs = {}

                if is_async:
                    run_async(func(*_args, **_kwargs))
                else:
                    func(*_args, **_kwargs)
Esempio n. 2
0
                def get_wrapper(func):
                    complete_arg_spec = inspect.getfullargspec(method)
                    argspec = complete_arg_spec.args
                    has_vararg = inspect.getfullargspec(method).varargs

                    if (argspec[0] != 'self'):
                        raise ValueError(
                            'First argument of TelegramCommand should be '
                            'self')
                    argspec = argspec[1:]

                    def wrapper(bot, update, user_data=None, args=[]):
                        amount_of_method_arguments = len(argspec)
                        amount_of_given_arguments = len(args)
                        not_enough_given_arguments = amount_of_given_arguments < amount_of_method_arguments
                        too_many_given_arguments = amount_of_given_arguments > amount_of_method_arguments

                        ins = x(context=user_data, update=update, bot=bot)
                        if (not_enough_given_arguments) or (
                                too_many_given_arguments and not has_vararg):
                            logging.debug(
                                'Command called with invalid argument count')
                            bot.send_message(
                                chat_id=update.message.chat_id,
                                text='Ongeldig aantal argumenten.')
                            bot.send_message(chat_id=update.message.chat_id,
                                             text=cls.get_help_text(func))
                        else:
                            func(ins, *args)

                    wrapper.original = func
                    return run_async(wrapper)
Esempio n. 3
0
    def __init__(self,
                 func,
                 admin_required,
                 handler_class,
                 use_async=False,
                 *args,
                 **kwargs):
        @functools.wraps(func)
        def callback(update, context):
            logger = logging.getLogger(func.__module__)
            logger.debug(f"☎️  CALLING: {func.__name__:30s}")
            logger.debug(f"📑\n{update}")
            if admin_required:
                if utils.user_is_admin(context.bot, update):
                    return func(update, context)
                update.message.reply_text("Only admin can use this command.")
            else:
                return func(update, context)

        if use_async:
            callback = run_async(callback)

        self.callback = callback
        self.handler_class = handler_class
        self.handler = handler_class(*args, callback=callback, **kwargs)
        self.__doc__ = func.__doc__
            def wrapper(*inner_args, **inner_kwargs):
                method_class = get_class_that_defined_method(func)

                if (inner_args and isinstance(inner_args[0], method_class)) \
                        or not (len(inner_args) > 1
                                and isinstance(inner_args[0], Bot)
                                and isinstance(inner_args[1], Update)):
                    return func(*inner_args, **inner_kwargs)

                _args, _kwargs = inner_args, inner_kwargs
                if method_class and BaseCommand in method_class.__bases__:
                    try:
                        instance = method_class(*inner_args, **inner_kwargs)
                    except CancelOperation:
                        return
                    _args = [instance]
                    _kwargs = {}

                if is_async:
                    run_async(func(*_args, **_kwargs))
                else:
                    func(*_args, **_kwargs)
Esempio n. 5
0
def make_async(handlers: List[HandlerWrapper]):
    for wrapper in handlers:
        wrapper.handler.callback = run_async(wrapper.handler.callback)