Exemplo n.º 1
0
def _matches_field_changes(
    handler: handlers.ResourceHandler,
    cause: causation.ResourceCause,
    kwargs: MutableMapping[str, Any],
) -> bool:
    if not isinstance(handler, handlers.ResourceChangingHandler):
        return True
    if not isinstance(cause, causation.ResourceChangingCause):
        return True
    if not handler.field:
        return True

    if not kwargs:
        kwargs.update(invocation.build_kwargs(cause=cause))

    absent = _UNSET.token  # or any other identifyable object
    old = dicts.resolve(cause.old, handler.field, absent)
    new = dicts.resolve(cause.new, handler.field, absent)
    return ((
        not handler.field_needs_change
        or old != new  # ... or there IS a change.
    ) and ((handler.old is None) or
           (handler.old is filters.ABSENT and old is absent) or
           (handler.old is filters.PRESENT and old is not absent) or
           (callable(handler.old) and handler.old(old, **kwargs)) or
           (handler.old == old))
            and ((handler.new is None) or
                 (handler.new is filters.ABSENT and new is absent) or
                 (handler.new is filters.PRESENT and new is not absent) or
                 (callable(handler.new) and handler.new(new, **kwargs)) or
                 (handler.new == new)))
Exemplo n.º 2
0
def _matches_field_values(
    handler: handlers.ResourceHandler,
    cause: causation.ResourceCause,
    kwargs: MutableMapping[str, Any],
) -> bool:
    if not handler.field:
        return True

    if not kwargs:
        kwargs.update(invocation.build_kwargs(cause=cause))

    absent = _UNSET.token  # or any other identifyable object
    if isinstance(cause, causation.ResourceChangingCause):
        # For on.update/on.field, so as for on.create/resume/delete for uniformity and simplicity:
        old = dicts.resolve(cause.old, handler.field, absent)
        new = dicts.resolve(cause.new, handler.field, absent)
        values = [
            new, old
        ]  # keep "new" first, to avoid "old" callbacks if "new" works.
    else:
        # For event-watching, timers/daemons (could also work for on.create/resume/delete):
        val = dicts.resolve(cause.body, handler.field, absent)
        values = [val]
    return ((handler.value is None and any(value is not absent
                                           for value in values))
            or (handler.value is filters.PRESENT and any(value is not absent
                                                         for value in values))
            or (handler.value is filters.ABSENT and any(value is absent
                                                        for value in values))
            or (callable(handler.value)
                and any(handler.value(value, **kwargs) for value in values))
            or (any(handler.value == value for value in values)))
Exemplo n.º 3
0
def _matches_filter_callback(
    handler: handlers.ResourceHandler,
    cause: causation.ResourceCause,
    kwargs: MutableMapping[str, Any],
) -> bool:
    if handler.when is None:
        return True
    if not kwargs:
        kwargs.update(invocation.build_kwargs(cause=cause))
    return handler.when(**kwargs)