<commit_before>#!/usr/bin/python
# -- Content-Encoding: UTF-8 --
"""
Herald Bluetooth Message Implementation

:author: <NAME>
:copyright: Copyright 2014, isandlaTech
:license: Apache License 2.0
:version: 0.0.3
:status: Alpha

..

    Copyright 2014 isandlaTech

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
"""

DELIMITER = ':'


def to_string(msg):
    """
    Take a str and returns the same object
    or take a bytes object and transform it
    to a string object (useful for comparisons)
    :param msg: message to transform to a string
    :return: resulted string
    """
    if type(msg) is bytes:
        msg = str(msg)
        msg = msg[2:]
        return msg[:-1]
    else:
        return msg


def to_bluetooth_message(msg):
    """
    put a bluetooth message to the peer.
    It adds correct delimiters
    :param msg: string message
    :return: bluetooth message as a string
    """
    msg = to_string(msg)
    return str(len(msg))+DELIMITER+msg


class MessageReader:
    """
    reads herald message from bluetooth messages
    """

    def __init__(self, automata):
        self._automata = automata
        self._buffer = []

    def read(self):
        if self._automata.any_message():
            msg = self._automata.get_message()
            print(self._buffer)
            self._buffer.append(msg)
            if len(self._buffer) >= 7:
                res = SerialHeraldMessage(*self._buffer)
                self._buffer.clear()
                return res
        return None


class SerialHeraldMessage:
    """
    Represents a bluetooth message implementation
    """

    def __init__(self,
                 subject,
                 sender_uid,
                 original_sender,
                 final_destination,<commit_msg>add hello callback for serial herald message reader<commit_after>#!/usr/bin/python
# -- Content-Encoding: UTF-8 --
"""
Herald Bluetooth Message Implementation

:author: <NAME>
:copyright: Copyright 2014, isandlaTech
:license: Apache License 2.0
:version: 0.0.3
:status: Alpha

..

    Copyright 2014 isandlaTech

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
"""

DELIMITER = ':'
HELLO_MESSAGE = b'[[[HELLO]]]'


def to_string(msg):
    """
    Take a str and returns the same object
    or take a bytes object and transform it
    to a string object (useful for comparisons)
    :param msg: message to transform to a string
    :return: resulted string
    """
    if type(msg) is bytes:
        msg = str(msg)
        msg = msg[2:]
        return msg[:-1]
    else:
        return msg


def to_bluetooth_message(msg):
    """
    put a bluetooth message to the peer.
    It adds correct delimiters
    :param msg: string message
    :return: bluetooth message as a string
    """
    msg = to_string(msg)
    return str(len(msg)) + DELIMITER + msg


class MessageReader:
    """
    reads herald message from bluetooth messages
    """

    def __init__(self, automata, hello_callback=None):
        self._automata = automata
        self._buffer = []
        self._hello_received_callback = hello_callback

    def read(self):
        if self._automata.any_message():
            msg = self._automata.get_message()
            # if there is a hello message
            if len(self._buffer) == 0:
                # if we are not into reading a new herald message
                if to_string(msg) == to_string(HELLO_MESSAGE):
                    # call the hello received callback
                    if self._hello_received_callback:
                        self._hello_received_callback()
                    # exiting before continuing in the
                    # creation of an herald message
                    return None
            self._buffer.append(msg)
            if len(self._buffer) >= 7:
                res = SerialHeraldMessage(*self._buffer)
                self._buffer.clear()
                return res
        return None


class SerialHeraldMessage:
    """
    Represents a bluetooth message implementation
    """

    def __init__(self,
                 subject,
                 sender_uid,
                 original_sender,
                 final_destination,
{'fraction_non_alphanumeric': 0.0781702374059062, 'fraction_numerical': 0.008299556070256708, 'mean_word_length': 2.2146401985111663, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '6020741', 'n_tokens_mistral': 1435, 'n_tokens_neox': 1352, 'n_words': 502}