Exemplo n.º 1
0
    def do_command(self, predicates, meth, client, target, data=None, **kw):
        nick = self.context.nick or '-'
        to = target == nick and client.nick or target
        doc = meth.__doc__ or ''
        doc = [l.strip() for l in doc.strip().split('\n')]
        doc = [nick + ' ' + l.strip('%%')
               for l in doc if l.startswith('%%')]
        doc = 'Usage:' + '\n    ' + '\n    '.join(doc)
        encoding = self.context.encoding
        if data:
            if not isinstance(data, str):  # pragma: no cover
                data = data.encode(encoding)
        try:
            data = self.split_command(
                data, use_shlex=predicates.get('use_shlex', True))
        except ValueError as e:
            if not predicates.get('quiet', False):
                self.context.privmsg(to, 'Invalid arguments: {}.'.format(e))
            return
        docopt_args = dict(help=False)
        if "options_first" in predicates:
            docopt_args.update(options_first=predicates["options_first"])
        cmd_name = predicates.get('name', meth.__name__)
        try:
            args = docopt.docopt(doc, [cmd_name] + data, **docopt_args)
        except docopt.DocoptExit:
            if not predicates.get('quiet', False):
                self.context.privmsg(to, 'Invalid arguments.')
        else:
            uid = (cmd_name, to)
            use_client = isinstance(client, asyncio.Protocol)
            if not self.tasks[uid].done():
                self.context.notice(
                    client if use_client else client.nick,
                    "Another task is already running. "
                    "Please be patient and don't flood me", nowait=True)
            elif not self.handles[uid].done() and self.antiflood:
                self.context.notice(
                    client if use_client else client.nick,
                    "Please be patient and don't flood me", nowait=True)
            else:
                # get command result
                res = self.guard(predicates, meth, client, target, args=args)

                callback = functools.partial(self.command_callback, uid, to)
                if res is not None:
                    if (asyncio.iscoroutinefunction(meth) or
                       asyncio.iscoroutinefunction(self.guard.__call__)):
                        task = asyncio.async(res, loop=self.context.loop)
                        # use a callback if command is a coroutine
                        task.add_done_callback(callback)
                        self.tasks[uid] = task
                        return task
                    else:
                        # no callback needed
                        callback(res)
Exemplo n.º 2
0
    def do_command(self, predicates, meth, client, target, data=None, **kw):
        nick = self.context.nick or '-'
        to = target == nick and client.nick or target
        doc = meth.__doc__ or ''
        doc = [l.strip() for l in doc.strip().split('\n')]
        doc = [nick + ' ' + l.strip('%%')
               for l in doc if l.startswith('%%')]
        doc = 'Usage:' + '\n    ' + '\n    '.join(doc)
        encoding = self.context.encoding
        if data:
            if not isinstance(data, str):  # pragma: no cover
                data = data.encode(encoding)
        data = data and data.split() or []
        docopt_args = dict(help=False)
        if "options_first" in predicates:
            docopt_args.update(options_first=predicates["options_first"])
        cmd_name = predicates.get('name', meth.__name__)
        try:
            args = docopt.docopt(doc, [cmd_name] + data, **docopt_args)
        except docopt.DocoptExit:
            self.context.privmsg(to, 'Invalid arguments.')
        else:
            uid = (cmd_name, to)
            use_client = isinstance(client, asyncio.Protocol)
            if not self.tasks[uid].done():
                self.context.notice(
                    client if use_client else client.nick,
                    "Another task is already running. "
                    "Please be patient and don't flood me")
            elif not self.handles[uid].done() and self.antiflood:
                self.context.notice(
                    client if use_client else client.nick,
                    "Please be patient and don't flood me")
            else:
                if not PY3:  # pragma: no cover
                    # back to unicode
                    for k, v in args.items():
                        if isinstance(v, list):
                            args[k] = [s.decode(encoding) for s in v]
                        elif v not in (None, True, False):
                            args[k] = v.decode(encoding)
                # get command result
                res = self.guard(predicates, meth, client, target, args=args)

                callback = functools.partial(self.command_callback, uid, to)
                if res is not None:
                    if (asyncio.iscoroutinefunction(meth) or
                       asyncio.iscoroutinefunction(self.guard.__call__)):
                        task = asyncio.async(res, loop=self.context.loop)
                        # use a callback if command is a coroutine
                        task.add_done_callback(callback)
                        self.tasks[uid] = task
                        return task
                    else:
                        # no callback needed
                        callback(res)
Exemplo n.º 3
0
 def __init__(self, regexp, callback=None, iotype='in',
              venusian_category='irc3.rfc1459'):
     try:
         re.compile(getattr(regexp, 're', regexp))
     except Exception as e:
         raise e.__class__(str(e) + ' in ' + getattr(regexp, 're', regexp))
     self.regexp = regexp
     self.iotype = iotype
     self.callback = callback
     self.venusian_category = venusian_category
     self.iscoroutine = False
     if callback is not None:
         self.iscoroutine = asyncio.iscoroutinefunction(callback)
Exemplo n.º 4
0
    def do_command(self, predicates, meth, client, target, data=None, **kw):
        nick = self.context.nick or '-'
        to = client.nick if target == nick else target
        doc = meth.__doc__ or ''
        doc = [l.strip() for l in doc.strip().split('\n')]
        doc = [nick + ' ' + l.strip('%%')
               for l in doc if l.startswith('%%')]
        doc = 'Usage:' + '\n    ' + '\n    '.join(doc)
        if data:
            if not isinstance(data, str):  # pragma: no cover
                encoding = self.context.encoding
                data = data.encode(encoding)
        try:
            data = self.split_command(
                data, use_shlex=predicates.get('use_shlex', self.use_shlex))
        except ValueError as e:
            if not predicates.get('quiet', False):
                self.context.privmsg(to, 'Invalid arguments: {}.'.format(e))
            return
        docopt_args = dict(help=False)
        if "options_first" in predicates:
            docopt_args.update(options_first=predicates["options_first"])
        cmd_name = predicates.get('name', meth.__name__)
        try:
            args = docopt.docopt(doc, [cmd_name] + data, **docopt_args)
        except docopt.DocoptExit as exc:
            if not predicates.get('quiet', False):
                args = {'cmd': cmd_name, 'args': data,
                        'args_str': " ".join(data), 'exc': exc}
                error_format = predicates.get('error_format',
                                              self.error_format)
                self.context.privmsg(to, error_format(**args))
        else:
            uid = (cmd_name, to)
            use_client = isinstance(client, asyncio.Protocol)
            if not self.tasks[uid].done():
                self.context.notice(
                    client if use_client else client.nick,
                    "Another task is already running. "
                    "Please be patient and don't flood me", nowait=True)
            elif not self.handles[uid].done() and self.antiflood:
                self.context.notice(
                    client if use_client else client.nick,
                    "Please be patient and don't flood me", nowait=True)
            else:
                # get command result
                res = self.guard(predicates, meth, client, target, args=args)

                callback = functools.partial(self.command_callback, uid, to)
                if res is not None:
                    coros = (
                        asyncio.iscoroutinefunction(meth),
                        asyncio.iscoroutinefunction(self.guard.__call__)
                    )
                    if any(coros):
                        task = asyncio.ensure_future(
                            res, loop=self.context.loop)
                        # use a callback if command is a coroutine
                        task.add_done_callback(callback)
                        self.tasks[uid] = task
                        return task
                    else:
                        # no callback needed
                        callback(res)