Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async maybe #5

Merged
merged 3 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.8', '3.9', '3.10', '3.11']
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']

steps:
- uses: actions/checkout@v3
Expand Down
10 changes: 6 additions & 4 deletions pyflowlauncher/event.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@


import asyncio
from typing import Any, Callable, Iterable, Type


Expand All @@ -24,9 +23,12 @@ def add_methods(self, methods: Iterable[Callable[..., Any]]):
def add_exception_handler(self, exception: Type[Exception], handler: Callable[..., Any]):
self._handlers[exception] = handler

def __call__(self, method: str, *args, **kwargs):
async def __call__(self, method: str, *args, **kwargs):
try:
return self._methods[method](*args, **kwargs)
result = self._methods[method](*args, **kwargs)
if asyncio.iscoroutine(result):
return await result
return result
except Exception as e:
handler = self._handlers.get(type(e), None)
if handler:
Expand Down
7 changes: 6 additions & 1 deletion pyflowlauncher/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Any, Callable, Iterable, Optional, Type, Union
from pathlib import Path
import json
import asyncio

from pyflowlauncher.shared import logger

Expand Down Expand Up @@ -71,7 +72,11 @@ def run(self) -> None:
request = self._client.recieve()
method = request["method"]
parameters = request.get('parameters', [])
feedback = self._event_handler(method, *parameters)
if sys.version_info >= (3, 10, 0):
feedback = asyncio.run(self._event_handler(method, *parameters))
else:
loop = asyncio.get_event_loop()
feedback = loop.run_until_complete(self._event_handler(method, *parameters))
if not feedback:
return
self._client.send(feedback)
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
flake8
pytest
pytest-asyncio
tox
mypy>=1.7.0
bump-my-version>=0.12.0
21 changes: 17 additions & 4 deletions tests/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ def temp_method2():
return None


async def async_temp_method3():
return None


def except_method():
raise Exception

Expand All @@ -26,10 +30,18 @@ def test_add_methods():
assert handler._methods == {"temp_method1": temp_method1, "temp_method2": temp_method2}


def test_call():
@pytest.mark.asyncio
async def test_call():
handler = EventHandler()
handler.add_method(temp_method1)
assert handler("temp_method1") is None
assert await handler("temp_method1") is None


@pytest.mark.asyncio
async def test_call_async():
handler = EventHandler()
handler.add_method(async_temp_method3)
assert await handler("async_temp_method3") is None


def test_add_exception_handler():
Expand All @@ -38,8 +50,9 @@ def test_add_exception_handler():
assert handler._handlers == {Exception: temp_method1}


def test_call_exception():
@pytest.mark.asyncio
async def test_call_exception():
handler = EventHandler()
handler.add_method(except_method)
with pytest.raises(Exception):
handler("except_method")
await handler("except_method")
6 changes: 4 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
[tox]
package_name = "pyflowlauncher"
envlist = lint, type, py{38,39,310,311}
envlist = lint, type, py{38,39,310,311,312}

[testenv]
deps = pytest
deps =
pytest
pytest-asyncio
commands =
pytest {posargs:tests}

Expand Down
Loading