Skip to content
This repository has been archived by the owner on Sep 14, 2020. It is now read-only.

Commit

Permalink
Deprecate the proxying methods of registries
Browse files Browse the repository at this point in the history
  • Loading branch information
nolar committed Feb 20, 2020
1 parent d13add7 commit 3a41cfe
Show file tree
Hide file tree
Showing 11 changed files with 272 additions and 101 deletions.
57 changes: 57 additions & 0 deletions kopf/reactor/registries.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import abc
import collections
import functools
import warnings
from types import FunctionType, MethodType
from typing import (Any, MutableMapping, Optional, Sequence, Collection, Iterable, Iterator,
List, Set, FrozenSet, Mapping, Callable, cast, Generic, TypeVar)
Expand Down Expand Up @@ -65,6 +66,9 @@ def register(
activity: Optional[causation.Activity] = None,
_fallback: bool = False,
) -> callbacks.ActivityHandlerFn:
warnings.warn("registry.register() is deprecated; "
"use @kopf.on... decorators with registry= kwarg.",
DeprecationWarning)
real_id = generate_id(fn=fn, id=id)
handler = handlers.ActivityHandler(
id=real_id, fn=fn, activity=activity,
Expand Down Expand Up @@ -122,6 +126,10 @@ def register(
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)

Expand Down Expand Up @@ -228,6 +236,10 @@ def resources(self) -> FrozenSet[resources_.Resource]:
return (frozenset(self.resource_watching_handlers) |
frozenset(self.resource_changing_handlers))

#
# Everything below is deprecated and will be removed in the next major release.
#

def register_activity_handler(
self,
fn: callbacks.ActivityHandlerFn,
Expand All @@ -241,6 +253,9 @@ def register_activity_handler(
activity: Optional[causation.Activity] = None,
_fallback: bool = False,
) -> callbacks.ActivityHandlerFn:
warnings.warn("registry.register_activity_handler() is deprecated; "
"use @kopf.on... decorators with registry= kwarg.",
DeprecationWarning)
return self.activity_handlers.register(
fn=fn, id=id, activity=activity,
errors=errors, timeout=timeout, retries=retries, backoff=backoff, cooldown=cooldown,
Expand All @@ -261,6 +276,9 @@ def register_resource_watching_handler(
"""
Register an additional handler function for low-level events.
"""
warnings.warn("registry.register_resource_watching_handler() is deprecated; "
"use @kopf.on... decorators with registry= kwarg.",
DeprecationWarning)
resource = resources_.Resource(group, version, plural)
return self.resource_watching_handlers[resource].register(
fn=fn, id=id,
Expand Down Expand Up @@ -292,6 +310,9 @@ def register_resource_changing_handler(
"""
Register an additional handler function for the specific resource and specific reason.
"""
warnings.warn("registry.register_resource_changing_handler() is deprecated; "
"use @kopf.on... decorators with registry= kwarg.",
DeprecationWarning)
resource = resources_.Resource(group, version, plural)
return self.resource_changing_handlers[resource].register(
reason=reason, event=event, field=field, fn=fn, id=id,
Expand All @@ -303,44 +324,65 @@ def register_resource_changing_handler(
def has_activity_handlers(
self,
) -> bool:
warnings.warn("registry.has_activity_handlers() is deprecated; "
"use registry.activity_handlers directly.",
DeprecationWarning)
return bool(self.activity_handlers)

def has_resource_watching_handlers(
self,
resource: resources_.Resource,
) -> bool:
warnings.warn("registry.has_resource_watching_handlers() is deprecated; "
"use registry.resource_watching_handlers[resource] directly.",
DeprecationWarning)
return bool(self.resource_watching_handlers[resource])

def has_resource_changing_handlers(
self,
resource: resources_.Resource,
) -> bool:
warnings.warn("registry.has_resource_changing_handlers() is deprecated; "
"use registry.resource_changing_handlers[resource] directly.",
DeprecationWarning)
return bool(self.resource_changing_handlers[resource])

def get_activity_handlers(
self,
*,
activity: causation.Activity,
) -> Sequence[handlers.ActivityHandler]:
warnings.warn("registry.get_activity_handlers() is deprecated; "
"use registry.activity_handlers.get_handlers().",
DeprecationWarning)
return self.activity_handlers.get_handlers(activity=activity)

def get_resource_watching_handlers(
self,
cause: causation.ResourceWatchingCause,
) -> Sequence[handlers.ResourceHandler]:
warnings.warn("registry.get_resource_watching_handlers() is deprecated; "
"use registry.resource_watching_handlers[resource].get_handlers().",
DeprecationWarning)
return self.resource_watching_handlers[cause.resource].get_handlers(cause=cause)

def get_resource_changing_handlers(
self,
cause: causation.ResourceChangingCause,
) -> Sequence[handlers.ResourceHandler]:
warnings.warn("registry.get_resource_changing_handlers() is deprecated; "
"use registry.resource_changing_handlers[resource].get_handlers().",
DeprecationWarning)
return self.resource_changing_handlers[cause.resource].get_handlers(cause=cause)

def iter_activity_handlers(
self,
*,
activity: causation.Activity,
) -> Iterator[handlers.ActivityHandler]:
warnings.warn("registry.iter_activity_handlers() is deprecated; "
"use registry.activity_handlers.iter_handlers().",
DeprecationWarning)
yield from self.activity_handlers.iter_handlers(activity=activity)

def iter_resource_watching_handlers(
Expand All @@ -350,6 +392,9 @@ def iter_resource_watching_handlers(
"""
Iterate all handlers for the low-level events.
"""
warnings.warn("registry.iter_resource_watching_handlers() is deprecated; "
"use registry.resource_watching_handlers[resource].iter_handlers().",
DeprecationWarning)
yield from self.resource_watching_handlers[cause.resource].iter_handlers(cause=cause)

def iter_resource_changing_handlers(
Expand All @@ -359,18 +404,27 @@ def iter_resource_changing_handlers(
"""
Iterate all handlers that match this cause/event, in the order they were registered (even if mixed).
"""
warnings.warn("registry.iter_resource_changing_handlers() is deprecated; "
"use registry.resource_changing_handlers[resource].iter_handlers().",
DeprecationWarning)
yield from self.resource_changing_handlers[cause.resource].iter_handlers(cause=cause)

def get_extra_fields(
self,
resource: resources_.Resource,
) -> Set[dicts.FieldPath]:
warnings.warn("registry.get_extra_fields() is deprecated; "
"use registry.resource_changing_handlers[resource].get_extra_fields().",
DeprecationWarning)
return self.resource_changing_handlers[resource].get_extra_fields()

def iter_extra_fields(
self,
resource: resources_.Resource,
) -> Iterator[dicts.FieldPath]:
warnings.warn("registry.iter_extra_fields() is deprecated; "
"use registry.resource_changing_handlers[resource].iter_extra_fields().",
DeprecationWarning)
yield from self.resource_changing_handlers[resource].iter_extra_fields()

def requires_finalizer(
Expand All @@ -381,6 +435,9 @@ def requires_finalizer(
"""
Check whether a finalizer should be added to the given resource or not.
"""
warnings.warn("registry.requires_finalizer() is deprecated; "
"use registry.resource_changing_handlers[resource].requires_finalizer().",
DeprecationWarning)
return self.resource_changing_handlers[resource].requires_finalizer(cause=cause)


Expand Down
3 changes: 2 additions & 1 deletion tests/registries/legacy-1/test_legacy1_handler_matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ def registry(request):
@pytest.fixture()
def register_fn(registry, resource):
if isinstance(registry, SimpleRegistry):
yield registry.register
with pytest.deprecated_call(match=r"registry.register\(\) is deprecated"):
yield registry.register
elif isinstance(registry, GlobalRegistry):
with pytest.deprecated_call(match=r"GlobalRegistry.register_cause_handler\(\) is deprecated"):
yield functools.partial(registry.register_cause_handler, resource.group, resource.version, resource.plural)
Expand Down
15 changes: 10 additions & 5 deletions tests/registries/legacy-1/test_legacy1_id_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ def test_with_no_hints(mocker):
get_fn_id = mocker.patch('kopf.reactor.registries.get_callable_id', return_value='some-id')

registry = SimpleRegistry()
registry.register(some_fn)
with pytest.deprecated_call(match=r"registry.register\(\) is deprecated"):
registry.register(some_fn)
with pytest.deprecated_call(match=r"get_cause_handlers\(\) is deprecated"):
handlers = registry.get_cause_handlers(mocker.MagicMock())

Expand All @@ -91,7 +92,8 @@ def test_with_prefix(mocker):
get_fn_id = mocker.patch('kopf.reactor.registries.get_callable_id', return_value='some-id')

registry = SimpleRegistry()
registry.register(some_fn)
with pytest.deprecated_call(match=r"registry.register\(\) is deprecated"):
registry.register(some_fn)
with pytest.deprecated_call(match=r"get_cause_handlers\(\) is deprecated"):
handlers = registry.get_cause_handlers(mocker.MagicMock())

Expand All @@ -107,7 +109,8 @@ def test_with_suffix(mocker, field):
diff = [('add', ('some-field', 'sub-field'), 'old', 'new')]

registry = SimpleRegistry()
registry.register(some_fn, field=field)
with pytest.deprecated_call(match=r"registry.register\(\) is deprecated"):
registry.register(some_fn, field=field)
with pytest.deprecated_call(match=r"get_cause_handlers\(\) is deprecated"):
handlers = registry.get_cause_handlers(mocker.MagicMock(diff=diff))

Expand All @@ -124,7 +127,8 @@ def test_with_prefix_and_suffix(mocker, field):
diff = [('add', ('some-field', 'sub-field'), 'old', 'new')]

registry = SimpleRegistry(prefix='some-prefix')
registry.register(some_fn, field=field)
with pytest.deprecated_call(match=r"registry.register\(\) is deprecated"):
registry.register(some_fn, field=field)
with pytest.deprecated_call(match=r"get_cause_handlers\(\) is deprecated"):
handlers = registry.get_cause_handlers(mocker.MagicMock(diff=diff))

Expand All @@ -141,7 +145,8 @@ def test_with_explicit_id_and_prefix_and_suffix(mocker, field):
diff = [('add', ('some-field', 'sub-field'), 'old', 'new')]

registry = SimpleRegistry(prefix='some-prefix')
registry.register(some_fn, id='explicit-id', field=field)
with pytest.deprecated_call(match=r"registry.register\(\) is deprecated"):
registry.register(some_fn, id='explicit-id', field=field)
with pytest.deprecated_call(match=r"get_cause_handlers\(\) is deprecated"):
handlers = registry.get_cause_handlers(mocker.MagicMock(diff=diff))

Expand Down
3 changes: 2 additions & 1 deletion tests/registries/legacy-1/test_legacy1_registering.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ def test_simple_registry_with_minimal_signature(mocker):
cause = mocker.Mock(event=None, diff=None)

registry = SimpleRegistry()
registry.register(some_fn)
with pytest.deprecated_call(match=r"registry.register\(\) is deprecated"):
registry.register(some_fn)
with pytest.deprecated_call(match=r"use ResourceChangingRegistry.get_handlers\(\)"):
handlers = registry.get_cause_handlers(cause)

Expand Down
21 changes: 14 additions & 7 deletions tests/registries/legacy-1/test_legacy1_requires_finalizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def test_requires_finalizer_deletion_handler(optional, expected):
def fn(**_):
pass

requires_finalizer = registry.requires_finalizer(resource=resource, cause=CAUSE)
with pytest.deprecated_call(match=r"use registry.resource_changing_handlers"):
requires_finalizer = registry.requires_finalizer(resource=resource, cause=CAUSE)
assert requires_finalizer == expected


Expand All @@ -62,7 +63,8 @@ def fn1(**_):
def fn2(**_):
pass

requires_finalizer = registry.requires_finalizer(resource=resource, cause=CAUSE)
with pytest.deprecated_call(match=r"use registry.resource_changing_handlers"):
requires_finalizer = registry.requires_finalizer(resource=resource, cause=CAUSE)
assert requires_finalizer == expected


Expand All @@ -75,7 +77,8 @@ def test_requires_finalizer_no_deletion_handler():
def fn1(**_):
pass

requires_finalizer = registry.requires_finalizer(resource=resource, cause=CAUSE)
with pytest.deprecated_call(match=r"use registry.resource_changing_handlers"):
requires_finalizer = registry.requires_finalizer(resource=resource, cause=CAUSE)
assert requires_finalizer is False


Expand All @@ -97,7 +100,8 @@ def test_requires_finalizer_deletion_handler_matches_labels(labels, optional, ex
def fn(**_):
pass

requires_finalizer = registry.requires_finalizer(resource=resource, cause=CAUSE)
with pytest.deprecated_call(match=r"use registry.resource_changing_handlers"):
requires_finalizer = registry.requires_finalizer(resource=resource, cause=CAUSE)
assert requires_finalizer == expected


Expand All @@ -119,7 +123,8 @@ def test_requires_finalizer_deletion_handler_mismatches_labels(labels, optional,
def fn(**_):
pass

requires_finalizer = registry.requires_finalizer(resource=resource, cause=CAUSE)
with pytest.deprecated_call(match=r"use registry.resource_changing_handlers"):
requires_finalizer = registry.requires_finalizer(resource=resource, cause=CAUSE)
assert requires_finalizer == expected


Expand All @@ -141,7 +146,8 @@ def test_requires_finalizer_deletion_handler_matches_annotations(annotations, op
def fn(**_):
pass

requires_finalizer = registry.requires_finalizer(resource=resource, cause=CAUSE)
with pytest.deprecated_call(match=r"use registry.resource_changing_handlers"):
requires_finalizer = registry.requires_finalizer(resource=resource, cause=CAUSE)
assert requires_finalizer == expected


Expand All @@ -163,5 +169,6 @@ def test_requires_finalizer_deletion_handler_mismatches_annotations(annotations,
def fn(**_):
pass

requires_finalizer = registry.requires_finalizer(resource=resource, cause=CAUSE)
with pytest.deprecated_call(match=r"use registry.resource_changing_handlers"):
requires_finalizer = registry.requires_finalizer(resource=resource, cause=CAUSE)
assert requires_finalizer == expected
Loading

0 comments on commit 3a41cfe

Please sign in to comment.