<commit_before># -*- coding: utf-8 -*-
#
# Copyright (C) 2012 <NAME>
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.
#

import logging
from threading import _Timer, RLock
from collections import namedtuple
from .asynclib import AsyncChat
from .records import HeaderRecord, TerminatorRecord
from .constants import STX, CRLF, ENQ, ACK, NAK, EOT, ENCODING

log = logging.getLogger(__name__)

STATE = namedtuple('ASTMState', ['init', 'opened', 'transfer'])(*range(3))

class ASTMProtocol(AsyncChat):
    """Common ASTM protocol routines."""

    #: ASTM header record class.
    astm_header = HeaderRecord
    #: ASTM terminator record class.
    astm_terminator = TerminatorRecord
    #: Flag about chunked transfer.
    is_chunked_transfer = None
    #: Operation timeout value.
    timeout = None

    use_encoding = True
    encoding = ENCODING
    strip_terminator = False
    _last_recv_data = None
    _last_sent_data = None
    _state = None
    _lock = RLock()
    _timer = None
    _timer_cls = _Timer

    def found_terminator(self):
        while self.inbox:
            data = self.inbox.popleft()
            if not data:
                continue
            self.dispatch(data)

    def dispatch(self, data):
        """Dispatcher of received data."""
        self._last_recv_data = data
        if data == ENQ:
            handler = self.on_enq
        elif data == ACK:
            handler = self.on_ack
        elif data == NAK:
            handler = self.on_nak
        elif data == EOT:
            handler = self.on_eot
        elif data.startswith(STX): # this looks like a message
            handler = self.on_message
        else:
            handler = lambda: self.default_handler(data)

        with self._lock:
            resp = handler()
            self.start_timer()

        if resp is not None:
            self.push(resp)

    def default_handler(self, data):
        raise ValueError('Unable to dispatch data: %r', data)

    def push(self, data):
        self._last_sent_data = data
        return super(ASTMProtocol, self).push(data)

    def start_timer(self):
        if self.timeout is None:
            log.debug('Unable to start timer: timeout is None')
            return
        self.stop_timer()
        self._timer = self._timer_cls(self.timeout, self.on_timeout)
        self._timer.start()
        log.debug('Timer %r started', self._timer)

    def stop_timer(self):
        if self.timeout is None:
            return
        if self._timer is not None and self._timer.is_alive():
            self._timer.cancel()
        log.debug('Timer %r stopped', self._timer)
        self._timer = None

    def on_enq(self):
        """Calls on ``ENQ`` message receiving."""

    def on_ack(self):
        """Calls on ``ACK`` message receiving."""

    def on_nak(self):
        """Calls on ``NAK`` message receiving."""

    def on_eot(self):
        """Calls on ``EOT`` message receiving."""

    def on_message(self):
        """Calls on ASTM message receiving."""

    def _get_state(self):<commit_msg>Don't spam logs with useless messages.<commit_after># -*- coding: utf-8 -*-
#
# Copyright (C) 2012 <NAME>
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.
#

import logging
from threading import _Timer, RLock
from collections import namedtuple
from .asynclib import AsyncChat
from .records import HeaderRecord, TerminatorRecord
from .constants import STX, CRLF, ENQ, ACK, NAK, EOT, ENCODING

log = logging.getLogger(__name__)

STATE = namedtuple('ASTMState', ['init', 'opened', 'transfer'])(*range(3))

class ASTMProtocol(AsyncChat):
    """Common ASTM protocol routines."""

    #: ASTM header record class.
    astm_header = HeaderRecord
    #: ASTM terminator record class.
    astm_terminator = TerminatorRecord
    #: Flag about chunked transfer.
    is_chunked_transfer = None
    #: Operation timeout value.
    timeout = None

    use_encoding = True
    encoding = ENCODING
    strip_terminator = False
    _last_recv_data = None
    _last_sent_data = None
    _state = None
    _lock = RLock()
    _timer = None
    _timer_cls = _Timer

    def found_terminator(self):
        while self.inbox:
            data = self.inbox.popleft()
            if not data:
                continue
            self.dispatch(data)

    def dispatch(self, data):
        """Dispatcher of received data."""
        self._last_recv_data = data
        if data == ENQ:
            handler = self.on_enq
        elif data == ACK:
            handler = self.on_ack
        elif data == NAK:
            handler = self.on_nak
        elif data == EOT:
            handler = self.on_eot
        elif data.startswith(STX): # this looks like a message
            handler = self.on_message
        else:
            handler = lambda: self.default_handler(data)

        with self._lock:
            resp = handler()
            self.start_timer()

        if resp is not None:
            self.push(resp)

    def default_handler(self, data):
        raise ValueError('Unable to dispatch data: %r', data)

    def push(self, data):
        self._last_sent_data = data
        return super(ASTMProtocol, self).push(data)

    def start_timer(self):
        if self.timeout is None:
            return
        self.stop_timer()
        self._timer = self._timer_cls(self.timeout, self.on_timeout)
        self._timer.start()
        log.debug('Timer %r started', self._timer)

    def stop_timer(self):
        if self.timeout is None or self._timer is None:
            return
        if self._timer is not None and self._timer.is_alive():
            self._timer.cancel()
        log.debug('Timer %r stopped', self._timer)
        self._timer = None

    def on_enq(self):
        """Calls on ``ENQ`` message receiving."""

    def on_ack(self):
        """Calls on ``ACK`` message receiving."""

    def on_nak(self):
        """Calls on ``NAK`` message receiving."""

    def on_eot(self):
        """Calls on ``EOT`` message receiving."""

    def on_message(self):
        """Calls on ASTM message receiving."""

    def _get_state(self):
{'fraction_non_alphanumeric': 0.10216767612368505, 'fraction_numerical': 0.001912655403251514, 'mean_word_length': 2.3095991561181433, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2701507', 'n_tokens_mistral': 1854, 'n_tokens_neox': 1793, 'n_words': 555}