# Path: hedwig/utils.py
# LOGGER = logging.getLogger(__name__)
# def import_obj(cb):
# 
# Path: hedwig/core/base.py
# class Base(object):
#     def __init__(self, settings):
#         LOGGER.info("Base init for class - {0}".format(self.__class__.__name__))
#         self.settings = settings
#         LOGGER.debug("Setting credentials")
#         credentials = pika.PlainCredentials(username=self.settings.USERNAME,
#                                             password=self.settings.PASSWORD)
#         LOGGER.debug("Setting Connection params")
#         self.conn_params = pika.ConnectionParameters(host=self.settings.HOST, port=self.settings.PORT,
#                                                      virtual_host=self.settings.VHOST, credentials=credentials,
#                                                      heartbeat_interval=self.settings.HEARTBEAT_INTERVAL,
#                                                      socket_timeout=self.settings.SOCKET_TIMEOUT)
# 
#         self.connection = None
#         self.channel = None
# 
#     def create_channel(self):
#         LOGGER.debug("Creating a channel")
#         channel = self.connection.channel()
#         LOGGER.debug(
#             "Declaring {0} exchange - {1}".format(self.settings.EXCHANGE_TYPE, self.settings.EXCHANGE)
#         )
#         channel.exchange_declare(exchange=self.settings.EXCHANGE, exchange_type=self.settings.EXCHANGE_TYPE, durable=True)
#         return channel
# 
#     def get_channel(self):
# 
#         self.connect()
#         if self.channel is None or not self.channel.is_open:
#             LOGGER.info("No active channel found, creating a channel")
#             self.channel = self.create_channel()
#         return self.channel
# 
#     def connect(self):
#         LOGGER.debug("Making a blocking connection")
#         if self.connection is None or not self.connection.is_open:
#             LOGGER.info("No active connection found, creating a connection")
#             self.connection = pika.BlockingConnection(self.conn_params)
# 
#     def close_channel(self):
#         LOGGER.debug("Trying to close channel if open")
#         if self.channel and self.channel.is_open:
#             LOGGER.debug("Closing channel")
#             try:
#                 self.channel.cancel()
#                 self.channel.close()
#             except ConnectionClosed:
#                 self.channel = None
# 
#     def shutdown(self):
#         LOGGER.info("Shutdown called")
#         self.close_channel()
#         if self.connection and self.connection.is_open:
#             LOGGER.info("Closing connection")
#             try:
#                 self.connection.close()
#             except ConnectionClosed:
#                 self.connection = None
# 
# Path: hedwig/core/exceptions.py
# class IllegalOperation(Exception):
#     pass
# 
# Path: hedwig/core/settings.py
# DEFAULT_QUEUE_SETTINGS = {
#     "DURABLE": True,
#     "AUTO_DELETE": False,
#     "EXCLUSIVE": False,
#     "NO_ACK": False,
# }


import logging
import inspect
from future.utils import iteritems
from pika.exceptions import ConnectionClosed
from hedwig import utils
from hedwig.core.base import Base
from hedwig.core.exceptions import IllegalOperation
from hedwig.core.settings import DEFAULT_QUEUE_SETTINGS
        """
        Consumes messages from RabbitMQ
        :return:
        :raises KeyboardInterrupt: if one was sent to it
        :raises Exception: if RAISE_EXCEPTION on CONSUMER is true
        """
        LOGGER.debug('Consumer Initialized')
        # self.connect()
        channel = self.get_channel()
        self._bind_things(channel)

        try:
            LOGGER.info('Start consuming')
            channel.start_consuming()
        except ConnectionClosed:
            LOGGER.exception('Pika connection closed detected. Will attempt to start consuming again')
            self.consume()
        except KeyboardInterrupt as e:
            LOGGER.info('Keyboard interrupt, stop consuming')
            self.shutdown()
            raise
        except Exception as e:
            LOGGER.exception("'%s" % str(e))
            self.shutdown()
            if self.settings.CONSUMER['RAISE_EXCEPTION']:
                LOGGER.info("CONSUMER RAISED EXCEPTION")
                raise

    def add_queue(self, q_setting, q_name=None):
        if self.channel is not None:

# Path: hedwig/utils.py
# LOGGER = logging.getLogger(__name__)
# def import_obj(cb):
# 
# Path: hedwig/core/base.py
# class Base(object):
#     def __init__(self, settings):
#         LOGGER.info("Base init for class - {0}".format(self.__class__.__name__))
#         self.settings = settings
#         LOGGER.debug("Setting credentials")
#         credentials = pika.PlainCredentials(username=self.settings.USERNAME,
#                                             password=self.settings.PASSWORD)
#         LOGGER.debug("Setting Connection params")
#         self.conn_params = pika.ConnectionParameters(host=self.settings.HOST, port=self.settings.PORT,
#                                                      virtual_host=self.settings.VHOST, credentials=credentials,
#                                                      heartbeat_interval=self.settings.HEARTBEAT_INTERVAL,
#                                                      socket_timeout=self.settings.SOCKET_TIMEOUT)
# 
#         self.connection = None
#         self.channel = None
# 
#     def create_channel(self):
#         LOGGER.debug("Creating a channel")
#         channel = self.connection.channel()
#         LOGGER.debug(
#             "Declaring {0} exchange - {1}".format(self.settings.EXCHANGE_TYPE, self.settings.EXCHANGE)
#         )
#         channel.exchange_declare(exchange=self.settings.EXCHANGE, exchange_type=self.settings.EXCHANGE_TYPE, durable=True)
#         return channel
# 
#     def get_channel(self):
# 
#         self.connect()
#         if self.channel is None or not self.channel.is_open:
#             LOGGER.info("No active channel found, creating a channel")
#             self.channel = self.create_channel()
#         return self.channel
# 
#     def connect(self):
#         LOGGER.debug("Making a blocking connection")
#         if self.connection is None or not self.connection.is_open:
#             LOGGER.info("No active connection found, creating a connection")
#             self.connection = pika.BlockingConnection(self.conn_params)
# 
#     def close_channel(self):
#         LOGGER.debug("Trying to close channel if open")
#         if self.channel and self.channel.is_open:
#             LOGGER.debug("Closing channel")
#             try:
#                 self.channel.cancel()
#                 self.channel.close()
#             except ConnectionClosed:
#                 self.channel = None
# 
#     def shutdown(self):
#         LOGGER.info("Shutdown called")
#         self.close_channel()
#         if self.connection and self.connection.is_open:
#             LOGGER.info("Closing connection")
#             try:
#                 self.connection.close()
#             except ConnectionClosed:
#                 self.connection = None
# 
# Path: hedwig/core/exceptions.py
# class IllegalOperation(Exception):
#     pass
# 
# Path: hedwig/core/settings.py
# DEFAULT_QUEUE_SETTINGS = {
#     "DURABLE": True,
#     "AUTO_DELETE": False,
#     "EXCLUSIVE": False,
#     "NO_ACK": False,
# }


# Path: hedwig/core/consumer.py
import logging
import inspect
from future.utils import iteritems
from pika.exceptions import ConnectionClosed
from hedwig import utils
from hedwig.core.base import Base
from hedwig.core.exceptions import IllegalOperation
from hedwig.core.settings import DEFAULT_QUEUE_SETTINGS

        """
        Consumes messages from RabbitMQ
        :return:
        :raises KeyboardInterrupt: if one was sent to it
        :raises Exception: if RAISE_EXCEPTION on CONSUMER is true
        """
        LOGGER.debug('Consumer Initialized')
        # self.connect()
        channel = self.get_channel()
        self._bind_things(channel)

        try:
            LOGGER.info('Start consuming')
            channel.start_consuming()
        except ConnectionClosed:
            LOGGER.exception('Pika connection closed detected. Will attempt to start consuming again')
            self.consume()
        except KeyboardInterrupt as e:
            LOGGER.info('Keyboard interrupt, stop consuming')
            self.shutdown()
            raise
        except Exception as e:
            LOGGER.exception("'%s" % str(e))
            self.shutdown()
            if self.settings.CONSUMER['RAISE_EXCEPTION']:
                LOGGER.info("CONSUMER RAISED EXCEPTION")
                raise

    def add_queue(self, q_setting, q_name=None):
        if self.channel is not None:

            raise IllegalOperation("Should not add to queue after channel is created."