Exemple #1
0
    async def branch_processor(self, obj: dict, client_info: dict,
                               middleware_args: list):
        """ Process messages for users in branch storage
        #TODO Make a simple branch extendable processor
        #TODO Move middleware_args
        """
        obj["text"] = sub(r"\[club" + str(self.group_id) + r"\|.*?\] ", "",
                          obj["text"])

        message = Message(**obj, client_info=client_info)
        branch_checkup_key = message.dict()[self.branch.checkup_key.value]

        logger.debug(
            '-> BRANCHED MESSAGE FROM {} TEXT "{}"',
            message.peer_id,
            message.text.replace("\n", " "),
        )

        disposal, branch = await self.branch.load(branch_checkup_key)
        edited = None

        for n, member in disposal.items():
            rules = member[1]
            for rule in rules:
                if not await rule(message):
                    break
            else:
                task = types.MethodType(member[0], branch)
                args = [a for rule in rules for a in rule.context.args]
                kwargs = {
                    k: v
                    for rule in rules for k, v in rule.context.kwargs.items()
                }
                edited = await self.handler_return(
                    await task(message, *middleware_args, *args, **kwargs),
                    data={
                        "object": obj,
                        "client_info": client_info
                    },
                )
                break

        # FIXME
        if (edited is False
                and self.branch.__class__.generator is GeneratorType.DATABASE):
            if branch_checkup_key in await self.branch.queue:
                logger.debug("Updating branch context")
                await self.branch.add(branch_checkup_key, branch.key,
                                      **branch.context)

        logger.info(
            'New BRANCHED "{0}" compiled with branch <{2}> (from: {1})'.format(
                message.text.replace("\n", " "),
                message.from_id,
                repr(branch.key),
            ))
Exemple #2
0
    async def _processor(self, obj: dict, client_info: dict):
        processor = dict(obj=obj, client_info=client_info)
        middleware_args = []

        message = Message(**{
            **obj, "text":
            init_bot_mention(self.group_id, obj["text"])
        },
                          client_info=client_info)

        async for mr in self.middleware.run_middleware(message):
            if self.status.middleware_expressions:
                if mr is False:
                    return
                elif mr is not None:
                    middleware_args.append(mr)

        if message.peer_id in await self.branch.queue:
            await self._branched_processor(obj, client_info, middleware_args)
            return

        logger.debug(
            '-> MESSAGE FROM {} TEXT "{}"',
            message.from_id,
            message.text.replace("\n", " "),
        )

        task = None
        for rules in self.on.rules:

            for rule in rules:
                if not await rule(message):
                    break

            else:

                args = [a for rule in rules for a in rule.context.args]
                kwargs = {
                    k: v
                    for rule in rules for k, v in rule.context.kwargs.items()
                }

                args = [message, *middleware_args, *args]

                task = await rules[0].call(*args, **kwargs)

                logger.info(
                    'New message "{}" compiled with decorator <{}> (from: {}/{})'
                    .format(
                        message.text.replace("\n", " "),
                        rules[0].call.__name__,
                        message.peer_id,
                        message.from_id,
                    ))
                break

        await self._handler_return(task, **processor)
Exemple #3
0
    async def _branched_processor(self, obj: dict, client_info: dict,
                                  middleware_args: list):
        """
        Branched messages processor manager
        :param obj: VK Server Event Object
        """
        obj["text"] = sub(r"\[club" + str(self.group_id) + r"\|.*?\] ", "",
                          obj["text"])

        answer = Message(**obj, client_info=client_info)

        logger.debug(
            '-> BRANCHED MESSAGE FROM {} TEXT "{}"',
            answer.peer_id,
            answer.text.replace("\n", " "),
        )

        disposal, branch = await self.branch.load(answer.peer_id)
        edited = None

        for n, member in disposal.items():
            rules = member[1]
            for rule in rules:
                if not await rule(answer):
                    break
            else:
                task = types.MethodType(member[0], branch)
                args = [a for rule in rules for a in rule.context.args]
                kwargs = {
                    k: v
                    for rule in rules for k, v in rule.context.kwargs.items()
                }
                edited = await self._handler_return(
                    await task(answer, *middleware_args, *args, **kwargs),
                    obj,
                    client_info,
                )
                break

        if edited is False and self.branch.generator is GeneratorType.DATABASE:
            if answer.peer_id in await self.branch.queue:
                await self.branch.add(answer.peer_id, branch.key,
                                      **branch.context)

        logger.info(
            'New BRANCHED "{0}" compiled with branch <{2}> (from: {1})'.format(
                answer.text.replace("\n", " "),
                answer.from_id,
                '"{}" with {} kwargs'.format(
                    branch.key,
                    branch.context if len(branch.context) < 100 else
                    branch.context[1:99] + "...",
                ),
            ))
Exemple #4
0
    async def message_processor(self, obj: dict, client_info: dict):
        processor = dict(object=obj, client_info=client_info)
        middleware_args = []

        message = Message(
            **{
                **obj, "text": init_bot_mention(self.group_id, obj["text"])
            },
            client_info=client_info,
        )
        branch_checkup_key = message.dict()[self.branch.checkup_key.value]

        async for mr in self.middleware.run_middleware(
                message, flag=MiddlewareFlags.PRE):
            if self.status.middleware_expressions:
                if mr is False:
                    return
                elif mr is not None:
                    middleware_args.append(mr)

        if branch_checkup_key in await self.branch.queue:
            await self.branch_processor(obj, client_info, middleware_args)
            return

        logger.debug(
            '-> MESSAGE FROM {} TEXT "{}"',
            message.from_id,
            message.text.replace("\n", " "),
        )

        for rules in self.handler.message_rules:

            for rule in rules:
                if not await rule(message):
                    break

            else:

                args = [a for rule in rules for a in rule.context.args]
                kwargs = {
                    k: v
                    for rule in rules for k, v in rule.context.kwargs.items()
                }
                args = [message, *middleware_args, *args]

                task = await rules[0].call(*args, **kwargs)
                await self.handler_return(task, data=processor)

                logger.info(
                    'New message "{}" compiled with decorator <{}> (from: {}/{})'
                    .format(
                        message.text.replace("\n", " "),
                        rules[0].call.__name__,
                        message.peer_id,
                        message.from_id,
                    ))
                break

        async for mr in self.middleware.run_middleware(message,
                                                       MiddlewareFlags.POST,
                                                       *middleware_args):
            logger.debug(f"POST Middleware handler returned: {mr}")