<commit_before>from .protocol import AnthemProtocol
import asyncio
import logging

__all__ = ('Connection', )

try:
    ensure_future = asyncio.ensure_future
except:
    ensure_future = asyncio.async

class Connection:
    def __init__(self):
        self.log = logging.getLogger(__name__)

    @classmethod
    @asyncio.coroutine
    def create(cls, host='localhost', port=14999,
            auto_reconnect=True, loop=None, protocol_class=AnthemProtocol,
            update_callback=None):

        assert port >= 0, "Invalid port value: %r" % (port)
        conn = cls()

        conn.host = host
        conn.port = port
        conn._loop = loop or asyncio.get_event_loop()
        conn._retry_interval = 1
        conn._closed = False
        conn._closing = False
        conn._auto_reconnect = auto_reconnect

        def connection_lost():
            if conn._auto_reconnect and not conn._closing:
                ensure_future(conn._reconnect(), loop=conn._loop)

        conn.protocol = protocol_class(connection_lost_callback=connection_lost,
                loop=conn._loop, update_callback=update_callback)

        yield from conn._reconnect()

        return conn

    @property
    def transport(self):
        return self.protocol.transport

    def _get_retry_interval(self):
        return self._retry_interval

    def _reset_retry_interval(self):
        self._retry_interval = 1

    def _increase_retry_interval(self):
        self._retry_interval = min(300, 1.5 * self._retry_interval)

    @asyncio.coroutine
    def _reconnect(self):
        while True:
            try:
                self.log.info("Connecting to Anthem AVR at %s:%d" % (self.host, self.port))
                yield from self._loop.create_connection(lambda: self.protocol, self.host, self.port)
                self._reset_retry_interval()
                return
            except OSError:
                self._increase_retry_interval()
                interval = self._get_retry_interval()
                self.log.warn('Connecting failed, retrying in %i seconds' % interval)
                yield from asyncio.sleep(interval, loop=self._loop)

    def close(self):
        self._closing = True

        if self.protocol.transport:
            self.protocol.transport.close()

    @property
    def dump_conndata(self):
        attrs = vars(self)
        return(', '.join("%s: %s" % item for item in attrs.items()))<commit_msg>Add halt and resume methods<commit_after>from .protocol import AnthemProtocol
import asyncio
import logging

__all__ = ('Connection', )

try:
    ensure_future = asyncio.ensure_future
except:
    ensure_future = asyncio.async

class Connection:
    def __init__(self):
        self.log = logging.getLogger(__name__)

    @classmethod
    @asyncio.coroutine
    def create(cls, host='localhost', port=14999,
            auto_reconnect=True, loop=None, protocol_class=AnthemProtocol,
            update_callback=None):

        assert port >= 0, "Invalid port value: %r" % (port)
        conn = cls()

        conn.host = host
        conn.port = port
        conn._loop = loop or asyncio.get_event_loop()
        conn._retry_interval = 1
        conn._closed = False
        conn._closing = False
        conn._halted = False
        conn._auto_reconnect = auto_reconnect

        def connection_lost():
            if conn._auto_reconnect and not conn._closing:
                ensure_future(conn._reconnect(), loop=conn._loop)

        conn.protocol = protocol_class(connection_lost_callback=connection_lost,
                loop=conn._loop, update_callback=update_callback)

        yield from conn._reconnect()

        return conn

    @property
    def transport(self):
        return self.protocol.transport

    def _get_retry_interval(self):
        return self._retry_interval

    def _reset_retry_interval(self):
        self._retry_interval = 1

    def _increase_retry_interval(self):
        self._retry_interval = min(300, 1.5 * self._retry_interval)

    @asyncio.coroutine
    def _reconnect(self):
        while True:
            try:
                if self._halted:
                    yield from asyncio.sleep(2, loop=self._loop)
                else:
                    self.log.info("Connecting to Anthem AVR at %s:%d" % (self.host, self.port))
                    yield from self._loop.create_connection(lambda: self.protocol, self.host, self.port)
                    self._reset_retry_interval()
                    return

            except OSError:
                self._increase_retry_interval()
                interval = self._get_retry_interval()
                self.log.warn('Connecting failed, retrying in %i seconds' % interval)
                yield from asyncio.sleep(interval, loop=self._loop)

    def close(self):
        self.log.warn('Closing connection to AVR')
        self._closing = True
        if self.protocol.transport:
            self.protocol.transport.close()

    def halt(self):
        self.log.warn('Halting connection to AVR')
        self._halted = True
        if self.protocol.transport:
            self.protocol.transport.close()

    def resume(self):
        self.log.warn('Resuming connection to AVR')
        self._halted = False

    @property
    def dump_conndata(self):
        attrs = vars(self)
        return(', '.join("%s: %s" % item for item in attrs.items()))
{'fraction_non_alphanumeric': 0.09187476705180768, 'fraction_numerical': 0.005031680954155795, 'mean_word_length': 2.2350813743218807, 'pattern_counts': {'":': 0, '<': 3, '<?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': '3269106', 'n_tokens_mistral': 1553, 'n_tokens_neox': 1491, 'n_words': 341}