def when(*desired_states):
    """
    Register the decorated function to run when all ``desired_states`` are active.

    If a state is associated with a relation, an instance of that relation
    class will be passed in to the decorated function.

    This can be used from Bash using the ``reactive.sh`` helpers::

        source `which reactive.sh`

        when db.ready cache.ready; then
            db_dsn=$(state_relation_call db.ready uri)
            cache_uri=$(state_relation_call cache.ready uri)
            chlp render_template db_dsn=$db_dsn cache_uri=$cache_uri
        nehw

    The Bash helper uses the :func:`when_cli` ``chlp`` command, and the above is
    exactly equivalent to::

        source `which reactive.sh`

        if chlp when_cli db.ready cache.ready; then
            db_dsn=$(state_relation_call db.ready uri)
            cache_uri=$(state_relation_call cache.ready uri)
            chlp render_template db_dsn=$db_dsn cache_uri=$cache_uri
        fi
    """
    return Handler.decorator(
        lambda: all_states(*desired_states),
        lambda: filter(None, map(RelationBase.from_state, desired_states)))
def when_not(*desired_states):
    """
    Register the decorated function to run when **not** all desired_states are active.

    If a state is associated with a relation, an instance of that relation
    class will be passed in to the decorated function.

    This can be used from Bash using the ``reactive.sh`` helpers::

        source `which reactive.sh`

        when_not db.ready cache.ready; then
            db_dsn=$(state_relation_call db.ready uri)
            cache_uri=$(state_relation_call cache.ready uri)
            chlp render_template db_dsn=$db_dsn cache_uri=$cache_uri
        nehw

    The Bash helper uses the `all_states` ``chlp`` command, and the above is
    exactly equivalent to::

        source `which reactive.sh`

        if ! chlp all_states db.ready cache.ready; then
            db_dsn=$(state_relation_call db.ready uri)
            cache_uri=$(state_relation_call cache.ready uri)
            chlp render_template db_dsn=$db_dsn cache_uri=$cache_uri
        fi
    """
    return Handler.decorator(lambda: not all_states(*desired_states))
def when_not_cli(block_id, *desired_states):
    """
    CLI version of the when_not decorator.
    """
    if not was_invoked(block_id) and not all_states(*desired_states):
        set_invoked(block_id)
        return True
    return False
def when_cli(block_id, *desired_states):
    """
    CLI version of the when decorator.

    Evaluates to true (exit 0) if all of the ``desired_states`` are active,
    as per :func:`~charmhelpers.core.reactive.bus.all_states`, and if it
    has not evaluated to true for the given ``block_id`` already for this
    round of dispatch.

    :param str block_id: Any unique identifier for the block, such as the
        filename and line number of the start of the block.
    :param list desired_states: List of states that should be active.
    """
    if not was_invoked(block_id) and all_states(*desired_states):
        set_invoked(block_id)
        return True
    return False
Пример #5
0
def _when(handler_id, states, invert):
    dispatch_phase = unitdata.kv().get('reactive.dispatch.phase')
    return dispatch_phase == 'other' and StateWatch.watch(handler_id, states) and (all_states(*states) ^ invert)