<commit_before>#!/usr/bin/env python
# -- Content-Encoding: UTF-8 --
"""
COHORTE Composer Core: Event dispatcher

:author: <NAME>
:license: GPLv3
"""

# Documentation strings format
__docformat__ = "restructuredtext en"

# Boot module version
__version__ = "1.0.0"

# ------------------------------------------------------------------------------

# COHORTE
import cohorte.composer.core
import cohorte.composer.core.events as events
import cohorte.signals

# iPOPO Decorators
from pelix.ipopo.decorators import ComponentFactory, Requires, Validate, \
    Invalidate, Provides, Instantiate

# Standard library
import logging

# ------------------------------------------------------------------------------

_logger = logging.getLogger(__name__)

# ------------------------------------------------------------------------------

@ComponentFactory("cohorte-composer-core-dispatcher-factory")
@Provides('cohorte.agent.listener')
@Provides(cohorte.signals.SERVICE_ISOLATE_PRESENCE_LISTENER)
@Requires('_distributor', cohorte.composer.SERVICE_DISTRIBUTOR)
@Requires('_receiver', cohorte.SERVICE_SIGNALS_RECEIVER)
@Requires('_status', cohorte.composer.core.SERVICE_STATUS)
@Requires('_ratings', cohorte.composer.SERVICE_UPDATABLE_RATING,
          aggregate=True, optional=True)
@Instantiate('cohorte-composer-dispatcher')
class EventDispatcher(object):
    """
    Components events/signals dispatcher
    """
    def __init__(self):
        """
        Sets up members
        """
        # Injected services
        self._distributor = None
        self._receiver = None
        self._status = None
        self._ratings = []


    def handle_component_event(self, isolate_uid, node, component_uid, kind):
        """
        Called by an agent when a component event occurs
        
        :param isolate_uid: Component host isolate UID
        :param node: Isolate node
        :param component_uid: Component UID
        :param kind: Kind of event
        :return: The original component bean if it needs to be redistributed,
                 else None
        """
        if kind and kind[0] == '/':
            kind = kind[1:]

        if kind in ("instantiated", "running"):
            # Use the UID of the isolate
            component = self._status.get_component(component_uid)
            component.isolate = isolate_uid

        elif kind in ("validated", "invalidated"):
            # Update status
            self._status.component_event(component_uid, "/" + kind)

        elif kind in ("gone", "lost"):
            # Component lost
            # Update status
            component = self._status.get_component(component_uid)
            self._status.remove_component(component.uid)

            # Update ratings
            event = events.BasicEvent(events.BasicEvent.COMPONENT_GONE,
                                      isolate_uid, node,
                                      {"uid": component_uid})
            self.__update_ratings(event)

            # Require a new distribution
            return component.original

        else:
            _logger.error("Unknown kind of component event: %s", kind)<commit_msg>Protection against components UID absent of the status storage<commit_after>#!/usr/bin/env python
# -- Content-Encoding: UTF-8 --
"""
COHORTE Composer Core: Event dispatcher

:author: <NAME>
:license: GPLv3
"""

# Documentation strings format
__docformat__ = "restructuredtext en"

# Boot module version
__version__ = "1.0.0"

# ------------------------------------------------------------------------------

# COHORTE
import cohorte.composer.core
import cohorte.composer.core.events as events
import cohorte.signals

# iPOPO Decorators
from pelix.ipopo.decorators import ComponentFactory, Requires, Validate, \
    Invalidate, Provides, Instantiate

# Standard library
import logging

# ------------------------------------------------------------------------------

_logger = logging.getLogger(__name__)

# ------------------------------------------------------------------------------

@ComponentFactory("cohorte-composer-core-dispatcher-factory")
@Provides('cohorte.agent.listener')
@Provides(cohorte.signals.SERVICE_ISOLATE_PRESENCE_LISTENER)
@Requires('_distributor', cohorte.composer.SERVICE_DISTRIBUTOR)
@Requires('_receiver', cohorte.SERVICE_SIGNALS_RECEIVER)
@Requires('_status', cohorte.composer.core.SERVICE_STATUS)
@Requires('_ratings', cohorte.composer.SERVICE_UPDATABLE_RATING,
          aggregate=True, optional=True)
@Instantiate('cohorte-composer-dispatcher')
class EventDispatcher(object):
    """
    Components events/signals dispatcher
    """
    def __init__(self):
        """
        Sets up members
        """
        # Injected services
        self._distributor = None
        self._receiver = None
        self._status = None
        self._ratings = []


    def handle_component_event(self, isolate_uid, node, component_uid, kind):
        """
        Called by an agent when a component event occurs
        
        :param isolate_uid: Component host isolate UID
        :param node: Isolate node
        :param component_uid: Component UID
        :param kind: Kind of event
        :return: The original component bean if it needs to be redistributed,
                 else None
        """
        # Get the associated component
        try:
            component = self._status.get_component(component_uid)

        except KeyError:
            _logger.warning("Unknown component: %s", component_uid)
            return None

        if kind and kind[0] == '/':
            kind = kind[1:]

        if kind in ("instantiated", "running"):
            # Use the UID of the isolate
            component.isolate = isolate_uid

        elif kind in ("validated", "invalidated"):
            # Update status
            self._status.component_event(component_uid, "/" + kind)

        elif kind in ("gone", "lost"):
            # Component lost
            # Update status
            self._status.remove_component(component.uid)

            # Update ratings
            event = events.BasicEvent(events.BasicEvent.COMPONENT_GONE,
                                      isolate_uid, node,
                                      {"uid": component_uid})
            self.__update_ratings(event)

            # Require a new distribution
            return component.original

        else:
            _logger.error("Unknown kind of component event: %s", kind)
{'fraction_non_alphanumeric': 0.15961418792781581, 'fraction_numerical': 0.0021779713752333543, 'mean_word_length': 1.975011568718186, 'pattern_counts': {'":': 2, '<': 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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '675508', 'n_tokens_mistral': 1729, 'n_tokens_neox': 1659, 'n_words': 408}