<commit_before>import datetime
import logging
import st2reactor.ruleenforcement.enforce

from st2common.persistence.reactor import Trigger, TriggerInstance
from st2common.models.db.reactor import TriggerDB, TriggerInstanceDB

LOG = logging.getLogger('st2reactor.sensor.dispatcher')
DISPATCH_HANDLER = st2reactor.ruleenforcement.enforce.handle_trigger_instances


def __create_trigger_instance(trigger_name, payload,
                              occurrence_time=datetime.datetime.now()):
    triggers = Trigger.query(name=trigger_name)
    trigger = None if len(triggers) == 0 else triggers[0]
    if trigger is None:
        LOG.info('No trigger with name {} found.', trigger_name)
        return None
    trigger_instance = TriggerInstanceDB()
    trigger_instance.name = 'auto-generated'
    trigger_instance.trigger = trigger
    trigger_instance.payload = payload
    trigger_instance.occurrence_time = occurrence_time
    return TriggerInstance.add_or_update(trigger_instance)


def dispatch_triggers(triggers):
    """
    """
    trigger_instances = [__create_trigger_instance(
        trigger['name'],
        trigger['payload'] if 'payload' in trigger else {},
        trigger['occurrence_time'] if 'occurrence_time' in trigger else
        datetime.datetime.now())
        for trigger in triggers]
    DISPATCH_HANDLER(trigger_instances)


def dispatch_trigger(trigger):
    """
    Trigger must have named properties name, payload, occurrence_time.
    """
    dispatch_triggers([trigger])


def __create_trigger_type(name, description=None, payload_info=None):
    triggers = Trigger.query(name=name)
    trigger_type = TriggerDB()
    if len(triggers) > 0:
        trigger_type = triggers[0]
        LOG.info('Found existing trigger id:%s with name:%s. Will update '
                 'trigger.', trigger_type.id, name)
    trigger_type.name = name
    trigger_type.description = description
    trigger_type.payload_info = payload_info
    return Trigger.add_or_update(trigger_type)


def __validate_trigger_type(trigger_type):
    """
    XXX: We need validator objects that defines the required and optional fields.
    For now, manually check them.
    """
    required_fields = ['name']
    for field in required_fields:
        if field not in trigger_type:
            raise Exception('Invaid trigger type. Missing field %s' % field)


def add_trigger_type(trigger_type):
    __validate_trigger_type(trigger_type)
    __create_trigger_type(
        trigger_type['name'],
        trigger_type['description'] if 'description' in trigger_type else '',
        trigger_type['payload_info'] if 'payload_info' in trigger_type else [])<commit_msg>Fix: Grammar in comment<commit_after>import datetime
import logging
import st2reactor.ruleenforcement.enforce

from st2common.persistence.reactor import Trigger, TriggerInstance
from st2common.models.db.reactor import TriggerDB, TriggerInstanceDB

LOG = logging.getLogger('st2reactor.sensor.dispatcher')
DISPATCH_HANDLER = st2reactor.ruleenforcement.enforce.handle_trigger_instances


def __create_trigger_instance(trigger_name, payload,
                              occurrence_time=datetime.datetime.now()):
    triggers = Trigger.query(name=trigger_name)
    trigger = None if len(triggers) == 0 else triggers[0]
    if trigger is None:
        LOG.info('No trigger with name {} found.', trigger_name)
        return None
    trigger_instance = TriggerInstanceDB()
    trigger_instance.name = 'auto-generated'
    trigger_instance.trigger = trigger
    trigger_instance.payload = payload
    trigger_instance.occurrence_time = occurrence_time
    return TriggerInstance.add_or_update(trigger_instance)


def dispatch_triggers(triggers):
    """
    """
    trigger_instances = [__create_trigger_instance(
        trigger['name'],
        trigger['payload'] if 'payload' in trigger else {},
        trigger['occurrence_time'] if 'occurrence_time' in trigger else
        datetime.datetime.now())
        for trigger in triggers]
    DISPATCH_HANDLER(trigger_instances)


def dispatch_trigger(trigger):
    """
    Trigger must have named properties name, payload, occurrence_time.
    """
    dispatch_triggers([trigger])


def __create_trigger_type(name, description=None, payload_info=None):
    triggers = Trigger.query(name=name)
    trigger_type = TriggerDB()
    if len(triggers) > 0:
        trigger_type = triggers[0]
        LOG.info('Found existing trigger id:%s with name:%s. Will update '
                 'trigger.', trigger_type.id, name)
    trigger_type.name = name
    trigger_type.description = description
    trigger_type.payload_info = payload_info
    return Trigger.add_or_update(trigger_type)


def __validate_trigger_type(trigger_type):
    """
    XXX: We need validator objects that define the required and optional fields.
    For now, manually check them.
    """
    required_fields = ['name']
    for field in required_fields:
        if field not in trigger_type:
            raise Exception('Invaid trigger type. Missing field %s' % field)


def add_trigger_type(trigger_type):
    __validate_trigger_type(trigger_type)
    __create_trigger_type(
        trigger_type['name'],
        trigger_type['description'] if 'description' in trigger_type else '',
        trigger_type['payload_info'] if 'payload_info' in trigger_type else [])
{'fraction_non_alphanumeric': 0.08725839744792643, 'fraction_numerical': 0.003377744417339088, 'mean_word_length': 3.8104693140794224, 'pattern_counts': {'":': 0, '<': 3, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5887626', 'n_tokens_mistral': 1528, 'n_tokens_neox': 1491, 'n_words': 359}