From 8c414fd87db8148b032c6d17641d725c602581bb Mon Sep 17 00:00:00 2001 From: Michael Pilosov Date: Sun, 19 Mar 2023 17:50:24 -0600 Subject: [PATCH] backwards-compatible renaming --- README.md | 4 ++-- src/announce_server/__init__.py | 8 ++++++- src/announce_server/decorator.py | 25 +++++++++++++++++++-- tests/test_aliases.py | 37 ++++++++++++++++++++++++++++++++ tests/test_decorator.py | 4 ++-- 5 files changed, 71 insertions(+), 7 deletions(-) create mode 100644 tests/test_aliases.py diff --git a/README.md b/README.md index 6383f37..69545d2 100644 --- a/README.md +++ b/README.md @@ -40,9 +40,9 @@ pytest ## Usage ```python -from announce_server.announce import announce_server +from announce_server import register_service -@announce_server(name="server_name", ip="server_ip", port=8000, host_ip="host_server_ip", host_port=5000, retry_interval=5) +@register_service(name="server_name", ip="server_ip", port=8000, host_ip="host_server_ip", host_port=5000, retry_interval=5) def your_function(): pass diff --git a/src/announce_server/__init__.py b/src/announce_server/__init__.py index 7d8a71d..058f0ea 100644 --- a/src/announce_server/__init__.py +++ b/src/announce_server/__init__.py @@ -1,2 +1,8 @@ -from .decorator import _announce_server, announce_server +from .decorator import ( + _announce_server, + announce_server, + register_block, + register_server, + register_service, +) from .get_ip import get_ip_address diff --git a/src/announce_server/decorator.py b/src/announce_server/decorator.py index 67965f9..40a30c1 100644 --- a/src/announce_server/decorator.py +++ b/src/announce_server/decorator.py @@ -1,4 +1,5 @@ import asyncio +import warnings from functools import wraps import socketio @@ -45,9 +46,9 @@ async def _announce_server(**kwargs): await main() -def announce_server(task=None, **outer_kwargs): +def register_service(task=None, **outer_kwargs): if task is None: - return lambda f: announce_server(f, **outer_kwargs) + return lambda f: register_service(f, **outer_kwargs) @wraps(task) def wrapper(*args, **kwargs): @@ -64,3 +65,23 @@ def announce_server(task=None, **outer_kwargs): return asyncio.run(main()) return wrapper + + +def announce_server(*args, **kwargs): + """Wrapper for register_service""" + warnings.warn( + "announce_server is deprecated, use register_service instead", + DeprecationWarning, + stacklevel=2, + ) + return register_service(*args, **kwargs) + + +def register_block(*args, **kwargs): + """Wrapper for register_service""" + return register_service(*args, **kwargs) + + +def register_server(*args, **kwargs): + """Wrapper for register_service""" + return register_service(*args, **kwargs) diff --git a/tests/test_aliases.py b/tests/test_aliases.py new file mode 100644 index 0000000..8e529a2 --- /dev/null +++ b/tests/test_aliases.py @@ -0,0 +1,37 @@ +from unittest.mock import MagicMock, patch + +import pytest + +from announce_server.decorator import ( + announce_server, + register_block, + register_server, + register_service, +) + + +@pytest.mark.parametrize( + "decorator_alias", + [ + announce_server, + register_block, + register_server, + ], +) +def test_alias_calls_register_service(decorator_alias): + test_args = (None, None) + test_kwargs = { + "name": "test_server", + "ip": "127.0.0.1", + "port": 8000, + "host_ip": "127.0.0.1", + "host_port": 5000, + } + + with patch("announce_server.decorator.register_service") as mock_register_service: + mock_register_service.return_value = MagicMock() + + decorator = decorator_alias(*test_args, **test_kwargs) + decorator(MagicMock()) + + mock_register_service.assert_called_once_with(*test_args, **test_kwargs) diff --git a/tests/test_decorator.py b/tests/test_decorator.py index b76f454..fa117db 100644 --- a/tests/test_decorator.py +++ b/tests/test_decorator.py @@ -4,7 +4,7 @@ from unittest.mock import MagicMock, patch import pytest -from announce_server.decorator import _announce_server, announce_server +from announce_server.decorator import _announce_server, register_service @patch("announce_server.decorator._announce_server") @@ -13,7 +13,7 @@ def test_announce_server_decorator(mock_announce_server): mock_announce_server.return_value = MagicMock() # Decorate the sample function with announce_server - @announce_server( + @register_service( name="test_server", ip="127.0.0.1", port=8000,