def _bind_listener(self, field, listener, svc_ref): """ A new MQTT listener has been bound """ topics = to_iterable(svc_ref.get_property(services.PROP_MQTT_TOPICS), False) for topic in topics: self.__add_listener(topic, listener)
def _unbind_listener(self, field, listener, svc_ref): """ An MQTT listener is gone """ topics = to_iterable(svc_ref.get_property(services.PROP_MQTT_TOPICS), False) for topic in topics: self.__remove_listener(topic, listener)
def __init__(self, name=None, excluded=None): """ Sets up the decorator :param name: Name of the component factory :param excluded: List of IDs of handlers which configuration must not be inherited from the parent class """ self.__factory_name = name self.__excluded_inheritance = to_iterable(excluded)
def testToIterable(self): """ Tests the to_iterable() method """ # None value self.assertIsNone(utilities.to_iterable(None, True), "None value refused") self.assertListEqual(utilities.to_iterable(None, False), [], "None value accepted") # Check iterable types for clazz in (list, tuple, set, frozenset): iterable = clazz() self.assertIs(utilities.to_iterable(iterable), iterable, "to_iterable() didn't returned the original object") # Check other types for value in ("hello", 123, {1: 2}, object()): self.assertListEqual(utilities.to_iterable(value), [value], "to_iterable() didn't returned a list")
def _get_handlers_ids(self, topic, properties): """ Retrieves the IDs of the listeners that requested to handle this event :param topic: Topic of the event :param properties: Associated properties :return: The IDs of the services to call back for this event """ handlers = [] # Get the handler service references handlers_refs = self._context.get_all_service_references( pelix.services.SERVICE_EVENT_HANDLER, None) if handlers_refs is None: # No service found return None for svc_ref in handlers_refs: # Check the LDAP filter ldap_filter = svc_ref.get_property( pelix.services.PROP_EVENT_FILTER) if self.__match_filter(properties, ldap_filter): # Get the service ID svc_id = svc_ref.get_property(pelix.constants.SERVICE_ID) # Filter matches the event, ycappuccino_core the topic topics = to_iterable( svc_ref.get_property(pelix.services.PROP_EVENT_TOPICS), True) if not topics: # Filter matches, and no topic filter given: notify it handlers.append(svc_id) else: for handled_topic in to_iterable(topics, False): if fnmatch.fnmatch(topic, handled_topic): # Full match, keep the service ID handlers.append(svc_id) break return handlers
def _get_handlers_ids(self, topic, properties): """ Retrieves the IDs of the listeners that requested to handle this event :param topic: Topic of the event :param properties: Associated properties :return: The IDs of the services to call back for this event """ handlers = [] # Get the handler service references handlers_refs = self._context.get_all_service_references( pelix.services.SERVICE_EVENT_HANDLER, None ) if handlers_refs is None: # No service found return None for svc_ref in handlers_refs: # Check the LDAP filter ldap_filter = svc_ref.get_property(pelix.services.PROP_EVENT_FILTER) if self.__match_filter(properties, ldap_filter): # Get the service ID svc_id = svc_ref.get_property(pelix.constants.SERVICE_ID) # Filter matches the event, test the topic topics = to_iterable( svc_ref.get_property(pelix.services.PROP_EVENT_TOPICS), True ) if not topics: # Filter matches, and no topic filter given: notify it handlers.append(svc_id) else: for handled_topic in to_iterable(topics, False): if fnmatch.fnmatch(topic, handled_topic): # Full match, keep the service ID handlers.append(svc_id) break return handlers
def _update_listener(self, field, listener, svc_ref, old_props): """ A listener has been updated """ old_topics = set(old_props[services.PROP_MQTT_TOPICS]) topics = set(to_iterable( svc_ref.get_property(services.PROP_MQTT_TOPICS), False)) # New topics for topic in topics.difference(old_topics): self.__add_listener(topic, listener) # Removed old ones for topic in old_topics.difference(topics): self.__remove_listener(topic, listener)