Esempio n. 1
0
    def register(
            self,
            fn: ResourceHandlerFn,
            *,
            id: Optional[str] = None,
            reason: Optional[causation.Reason] = None,
            event: Optional[str] = None,  # deprecated, use `reason`
            field: Optional[dicts.FieldSpec] = None,
            errors: Optional[ErrorsMode] = None,
            timeout: Optional[float] = None,
            retries: Optional[int] = None,
            backoff: Optional[float] = None,
            cooldown: Optional[float] = None,  # deprecated, use `backoff`
            initial: Optional[bool] = None,
            deleted: Optional[bool] = None,
            requires_finalizer: bool = False,
            labels: Optional[bodies.Labels] = None,
            annotations: Optional[bodies.Annotations] = None,
    ) -> ResourceHandlerFn:
        if reason is None and event is not None:
            reason = causation.Reason(event)

        real_field = dicts.parse_field(field) or None  # to not store tuple() as a no-field case.
        real_id = generate_id(fn=fn, id=id, prefix=self.prefix, suffix=".".join(real_field or []))
        handler = ResourceHandler(
            id=real_id, fn=fn, reason=reason, field=real_field,
            errors=errors, timeout=timeout, retries=retries, backoff=backoff, cooldown=cooldown,
            initial=initial, deleted=deleted, requires_finalizer=requires_finalizer,
            labels=labels, annotations=annotations,
        )

        self.append(handler)
        return fn
Esempio n. 2
0
    def register(
            self,
            fn: ResourceHandlerFn,
            id: Optional[str] = None,
            reason: Optional[causation.Reason] = None,
            event: Optional[str] = None,  # deprecated, use `reason`
            field: Optional[dicts.FieldSpec] = None,
            timeout: Optional[float] = None,
            initial: Optional[bool] = None,
            requires_finalizer: bool = False,
            labels: Optional[bodies.Labels] = None,
            annotations: Optional[bodies.Annotations] = None,
    ) -> ResourceHandlerFn:
        if reason is None and event is not None:
            reason = causation.Reason(event)

        real_field = dicts.parse_field(field) or None  # to not store tuple() as a no-field case.
        real_id = generate_id(fn=fn, id=id, prefix=self.prefix, suffix=".".join(real_field or []))
        handler = ResourceHandler(
            id=real_id, fn=fn, reason=reason, field=real_field, timeout=timeout,
            initial=initial,
            labels=labels, annotations=annotations,
        )

        self.append(handler)

        if requires_finalizer:
            self._handlers_requiring_finalizer.append(handler)

        return fn  # to be usable as a decorator too.
Esempio n. 3
0
    def register(
        self,
        fn: callbacks.ResourceHandlerFn,
        *,
        id: Optional[str] = None,
        reason: Optional[causation.Reason] = None,
        event: Optional[str] = None,  # deprecated, use `reason`
        field: Optional[dicts.FieldSpec] = None,
        errors: Optional[errors_.ErrorsMode] = None,
        timeout: Optional[float] = None,
        retries: Optional[int] = None,
        backoff: Optional[float] = None,
        cooldown: Optional[float] = None,  # deprecated, use `backoff`
        initial: Optional[bool] = None,
        deleted: Optional[bool] = None,
        requires_finalizer: bool = False,
        labels: Optional[bodies.Labels] = None,
        annotations: Optional[bodies.Annotations] = None,
        when: Optional[callbacks.WhenHandlerFn] = None,
    ) -> callbacks.ResourceHandlerFn:
        warnings.warn(
            "registry.register() is deprecated; "
            "use @kopf.on... decorators with registry= kwarg.",
            DeprecationWarning)

        if reason is None and event is not None:
            reason = causation.Reason(event)

        real_field = dicts.parse_field(
            field) or None  # to not store tuple() as a no-field case.
        real_id = generate_id(fn=fn, id=id, suffix=".".join(real_field or []))
        handler = handlers.ResourceHandler(
            id=real_id,
            fn=fn,
            reason=reason,
            field=real_field,
            errors=errors,
            timeout=timeout,
            retries=retries,
            backoff=backoff,
            cooldown=cooldown,
            initial=initial,
            deleted=deleted,
            requires_finalizer=requires_finalizer,
            labels=labels,
            annotations=annotations,
            when=when,
        )

        self.append(handler)
        return fn
Esempio n. 4
0
    def register(
        self,
        fn: HandlerFn,
        id: Optional[str] = None,
        reason: Optional[causation.Reason] = None,
        event: Optional[str] = None,  # deprecated, use `reason`
        field: Optional[dicts.FieldSpec] = None,
        timeout: Optional[float] = None,
        initial: Optional[bool] = None,
        requires_finalizer: bool = False,
        labels: Optional[bodies.Labels] = None,
        annotations: Optional[bodies.Annotations] = None,
    ) -> HandlerFn:
        if reason is None and event is not None:
            reason = causation.Reason(event)

        if field is None:
            field = None  # for the non-field events
        elif isinstance(field, str):
            field = tuple(field.split('.'))
        elif isinstance(field, (list, tuple)):
            field = tuple(field)
        else:
            raise ValueError(
                f"Field must be either a str, or a list/tuple. Got {field!r}")

        real_id: HandlerId
        real_id = cast(HandlerId, id) if id is not None else cast(
            HandlerId, get_callable_id(fn))
        real_id = real_id if field is None else cast(
            HandlerId, f'{real_id}/{".".join(field)}')
        real_id = real_id if self.prefix is None else cast(
            HandlerId, f'{self.prefix}/{real_id}')
        handler = Handler(id=real_id,
                          fn=fn,
                          reason=reason,
                          field=field,
                          timeout=timeout,
                          initial=initial,
                          labels=labels,
                          annotations=annotations)

        self.append(handler)

        if requires_finalizer:
            self._handlers_requiring_finalizer.append(handler)

        return fn  # to be usable as a decorator too.