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())]))
def arg_gen(): # use a generator to defer calling of hookenv.relation_type, for tests rel = RelationBase.from_name(hookenv.relation_type()) if rel: yield rel