Skip to content
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
283 changes: 145 additions & 138 deletions packages/polywrap-client/poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions packages/polywrap-client/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ tox-poetry = "^0.4.1"
isort = "^5.10.1"
pyright = "^1.1.275"
pydocstyle = "^6.1.1"
polywrap-plugin = { path = "../polywrap-plugin", develop = true }

[tool.bandit]
exclude_dirs = ["tests"]
Expand Down
49 changes: 49 additions & 0 deletions packages/polywrap-client/tests/test_timer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import asyncio
from typing import Any, Dict

from pathlib import Path
from polywrap_core import Invoker, Uri, InvokerOptions, UriWrapper, Wrapper
from polywrap_plugin import PluginModule, PluginWrapper
from polywrap_uri_resolvers import StaticResolver
from polywrap_manifest import AnyWrapManifest
from polywrap_result import Result, Ok, Err
from polywrap_client import PolywrapClient, PolywrapClientConfig
from pytest import fixture


@fixture
def timer_module():
class TimerModule(PluginModule[None, str]):
def __init__(self, config: None):
super().__init__(config)

async def sleep(self, args: Dict[str, Any], client: Invoker):
await asyncio.sleep(args["time"])
print(f"Woke up after {args['time']} seconds")
return Ok(True)

return TimerModule(None)

@fixture
def simple_wrap_manifest():
wrap_path = Path(__file__).parent / "cases" / "simple-invoke" / "wrap.info"
with open(wrap_path, "rb") as f:
yield f.read()

@fixture
def timer_wrapper(timer_module: PluginModule[None, str], simple_wrap_manifest: AnyWrapManifest):
return PluginWrapper(module=timer_module, manifest=simple_wrap_manifest)


async def test_timer(timer_wrapper: Wrapper):
uri_wrapper = UriWrapper(uri=Uri("ens/timer.eth"), wrapper=timer_wrapper)
resolver = StaticResolver.from_list([uri_wrapper]).unwrap()

config = PolywrapClientConfig(resolver=resolver)

client = PolywrapClient(config)
uri = Uri('ens/timer.eth') or Uri(f'fs/{Path(__file__).parent.joinpath("cases", "big-number").absolute()}')
args = { "time": 1 }
options = InvokerOptions(uri=uri, method="sleep", args=args, encode_result=False)
result = await client.invoke(options)
assert result.unwrap() == True
30 changes: 30 additions & 0 deletions packages/polywrap-plugin/node_modules/.yarn-integrity

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions packages/polywrap-plugin/polywrap_plugin/module.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from abc import ABC
from typing import Any, Dict, TypeVar, Generic, List
from typing import Any, Dict, TypeVar, Generic, List, cast

from polywrap_core import Invoker
from polywrap_core import Invoker, execute_maybe_async_function
from polywrap_result import Err, Ok, Result

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

callable_method = getattr(self, method)
return Ok(callable_method(args, client)) if callable(callable_method) else Err.from_str(f"{method} is an attribute, not a method")
if callable(callable_method):
try:
result = await execute_maybe_async_function(callable_method, args, client)
if isinstance(result, (Ok, Err)):
return cast(Result[TResult], result)
return Ok(result)
except Exception as e:
return Err(e)
return Err.from_str(f"{method} is an attribute, not a method")
3 changes: 1 addition & 2 deletions packages/polywrap-plugin/polywrap_plugin/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ async def invoke(

if result.is_err():
return cast(Err, result.err)

return Ok(InvocableResult(result=result,encoded=False))
return Ok(InvocableResult(result=result.unwrap(),encoded=False))


async def get_file(self, options: GetFileOptions) -> Result[Union[str, bytes]]:
Expand Down
1 change: 0 additions & 1 deletion packages/polywrap-plugin/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ polywrap_core = { path = "../polywrap-core" }
polywrap_manifest = { path = "../polywrap-manifest" }
polywrap_result = { path = "../polywrap-result" }
polywrap_msgpack = { path = "../polywrap-msgpack" }
polywrap_client = { path = "../polywrap-client" }

[tool.poetry.dev-dependencies]
pytest = "^7.1.2"
Expand Down
19 changes: 16 additions & 3 deletions packages/polywrap-plugin/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
from pytest import fixture
from typing import Any, Dict
from typing import Any, Dict, List, Union

from polywrap_plugin import PluginModule
from polywrap_core import Invoker
from polywrap_core import Invoker, Uri, InvokerOptions
from polywrap_result import Result

@fixture
def get_greeting_module():
def invoker() -> Invoker:
class MockInvoker(Invoker):
async def invoke(self, options: InvokerOptions) -> Result[Any]:
raise NotImplemented

def get_implementations(self, uri: Uri) -> Result[Union[List[Uri], None]]:
raise NotImplemented

return MockInvoker()


@fixture
def greeting_module():
class GreetingModule(PluginModule[None, str]):
def __init__(self, config: None):
super().__init__(config)
Expand Down
12 changes: 3 additions & 9 deletions packages/polywrap-plugin/tests/test_plugin_module.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
from typing import Any

import pytest
from polywrap_client import PolywrapClient
from polywrap_result import Ok

from polywrap_core import Invoker
from polywrap_plugin import PluginModule

@pytest.mark.asyncio
async def test_plugin_module(get_greeting_module: PluginModule[None, str]):
module = get_greeting_module

client = PolywrapClient()
result = await module.__wrap_invoke__("greeting", { "name": "Joe" }, client)
async def test_plugin_module(greeting_module: PluginModule[None, str], invoker: Invoker):
result = await greeting_module.__wrap_invoke__("greeting", { "name": "Joe" }, invoker)
assert result, Ok("Greetings from: Joe")
11 changes: 4 additions & 7 deletions packages/polywrap-plugin/tests/test_plugin_package.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
from typing import cast

import pytest
from polywrap_core import InvokeOptions, Uri, AnyWrapManifest
from polywrap_core import InvokeOptions, Uri, AnyWrapManifest, Invoker
from polywrap_plugin import PluginPackage, PluginModule
from polywrap_client import PolywrapClient
from polywrap_result import Ok

@pytest.mark.asyncio
async def test_plugin_package_invoke(get_greeting_module: PluginModule[None, str]):
module = get_greeting_module
async def test_plugin_package_invoke(greeting_module: PluginModule[None, str], invoker: Invoker):
manifest = cast(AnyWrapManifest, {})
plugin_package = PluginPackage(module, manifest)
plugin_package = PluginPackage(greeting_module, manifest)
wrapper = (await plugin_package.create_wrapper()).unwrap()
args = {
"name": "Joe"
Expand All @@ -21,6 +19,5 @@ async def test_plugin_package_invoke(get_greeting_module: PluginModule[None, str
args=args
)

client = PolywrapClient()
result = await wrapper.invoke(options, client)
result = await wrapper.invoke(options, invoker)
assert result, Ok("Greetings from: Joe")
11 changes: 4 additions & 7 deletions packages/polywrap-plugin/tests/test_plugin_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
from typing import cast

import pytest
from polywrap_core import InvokeOptions, Uri
from polywrap_core import InvokeOptions, Uri, Invoker
from polywrap_manifest import AnyWrapManifest
from polywrap_client import PolywrapClient
from polywrap_result import Ok

from polywrap_plugin import PluginWrapper, PluginModule

@pytest.mark.asyncio
async def test_plugin_wrapper_invoke(get_greeting_module: PluginModule[None, str]):
module = get_greeting_module
async def test_plugin_wrapper_invoke(greeting_module: PluginModule[None, str], invoker: Invoker):
manifest = cast(AnyWrapManifest, {})

wrapper = PluginWrapper(module, manifest)
wrapper = PluginWrapper(greeting_module, manifest)
args = {
"name": "Joe"
}
Expand All @@ -23,6 +21,5 @@ async def test_plugin_wrapper_invoke(get_greeting_module: PluginModule[None, str
args=args
)

client = PolywrapClient()
result = await wrapper.invoke(options, client)
result = await wrapper.invoke(options, invoker)
assert result, Ok("Greetings from: Joe")