def hook_cli(block_id, *hook_patterns):
    """
    CLI version of the hook decorator.
    """
    if not was_invoked(block_id) and any_hook(*hook_patterns):
        set_invoked(block_id)
        return True
    return False
def hook(*hook_patterns):
    """
    Register the decorated function to run when the current hook matches any of
    the ``hook_patterns``.

    The hook patterns can use the ``{interface:...}`` and ``{A,B,...}`` syntax
    supported by `any_hook`.

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

    For example, to match any joined or changed hook for any relation using the
    ``mysql`` interface::

        class MySQLRelation(RelationBase):
            @hook('{interface:mysql}-relation-{joined,changed}')
            def joined_or_changed(self):
                pass

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

        source `which reactive.sh`

        hook '{interface:mysql}-relation-{joined,changed}'; then
            chlp relation_call $JUJU_RELATION handle_relation
        kooh

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

        source `which reactive.sh`

        if chlp any_hook '{interface:mysql}-relation-{joined,changed}'; then
            chlp relation_call $JUJU_RELATION handle_relation
        kooh
    """
    return Handler.decorator(
        lambda: any_hook(*hook_patterns),
        lambda: filter(None, [RelationBase.from_name(hookenv.relation_type())]))
Esempio n. 3
0
def _hook(hook_patterns):
    dispatch_phase = unitdata.kv().get('reactive.dispatch.phase')
    return dispatch_phase == 'hooks' and any_hook(*hook_patterns)