Exemplo n.º 1
0
def sendEventToPlugins(anevent, metadata, pluginList):
    '''compare the event to the plugin registrations.
       plugins register with a list of keys or values
       or values they want to match on
       this function compares that registration list
       to the current event and sends the event to plugins
       in order
    '''
    if not isinstance(anevent, dict):
        raise TypeError('event is type {0}, should be a dict'.format(
            type(anevent)))

    # expecting tuple of module,criteria,priority in pluginList
    # sort the plugin list by priority
    for plugin in sorted(pluginList, key=itemgetter(2), reverse=False):
        # assume we don't run this event through the plugin
        send = False
        if isinstance(plugin[1], list):
            try:
                if (set(plugin[1]).intersection(
                    [e for e in dict2List(anevent)])):
                    send = True
            except TypeError:
                logger.error(
                    'TypeError on set intersection for dict {0}'.format(
                        anevent))
                return (anevent, metadata)
        if send:
            (anevent, metadata) = plugin[0].onMessage(anevent, metadata)
            if anevent is None:
                # plug-in is signalling to drop this message
                # early exit
                return (anevent, metadata)

    return (anevent, metadata)
Exemplo n.º 2
0
    def run_plugins(self, message, metadata=None):
        '''compare the message to the plugin registrations.
           plugins register with a list of keys or values
           or values they want to match on
           this function compares that registration list
           to the current message and sends the message to plugins
           in order
        '''
        if not isinstance(message, dict):
            raise TypeError('event is type {0}, should be a dict'.format(type(message)))

        for plugin in self.ordered_enabled_plugins:
            send = False
            message_fields = [e for e in dict2List(message)]
            # this is to make it so we can match on all fields
            message_fields.append('*')
            if isinstance(plugin['registration'], list):
                if set(plugin['registration']).intersection(message_fields):
                    send = True
            elif isinstance(plugin['registration'], str):
                if plugin['registration'] in message_fields:
                    send = True
            if send:
                try:
                    (message, metadata) = self.send_message_to_plugin(plugin_class=plugin['plugin_class'], message=message, metadata=metadata)
                except Exception as e:
                    logger.exception('Received exception in {0}: message: {1}\n{2}'.format(plugin['plugin_class'], message, e.message))
                if message is None:
                    return (message, metadata)
        return (message, metadata)
Exemplo n.º 3
0
    def run_plugins(self, message, metadata=None):
        '''compare the message to the plugin registrations.
           plugins register with a list of keys or values
           or values they want to match on
           this function compares that registration list
           to the current message and sends the message to plugins
           in order
        '''
        if not isinstance(message, dict):
            raise TypeError('event is type {0}, should be a dict'.format(
                type(message)))

        for plugin in self.ordered_enabled_plugins:
            send = False
            message_fields = [e for e in dict2List(message)]
            # this is to make it so we can match on all fields
            message_fields.append('*')
            if isinstance(plugin['registration'], list):
                if set(plugin['registration']).intersection(message_fields):
                    send = True
            elif isinstance(plugin['registration'], str):
                if plugin['registration'] in message_fields:
                    send = True
            if send:
                try:
                    (message, metadata) = self.send_message_to_plugin(
                        plugin_class=plugin['plugin_class'],
                        message=message,
                        metadata=metadata)
                except Exception as e:
                    logger.error(
                        'Received exception in {0}: message: {1}\n{2}'.format(
                            plugin['plugin_class'], message, e.message))
                if message is None:
                    return (message, metadata)
        return (message, metadata)