예제 #1
0
파일: process.py 프로젝트: crudbug/canopsis
def event_processing(event, ctx=None, **params):
    """Process input event in getting graph nodes bound to input event entity.

    If at least one graph nodes is found, execute its tasks.
    """

    nodes = []

    if ctx is None:
        ctx = {}

    entity = context.get_entity(event)

    if entity is not None:
        entity_id = context.get_entity_id(entity)
        nodes = graph.get_nodes(entity_id=entity_id)

        for node in nodes:
            task = node[Graph.TASK]
            if task is not None:
                run_task(
                    event=event,
                    task=task,
                    ctx=ctx,
                    node=node
                )

    return event
예제 #2
0
def condition(condition=None, statement=None, _else=None, **kwargs):
    """
    Run an statement if input condition is checked and return statement result.

    :param condition: condition to check.
    :type condition: str or dict
    :param statement: statement to process if condition is checked.
    :type statement: str or dict
    :param _else: else statement.
    :type _else: str or dict
    :param kwargs: condition and statement additional parameters.

    :return: statement result.
    """

    result = None

    checked = False

    if condition is not None:
        checked = run_task(condition, **kwargs)

    if checked:  # if condition is checked
        if statement is not None:  # process statement
            result = run_task(statement, **kwargs)
    elif _else is not None:  # else process _else statement
        result = run_task(_else, **kwargs)

    return result
예제 #3
0
파일: core.py 프로젝트: crudbug/canopsis
    def test_exception_without_raise(self):
        """
        Test task which raises an exception.
        """

        result = run_task('test_exception', raiseerror=False)
        self.assertTrue(isinstance(result, Exception))
예제 #4
0
파일: core.py 프로젝트: crudbug/canopsis
    def test_simple(self):
        """
        Test simple task.
        """

        result = run_task('test')
        self.assertIs(result, self)
예제 #5
0
def _any(confs=None, **kwargs):
    """
    True iif at least one input condition is equivalent to True.

    :param confs: confs to check.
    :type confs: list or dict or str
    :param kwargs: additional task kwargs.

    :return: True if at least one condition is checked (compared to True, but
            not an strict equivalence to True). False otherwise.
    :rtype: bool
    """

    result = False

    if confs is not None:
        # ensure confs is a list
        if isinstance(confs, (basestring, dict)):
            confs = [confs]
        for conf in confs:
            result = run_task(conf, **kwargs)
            if result:  # leave function as soon as a result if True
                break

    return result
예제 #6
0
def _all(confs=None, **kwargs):
    """
    True iif all input confs are True.

    :param confs: confs to check.
    :type confs: list or dict or str
    :param kwargs: additional task kwargs.

    :return: True if all conditions are checked. False otherwise.
    :rtype: bool
    """

    result = False

    if confs is not None:
        # ensure confs is a list
        if isinstance(confs, (basestring, dict)):
            confs = [confs]
        # if at least one conf exists, result is True by default
        result = True
        for conf in confs:
            result = run_task(conf, **kwargs)
            # stop when a result is False
            if not result:
                break

    return result
예제 #7
0
파일: core.py 프로젝트: crudbug/canopsis
    def test_simple_params(self):
        """
        Test task with params
        """

        conf = new_conf('test_params', **{'a': 1, 'b': 2})
        result = run_task(conf, ctx={'a': 1})
        self.assertEqual(result, 5)
예제 #8
0
파일: event.py 프로젝트: crudbug/canopsis
    def process(self, event, **kwargs):
        """Process this vertice task in a context of event processing.
        """

        result = None

        task = self.task

        result = run_task(conf=task, vertice=self, event=event, **kwargs)

        return result
예제 #9
0
def _not(condition=None, **kwargs):
    """
    Return the opposite of input condition.

    :param condition: condition to process.

    :result: not condition.
    :rtype: bool
    """

    result = True

    if condition is not None:
        result = not run_task(condition, **kwargs)

    return result
예제 #10
0
def switch(
        confs=None, remain=False, all_checked=False, _default=None, **kwargs
):
    """
    Execute first statement among conf where task result is True.
    If remain, process all statements conf starting from the first checked
    conf.

    :param confs: task confs to check. Each one may contain a task action at
        the key 'action' in conf.
    :type confs: str or dict or list
    :param bool remain: if True, execute all remaining actions after the
        first checked condition.
    :param bool all_checked: execute all statements where conditions are
        checked.
    :param _default: default task to process if others have not been checked.
    :type _default: str or dict

    :return: statement result or list of statement results if remain.
    :rtype: list or object
    """

    # init result
    result = [] if remain else None

        # check if remain and one task has already been checked.
    remaining = False

    if confs is not None:
        if isinstance(confs, (basestring, dict)):
            confs = [confs]
        for conf in confs:
            # check if task has to be checked or not
            check = remaining
            if not check:
                # try to check current conf
                check = run_task(conf=conf, **kwargs)
            # if task is checked or remaining
            if check:
                if STATEMENT in conf:  # if statements exist, run them
                    statement = conf[STATEMENT]
                    statement_result = run_task(statement, **kwargs)
                    # save result
                    if not remain:  # if not remain, result is statement_result
                        result = statement_result
                    else:  # else, add statement_result to result
                        result.append(statement_result)
                # if remain
                if remain:
                    # change of remaining status
                    if not remaining:
                        remaining = True
                elif all_checked:
                    pass
                else:  # leave execution if one statement has been executed
                    break

    # process _default statement if necessary
    if _default is not None and (remaining or (not result) or all_checked):
        last_result = run_task(_default, **kwargs)
        if not remain:
            result = last_result
        else:
            result.append(last_result)

    return result