Compare commits

...

4 Commits

Author SHA1 Message Date
Michael Pilosov
8a2a5fecf5 improve tests 2023-03-19 17:56:04 -06:00
Michael Pilosov
bfe94d951f backwards-compatible renaming 2023-03-19 17:50:24 -06:00
Michael Pilosov
f480cb3f12 add documentation 2023-03-19 17:16:47 -06:00
Michael Pilosov
8e51024b2c updated instructions for build, new pkgs 2023-03-19 17:14:06 -06:00
6 changed files with 116 additions and 14 deletions

View File

@ -8,29 +8,40 @@ A Python library that announces a server to a host.
pip install announce-server
```
## Development
To install the developer dependencies required for testing and publishing:
```bash
pip install -e .[dev,pub]
```
## Build
To build the package, run:
```bash
pip install -e .[dev]
pip install -m build
rm -rf dist/ build/ .eggs/ .pytest_cache/ src/announce_server.egg-info/
python -m build --sdist --wheel
```
To publish:
```bash
twine upload dist/*
```
## Test
To run the tests, install the package with the `[dev]` option:
To run the tests, call:
```bash
pip install -e .[dev]
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)
@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

View File

@ -31,13 +31,16 @@ where = src
[options.extras_require]
dev =
build
setuptools_scm
pytest
pytest-mock
pytest-asyncio
pytest-cov
# asynctest; python_version<'3.8'
pub =
build
wheel
setuptools_scm
twine
[coverage:run]
source = announce_server

View File

@ -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

View File

@ -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)

61
tests/test_aliases.py Normal file
View File

@ -0,0 +1,61 @@
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",
[
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)
@pytest.mark.parametrize(
"decorator_alias",
[
announce_server,
],
)
def test_deprecated_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()
with pytest.warns(DeprecationWarning):
decorator = decorator_alias(*test_args, **test_kwargs)
decorator(MagicMock())
mock_register_service.assert_called_once_with(*test_args, **test_kwargs)

View File

@ -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,