<commit_before>import logging
import pika

from delegate_base import Delegate

LOG = logging.getLogger(__name__)

class ConnectionManager(Delegate):
    _REQUIRED_DELEGATE_METHODS = ['on_connection_open', 'on_connection_closed']

    def __init__(self, url, reconnect_sleep=60, **kwargs):
        Delegate.__init__(self, **kwargs)
        self.url = url
        self.reconnect_sleep = reconnect_sleep


    def _on_connection_open(self, connection):
        self._connection = connection
        LOG.debug("adding connection close callback")
        connection.add_on_close_callback(self._on_connection_closed)

        for delegate in self.delegates:
            try:
                delegate.on_connection_open(self)
            except:
                LOG.exception('Delegating on_connection_open to %s failed',
                        delegate)

    def _on_connection_closed(self, method_frame):
        LOG.warning("Connection closed.  method_frame: %s", method_frame)

        # if you re-declare a queue or exchange with wrong params, connection
            # will be closed -- can we inspect the frame and behave differently?
        for delegate in self.delegates:
            try:
                delegate.on_connection_closed(method_frame)
            except:
                LOG.exception('Delegation of on_connection_closed to %s failed',
                        delegate)

        LOG.info("Sleeping for %d seconds before next reconnect attempt",
                self.reconnect_sleep)
        self._connection.ioloop.add_timeout(self.reconnect_sleep, self.start)


    def channel(self, *args, **kwargs):
        self._connection.channel(*args, **kwargs)


    def start(self):
        LOG.info("Attempting to connect to AMQP broker")
        self._connection = pika.SelectConnection(pika.URLParameters(self.url),
                self._on_connection_open)
        self._connection.ioloop.start()

    def stop(self):
        self._connection.close()
        self._connection.ioloop.start()<commit_msg>connection manager catchs keyboard interrupt<commit_after>import logging
import pika

from delegate_base import Delegate

LOG = logging.getLogger(__name__)

class ConnectionManager(Delegate):
    _REQUIRED_DELEGATE_METHODS = ['on_connection_open', 'on_connection_closed']

    def __init__(self, url, reconnect_sleep=60, **kwargs):
        Delegate.__init__(self, **kwargs)
        self.url = url
        self.reconnect_sleep = reconnect_sleep


    def _on_connection_open(self, connection):
        self._connection = connection
        LOG.debug("adding connection close callback")
        connection.add_on_close_callback(self._on_connection_closed)

        for delegate in self.delegates:
            try:
                delegate.on_connection_open(self)
            except:
                LOG.exception('Delegating on_connection_open to %s failed',
                        delegate)

    def _on_connection_closed(self, method_frame):
        LOG.warning("Connection closed.  method_frame: %s", method_frame)

        # if you re-declare a queue or exchange with wrong params, connection
            # will be closed -- can we inspect the frame and behave differently?
        for delegate in self.delegates:
            try:
                delegate.on_connection_closed(method_frame)
            except:
                LOG.exception('Delegation of on_connection_closed to %s failed',
                        delegate)

        LOG.info("Sleeping for %d seconds before next reconnect attempt",
                self.reconnect_sleep)
        self._connection.ioloop.add_timeout(self.reconnect_sleep, self.start)


    def channel(self, *args, **kwargs):
        self._connection.channel(*args, **kwargs)


    def start(self):
        LOG.info("Attempting to connect to AMQP broker")
        self._connection = pika.SelectConnection(pika.URLParameters(self.url),
                self._on_connection_open)
        try:
            self._connection.ioloop.start()
        except KeyboardInterrupt:
            self.stop()

    def stop(self):
        self._connection.close()
        self._connection.ioloop.start()
{'fraction_non_alphanumeric': 0.08240695988400193, 'fraction_numerical': 0.0009666505558240696, 'mean_word_length': 2.5135823429541597, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6255986', 'n_tokens_mistral': 1126, 'n_tokens_neox': 1069, 'n_words': 259}