Skip to content

Commit b785c3f

Browse files
authored
Merge pull request #56 from polywrap/nk/plugin
fix: plugin wrapper runtime
2 parents e427af0 + 9a5aefa commit b785c3f

File tree

11 files changed

+264
-170
lines changed

11 files changed

+264
-170
lines changed

packages/polywrap-client/poetry.lock

Lines changed: 145 additions & 138 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/polywrap-client/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ tox-poetry = "^0.4.1"
3333
isort = "^5.10.1"
3434
pyright = "^1.1.275"
3535
pydocstyle = "^6.1.1"
36+
polywrap-plugin = { path = "../polywrap-plugin", develop = true }
3637

3738
[tool.bandit]
3839
exclude_dirs = ["tests"]
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import asyncio
2+
from typing import Any, Dict
3+
4+
from pathlib import Path
5+
from polywrap_core import Invoker, Uri, InvokerOptions, UriWrapper, Wrapper
6+
from polywrap_plugin import PluginModule, PluginWrapper
7+
from polywrap_uri_resolvers import StaticResolver
8+
from polywrap_manifest import AnyWrapManifest
9+
from polywrap_result import Result, Ok, Err
10+
from polywrap_client import PolywrapClient, PolywrapClientConfig
11+
from pytest import fixture
12+
13+
14+
@fixture
15+
def timer_module():
16+
class TimerModule(PluginModule[None, str]):
17+
def __init__(self, config: None):
18+
super().__init__(config)
19+
20+
async def sleep(self, args: Dict[str, Any], client: Invoker):
21+
await asyncio.sleep(args["time"])
22+
print(f"Woke up after {args['time']} seconds")
23+
return Ok(True)
24+
25+
return TimerModule(None)
26+
27+
@fixture
28+
def simple_wrap_manifest():
29+
wrap_path = Path(__file__).parent / "cases" / "simple-invoke" / "wrap.info"
30+
with open(wrap_path, "rb") as f:
31+
yield f.read()
32+
33+
@fixture
34+
def timer_wrapper(timer_module: PluginModule[None, str], simple_wrap_manifest: AnyWrapManifest):
35+
return PluginWrapper(module=timer_module, manifest=simple_wrap_manifest)
36+
37+
38+
async def test_timer(timer_wrapper: Wrapper):
39+
uri_wrapper = UriWrapper(uri=Uri("ens/timer.eth"), wrapper=timer_wrapper)
40+
resolver = StaticResolver.from_list([uri_wrapper]).unwrap()
41+
42+
config = PolywrapClientConfig(resolver=resolver)
43+
44+
client = PolywrapClient(config)
45+
uri = Uri('ens/timer.eth') or Uri(f'fs/{Path(__file__).parent.joinpath("cases", "big-number").absolute()}')
46+
args = { "time": 1 }
47+
options = InvokerOptions(uri=uri, method="sleep", args=args, encode_result=False)
48+
result = await client.invoke(options)
49+
assert result.unwrap() == True

packages/polywrap-plugin/node_modules/.yarn-integrity

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/polywrap-plugin/polywrap_plugin/module.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from abc import ABC
2-
from typing import Any, Dict, TypeVar, Generic, List
2+
from typing import Any, Dict, TypeVar, Generic, List, cast
33

4-
from polywrap_core import Invoker
4+
from polywrap_core import Invoker, execute_maybe_async_function
55
from polywrap_result import Err, Ok, Result
66

77
TConfig = TypeVar('TConfig')
@@ -24,4 +24,12 @@ async def __wrap_invoke__(self, method: str, args: Dict[str, Any], client: Invok
2424
return Err.from_str(f"{method} is not defined in plugin")
2525

2626
callable_method = getattr(self, method)
27-
return Ok(callable_method(args, client)) if callable(callable_method) else Err.from_str(f"{method} is an attribute, not a method")
27+
if callable(callable_method):
28+
try:
29+
result = await execute_maybe_async_function(callable_method, args, client)
30+
if isinstance(result, (Ok, Err)):
31+
return cast(Result[TResult], result)
32+
return Ok(result)
33+
except Exception as e:
34+
return Err(e)
35+
return Err.from_str(f"{method} is an attribute, not a method")

packages/polywrap-plugin/polywrap_plugin/wrapper.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ async def invoke(
3333

3434
if result.is_err():
3535
return cast(Err, result.err)
36-
37-
return Ok(InvocableResult(result=result,encoded=False))
36+
return Ok(InvocableResult(result=result.unwrap(),encoded=False))
3837

3938

4039
async def get_file(self, options: GetFileOptions) -> Result[Union[str, bytes]]:

packages/polywrap-plugin/pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ polywrap_core = { path = "../polywrap-core" }
1515
polywrap_manifest = { path = "../polywrap-manifest" }
1616
polywrap_result = { path = "../polywrap-result" }
1717
polywrap_msgpack = { path = "../polywrap-msgpack" }
18-
polywrap_client = { path = "../polywrap-client" }
1918

2019
[tool.poetry.dev-dependencies]
2120
pytest = "^7.1.2"

packages/polywrap-plugin/tests/conftest.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
from pytest import fixture
2-
from typing import Any, Dict
2+
from typing import Any, Dict, List, Union
33

44
from polywrap_plugin import PluginModule
5-
from polywrap_core import Invoker
5+
from polywrap_core import Invoker, Uri, InvokerOptions
6+
from polywrap_result import Result
67

78
@fixture
8-
def get_greeting_module():
9+
def invoker() -> Invoker:
10+
class MockInvoker(Invoker):
11+
async def invoke(self, options: InvokerOptions) -> Result[Any]:
12+
raise NotImplemented
13+
14+
def get_implementations(self, uri: Uri) -> Result[Union[List[Uri], None]]:
15+
raise NotImplemented
16+
17+
return MockInvoker()
18+
19+
20+
@fixture
21+
def greeting_module():
922
class GreetingModule(PluginModule[None, str]):
1023
def __init__(self, config: None):
1124
super().__init__(config)
Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1-
from typing import Any
2-
31
import pytest
4-
from polywrap_client import PolywrapClient
52
from polywrap_result import Ok
6-
3+
from polywrap_core import Invoker
74
from polywrap_plugin import PluginModule
85

96
@pytest.mark.asyncio
10-
async def test_plugin_module(get_greeting_module: PluginModule[None, str]):
11-
module = get_greeting_module
12-
13-
client = PolywrapClient()
14-
result = await module.__wrap_invoke__("greeting", { "name": "Joe" }, client)
7+
async def test_plugin_module(greeting_module: PluginModule[None, str], invoker: Invoker):
8+
result = await greeting_module.__wrap_invoke__("greeting", { "name": "Joe" }, invoker)
159
assert result, Ok("Greetings from: Joe")
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
from typing import cast
22

33
import pytest
4-
from polywrap_core import InvokeOptions, Uri, AnyWrapManifest
4+
from polywrap_core import InvokeOptions, Uri, AnyWrapManifest, Invoker
55
from polywrap_plugin import PluginPackage, PluginModule
6-
from polywrap_client import PolywrapClient
76
from polywrap_result import Ok
87

98
@pytest.mark.asyncio
10-
async def test_plugin_package_invoke(get_greeting_module: PluginModule[None, str]):
11-
module = get_greeting_module
9+
async def test_plugin_package_invoke(greeting_module: PluginModule[None, str], invoker: Invoker):
1210
manifest = cast(AnyWrapManifest, {})
13-
plugin_package = PluginPackage(module, manifest)
11+
plugin_package = PluginPackage(greeting_module, manifest)
1412
wrapper = (await plugin_package.create_wrapper()).unwrap()
1513
args = {
1614
"name": "Joe"
@@ -21,6 +19,5 @@ async def test_plugin_package_invoke(get_greeting_module: PluginModule[None, str
2119
args=args
2220
)
2321

24-
client = PolywrapClient()
25-
result = await wrapper.invoke(options, client)
22+
result = await wrapper.invoke(options, invoker)
2623
assert result, Ok("Greetings from: Joe")

0 commit comments

Comments
 (0)