<commit_before>import functools

import controller
import entry_point
import exceptions
import messages
import proxies
import router


class Dispatcher(controller.AbstractController):

    """
    Dispatches incoming requests to the handler method in class.
    Dispatcher is the class property for each service controller.
    In that class ii find handler method to handle request.
    """

    def __init__(self):

        self._handlers = {
            "request": {},
            "response": {},
            "error": {},
            "notification": {}
        }

    @property
    def handlers(self):
        return self._handlers

    @property
    def subscriptions(self):
        return self._handlers["notification"]

    def register(self, entry_point, message_type, handler_method_name):
        """
        Registers for given message type and entry_point a handler

        :param entry_point: any EntryPoint object
        :type entry_point: EntryPoint
        :param message_type: type of incoming message
        :type message_type: string
        :param method_name: name of handler method
        :type method_name: string
        """
        ep = str(entry_point)
        if ep in self._handlers[message_type]:
            raise exceptions.DuplicatedEntryPointRegistration(entry_point=ep)
        if handler_method_name in self._handlers[message_type].values():
            raise exceptions.DuplicatedMethodRegistration(
                method_name=handler_method_name)
        self._handlers[message_type][ep] = handler_method_name

    def get_handler(self, entry_point, message_type):
        """
        Return handler that defined for given entry_point and message_type

        :param ep: any EntryPoint object
        :type ep: EntryPoint
        :param message_type: type of incoming message
        :type message_type: string
        :return:name of handler method
        :rtype: string
        """
        ep = str(entry_point)
        if message_type not in self._handlers or \
                ep not in self._handlers[message_type]:
            raise exceptions.HandlerNotFound(entry_point=str(ep),
                                             message_type=message_type)
        return self._handlers[message_type][ep]

    def get_publishers(self):
        for ep in self.subscriptions.keys():
            yield entry_point.EntryPointFactory().create(ep)

    def get_request_entry_services(self):
        for ep in self._handlers["request"].keys():
            yield entry_point.EntryPointFactory().create(ep).service

    def _get_dispatching_entry_point(self, message):
        """
        Defines by what header message should be dispatched

        :param message: IncomingRequest, IncomingError, IncomingResponse,
                        IncomingNotification
        :type message: Message
        :return: corresponding EntryPoint
        :rtype: EntryPoint
        """
        if isinstance(message, (messages.IncomingError,
                                messages.IncomingResponse)):
            return message.source
        elif isinstance(message, messages.IncomingNotification):
            return message.source
        else:
            return message.destination

    def _get_source_context(self, message, service_instance):
        if isinstance(message, (messages.IncomingError,
                                messages.IncomingResponse)):
            ep = message.destination.copy()
            return ep<commit_msg>Use copy dispatching entry point<commit_after>import functools

import controller
import entry_point
import exceptions
import messages
import proxies
import router


class Dispatcher(controller.AbstractController):

    """
    Dispatches incoming requests to the handler method in class.
    Dispatcher is the class property for each service controller.
    In that class ii find handler method to handle request.
    """

    def __init__(self):

        self._handlers = {
            "request": {},
            "response": {},
            "error": {},
            "notification": {}
        }

    @property
    def handlers(self):
        return self._handlers

    @property
    def subscriptions(self):
        return self._handlers["notification"]

    def register(self, entry_point, message_type, handler_method_name):
        """
        Registers for given message type and entry_point a handler

        :param entry_point: any EntryPoint object
        :type entry_point: EntryPoint
        :param message_type: type of incoming message
        :type message_type: string
        :param method_name: name of handler method
        :type method_name: string
        """
        ep = str(entry_point)
        if ep in self._handlers[message_type]:
            raise exceptions.DuplicatedEntryPointRegistration(entry_point=ep)
        if handler_method_name in self._handlers[message_type].values():
            raise exceptions.DuplicatedMethodRegistration(
                method_name=handler_method_name)
        self._handlers[message_type][ep] = handler_method_name

    def get_handler(self, entry_point, message_type):
        """
        Return handler that defined for given entry_point and message_type

        :param ep: any EntryPoint object
        :type ep: EntryPoint
        :param message_type: type of incoming message
        :type message_type: string
        :return:name of handler method
        :rtype: string
        """
        ep = str(entry_point)
        if message_type not in self._handlers or \
                ep not in self._handlers[message_type]:
            raise exceptions.HandlerNotFound(entry_point=str(ep),
                                             message_type=message_type)
        return self._handlers[message_type][ep]

    def get_publishers(self):
        for ep in self.subscriptions.keys():
            yield entry_point.EntryPointFactory().create(ep)

    def get_request_entry_services(self):
        for ep in self._handlers["request"].keys():
            yield entry_point.EntryPointFactory().create(ep).service

    def _get_dispatching_entry_point(self, message):
        """
        Defines by what header message should be dispatched

        :param message: IncomingRequest, IncomingError, IncomingResponse,
                        IncomingNotification
        :type message: Message
        :return: corresponding EntryPoint
        :rtype: EntryPoint
        """
        if isinstance(message, (messages.IncomingError,
                                messages.IncomingResponse)):
            return message.source.copy()
        elif isinstance(message, messages.IncomingNotification):
            return message.source
        else:
            return message.destination.copy()

    def _get_source_context(self, message, service_instance):
        if isinstance(message, (messages.IncomingError,
                                messages.IncomingResponse)):
            return message.destination
{'fraction_non_alphanumeric': 0.07171658986175115, 'fraction_numerical': 0.0, 'mean_word_length': 2.3405483405483407, 'pattern_counts': {'":': 8, '<': 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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '198087', 'n_tokens_mistral': 1704, 'n_tokens_neox': 1649, 'n_words': 477}