def only_once(action=None): """ Register the decorated function to be run once, and only once. This decorator will never cause arguments to be passed to the handler. """ if action is None: # allow to be used as @only_once or @only_once() return only_once action_id = _action_id(action) handler = Handler.get(action) handler.add_predicate(lambda: not was_invoked(action_id)) handler.add_post_callback(partial(mark_invoked, action_id)) return action
def _decorator(func): action_id = _action_id(func) short_action_id = _short_action_id(func) @wraps(func) def _wrapped(*args, **kwargs): active_states = get_states() missing_states = [state for state in desired_states if state not in active_states] if missing_states: hookenv.log('%s called before state%s: %s' % ( short_action_id, 's' if len(missing_states) > 1 else '', ', '.join(missing_states)), hookenv.WARNING) return func(*args, **kwargs) _wrapped._action_id = action_id _wrapped._short_action_id = short_action_id return _wrapped
def _decorator(func): action_id = _action_id(func) short_action_id = _short_action_id(func) @wraps(func) def _wrapped(*args, **kwargs): active_flags = get_flags() missing_flags = [flag for flag in desired_flags if flag not in active_flags] if missing_flags: hookenv.log('%s called before flag%s: %s' % ( short_action_id, 's' if len(missing_flags) > 1 else '', ', '.join(missing_flags)), hookenv.WARNING) return func(*args, **kwargs) _wrapped._action_id = action_id _wrapped._short_action_id = short_action_id return _wrapped
def _decorator(func): action_id = _action_id(func) short_action_id = _short_action_id(func) @wraps(func) def _wrapped(*args, **kwargs): active_states = get_states() missing_states = [state for state in desired_states if state not in active_states] if missing_states: hookenv.log( "%s called before state%s: %s" % (short_action_id, "s" if len(missing_states) > 1 else "", ", ".join(missing_states)), hookenv.WARNING, ) return func(*args, **kwargs) _wrapped._action_id = action_id _wrapped._short_action_id = short_action_id return _wrapped
def only_once(action=None): """ .. deprecated:: 0.5.0 Use :func:`when_not` in combination with :func:`set_state` instead. This handler is deprecated because it might actually be `called multiple times <https://github.com/juju-solutions/charms.reactive/issues/22>`_. Register the decorated function to be run once, and only once. This decorator will never cause arguments to be passed to the handler. """ if action is None: # allow to be used as @only_once or @only_once() return only_once action_id = _action_id(action) handler = Handler.get(action) handler.add_predicate(lambda: not was_invoked(action_id)) handler.add_post_callback(partial(mark_invoked, action_id)) return action
def wrapper(*args, **kwargs): action_id = _action_id(action) if not was_invoked(action_id): action(*args, **kwargs) mark_invoked(action_id)