<commit_before>from typing import Optional, List, Dict, Tuple

from math import ceil

from constants import MAX_LENGHT
from vk_plus_utils import Wait, EventType
from vk_plus_utils import MessageEventData, Attachment


class Message(object):
    """Класс, объект которого передаётся в плагин для упрощённого ответа"""

    __slots__ = ('message_data', 'api', 'is_multichat', 'chat_id', 'user_id', 'is_out', 'true_user_id',
                 'timestamp', 'answer_values', 'msg_id', 'text', 'full_text', 'data', 'is_event',
                 'brief_attaches', 'brief_forwarded', '_full_attaches', '_full_forwarded',
                 'reserved_by', 'occupied_by', 'peer_id', "is_forwarded", 'true_msg_id')

    def __init__(self, vk_api_object, message_data):
        self.message_data = message_data
        self.api = vk_api_object

        self.reserved_by = []
        self.occupied_by = []
        self.data = {}

        self.is_event = False
        self.is_multichat = message_data.is_multichat
        self.is_forwarded = message_data.is_forwarded

        self.user_id = message_data.user_id
        self.true_user_id = message_data.true_user_id
        self.chat_id = message_data.chat_id
        self.peer_id = (message_data.chat_id or message_data.user_id) + self.is_multichat * 2000000000
        self.full_text = message_data.full_text
        self.text = self.full_text.lower().replace("&quot;", "\"")

        self.msg_id = message_data.msg_id
        self.true_msg_id = message_data.true_msg_id
        self.is_out = message_data.is_out

        self.timestamp = message_data.time

        self.brief_forwarded = message_data.forwarded
        self._full_forwarded = None
        self.brief_attaches = message_data.attaches
        self._full_attaches = None

        if self.is_multichat:
            self.answer_values = {'chat_id': self.chat_id}

        else:
            self.answer_values = {'user_id': self.user_id}

    async def get_full_attaches(self):
        """Get list of all attachments as `Attachment` for this message"""

        if self._full_attaches is None:
            await self.get_full_data()

        return self._full_attaches

    async def get_full_forwarded(self):
        """Get list of all forwarded messages as `Message` for this message"""

        if self._full_forwarded is None:
            await self.get_full_data()

        return self._full_forwarded

    async def get_full_data(self, message_data=None):
        """Update lists of all forwarded messages and all attachments for this message"""

        self._full_attaches = []
        self._full_forwarded = []

        if not message_data:
            values = {'message_ids': self.msg_id}

            full_message_data = await self.api.messages.getById(**values)

            if not full_message_data or not full_message_data['items']:  # Если пришёл пустой ответ от VK API
                return

            message = full_message_data['items'][0]

        else:
            message = message_data

        if "attachments" in message:
            for raw_attach in message["attachments"]:
                attach = Attachment.from_raw(raw_attach)  # Создаём аттач

                self._full_attaches.append(attach)  # Добавляем к нашему внутреннему списку аттачей

        if 'fwd_messages' in message:
            self._full_forwarded, self.brief_forwarded = await self.parse_forwarded_messages(message)

    async def parse_forwarded_messages(self, im):
        if 'fwd_messages' not in im:
            return (), ()

        result = []
        brief_result = []

        for mes in im['fwd_messages']:
            obj = MessageEventData.from_message_body(mes)

            obj.msg_id = self.msg_id
            obj.chat_id = self.chat_id
            obj.user_id = self.user_id
            obj.is_multichat = self.is_multichat
            obj.is_out = self.is_out
            obj.is_forwarded = True

            m = await Message.create(self.api, obj)

            big_result, small_result = await self.parse_forwarded_messages(mes)

            result.append((m, big_result))
            brief_result.append((m.msg_id, small_result))

        return tuple(result), tuple(brief_result)

    @staticmethod
    def prepare_message(message):
        """Split message to parts that can be send by `messages.send`"""

        message_length = len(message)

        if message_length <= MAX_LENGHT:
            return [message]

        def fit_parts(sep):
            current_length = 0
            current_message = ""

            sep_length = len(sep)
            c = 0
            parts = message.split(sep)
            length = len(parts)

            for j in range(length):
                m = parts[j]
                temp_length = len(m)

                if temp_length > MAX_LENGHT:
                    return

                if j != length - 1 and current_length + temp_length + sep_length <= MAX_LENGHT:
                    current_message += m + sep
                    current_length += temp_length + sep_length

                elif current_length + temp_length <= MAX_LENGHT:
                    current_message += m
                    current_length += temp_length

                elif current_length + temp_length > MAX_LENGHT:
                    yield current_message

                    current_length = temp_length
                    current_message = m

                    if j != length - 1 and current_length + sep_length < MAX_LENGHT:
                        current_message += sep
                        current_length += sep_length<commit_msg>2 new plugins; small fixes; ability to turn transilt on<commit_after>from typing import Optional, List, Dict, Tuple

from math import ceil

from constants import MAX_LENGHT
from vk_plus_utils import Wait, EventType
from vk_plus_utils import MessageEventData, Attachment


class Message(object):
    """Класс, объект которого передаётся в плагин для упрощённого ответа"""

    __slots__ = ('message_data', 'api', 'is_multichat', 'chat_id', 'user_id', 'is_out', 'true_user_id',
                 'timestamp', 'answer_values', 'msg_id', 'text', 'full_text', 'data', 'is_event',
                 'brief_attaches', 'brief_forwarded', '_full_attaches', '_full_forwarded',
                 'reserved_by', 'occupied_by', 'peer_id', "is_forwarded", 'true_msg_id')

    def __init__(self, vk_api_object, message_data):
        self.message_data = message_data
        self.api = vk_api_object

        self.reserved_by = []
        self.occupied_by = []
        self.data = {}

        self.is_event = False
        self.is_multichat = message_data.is_multichat
        self.is_forwarded = message_data.is_forwarded

        self.user_id = message_data.user_id
        self.true_user_id = message_data.true_user_id
        self.chat_id = message_data.chat_id
        self.peer_id = (message_data.chat_id or message_data.user_id) + self.is_multichat * 2000000000
        self.full_text = message_data.full_text
        self.text = self.full_text.lower().replace("&quot;", "\"")

        self.msg_id = message_data.msg_id
        self.true_msg_id = message_data.true_msg_id
        self.is_out = message_data.is_out

        self.timestamp = message_data.time

        self.brief_forwarded = message_data.forwarded
        self._full_forwarded = None
        self.brief_attaches = message_data.attaches
        self._full_attaches = None

        if self.is_multichat:
            self.answer_values = {'chat_id': self.chat_id}

        else:
            self.answer_values = {'user_id': self.user_id}

    async def get_full_attaches(self):
        """Get list of all attachments as `Attachment` for this message"""

        if self._full_attaches is None:
            await self.get_full_data()

        return self._full_attaches

    async def get_full_forwarded(self):
        """Get list of all forwarded messages as `Message` for this message"""

        if self._full_forwarded is None:
            await self.get_full_data()

        return self._full_forwarded

    async def get_full_data(self, message_data=None):
        """Update lists of all forwarded messages and all attachments for this message"""

        self._full_attaches = []
        self._full_forwarded = []

        if not message_data:
            values = {'message_ids': self.msg_id}

            full_message_data = await self.api.messages.getById(**values)

            if not full_message_data or not full_message_data['items']:  # Если пришёл пустой ответ от VK API
                return

            message = full_message_data['items'][0]

        else:
            message = message_data

        if "attachments" in message:
            for raw_attach in message["attachments"]:
                attach = Attachment.from_raw(raw_attach)  # Создаём аттач

                self._full_attaches.append(attach)  # Добавляем к нашему внутреннему списку аттачей

        if 'fwd_messages' in message:
            self._full_forwarded, self.brief_forwarded = await self.parse_forwarded_messages(message)

    async def parse_forwarded_messages(self, im):
        if 'fwd_messages' not in im:
            return (), ()

        result = []
        brief_result = []

        for mes in im['fwd_messages']:
            obj = MessageEventData.from_message_body(mes)

            obj.msg_id = self.msg_id
            obj.chat_id = self.chat_id
            obj.user_id = self.user_id
            obj.is_multichat = self.is_multichat
            obj.is_out = self.is_out
            obj.is_forwarded = True

            m = await Message.create(self.api, obj)

            big_result, small_result = await self.parse_forwarded_messages(mes)

            result.append((m, big_result))
            brief_result.append((m.msg_id, small_result))

        return tuple(result), tuple(brief_result)

    @staticmethod
    def prepare_message(message):
        """Split message to parts that can be send by `messages.send`"""

        message_length = len(message)

        if message_length <= MAX_LENGHT:
            return [message]

        def fit_parts(sep):
            current_length = 0
            current_message = ""

            sep_length = len(sep)
            parts = message.split(sep)
            length = len(parts)

            for j in range(length):
                m = parts[j]
                temp_length = len(m)

                if temp_length > MAX_LENGHT:
                    return

                if j != length - 1 and current_length + temp_length + sep_length <= MAX_LENGHT:
                    current_message += m + sep
                    current_length += temp_length + sep_length

                elif current_length + temp_length <= MAX_LENGHT:
                    current_message += m
                    current_length += temp_length

                elif current_length + temp_length > MAX_LENGHT:
                    yield current_message

                    current_length = temp_length
                    current_message = m

                    if j != length - 1 and current_length + sep_length < MAX_LENGHT:
                        current_message += sep
                        current_length += sep_length
{'fraction_non_alphanumeric': 0.08401785714285714, 'fraction_numerical': 0.0026785714285714286, 'mean_word_length': 2.1157162726008343, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '570795', 'n_tokens_mistral': 3368, 'n_tokens_neox': 3229, 'n_words': 744}