Skip to content

Commit 0a7263f

Browse files
committed
feat(plugins): plugin package basic implementation works
1 parent 7338647 commit 0a7263f

7 files changed

Lines changed: 65 additions & 25 deletions

File tree

packages/polywrap-core/polywrap_core/types/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111
from .uri_resolver_handler import *
1212
from .uri_wrapper import *
1313
from .wasm_package import *
14+
from .wrap_package import *
1415
from .wrapper import *

packages/polywrap-plugin/polywrap_plugin/package.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
from typing import Generic
1+
from typing import Generic, Optional, cast
22

3+
from polywrap_core import IWrapPackage, Wrapper, GetManifestOptions
34
from polywrap_manifest import AnyWrapManifest
4-
from polywrap_plugin import PluginModule, TConfig, TResult
5+
from polywrap_result import Ok, Result
56

6-
class PluginPackage(Generic[TConfig, TResult]):
7+
from .module import PluginModule, TConfig, TResult
8+
from .wrapper import PluginWrapper
9+
10+
class PluginPackage(Generic[TConfig, TResult], IWrapPackage):
711
module: PluginModule[TConfig, TResult]
812
manifest: AnyWrapManifest
913

@@ -15,3 +19,8 @@ def __init__(
1519
self.module = module
1620
self.manifest = manifest
1721

22+
async def create_wrapper(self) -> Result[Wrapper]:
23+
return Ok(PluginWrapper(module=self.module, manifest=self.manifest))
24+
25+
async def get_manifest(self, options: Optional[GetManifestOptions] = None) -> Result[AnyWrapManifest]:
26+
return await super().get_manifest(options)

packages/polywrap-plugin/polywrap_plugin/wrapper.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111
from polywrap_msgpack import msgpack_decode
1212
from polywrap_result import Err, Ok, Result
1313

14-
from polywrap_plugin import PluginModule, TConfig, TResult
14+
from .module import PluginModule, TConfig, TResult
1515

1616
class PluginWrapper(Wrapper, Generic[TConfig, TResult]):
1717
module: PluginModule[TConfig, TResult]
18-
manifest: AnyWrapManifest
1918

2019
def __init__(self, module: PluginModule[TConfig, TResult], manifest: AnyWrapManifest) -> None:
2120
self.module = module
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from pytest import fixture
2+
from typing import Any, Dict
3+
4+
from polywrap_plugin import PluginModule
5+
from polywrap_core import Invoker
6+
7+
@fixture
8+
def get_greeting_module():
9+
class GreetingModule(PluginModule[Any, str]):
10+
def __init__(self, config: Any):
11+
super().__init__(config)
12+
13+
def greeting(self, args: Dict[str, Any], client: Invoker):
14+
return f"Greetings from: {args['name']}"
15+
16+
instance = GreetingModule({})
17+
return instance
Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,15 @@
1-
from typing import Any, Dict
1+
from typing import Any
22

33
import pytest
44
from polywrap_client import PolywrapClient
5-
from polywrap_core import Invoker
65
from polywrap_result import Ok
76

87
from polywrap_plugin import PluginModule
98

10-
11-
class GreetingModule(PluginModule[Any, str]):
12-
def __init__(self, config: Any):
13-
super().__init__(config)
14-
15-
def greeting(self, args: Dict[str, Any], client: Invoker):
16-
return f"Greetings from: {args['name']}"
17-
189
@pytest.mark.asyncio
19-
async def test_plugin_module():
20-
plugin = GreetingModule({})
10+
async def test_plugin_module(get_greeting_module: PluginModule[Any, Any]):
11+
module = get_greeting_module
2112

2213
client = PolywrapClient()
23-
result = await plugin._wrap_invoke("greeting", { "name": "Joe" }, client) # type: ignore
14+
result = await module._wrap_invoke("greeting", { "name": "Joe" }, client) # type: ignore
2415
assert result, Ok("Greetings from: Joe")
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from typing import Any, cast
2+
3+
import pytest
4+
from polywrap_core import InvokeOptions, Uri, AnyWrapManifest
5+
from polywrap_plugin import PluginPackage, PluginModule
6+
from polywrap_client import PolywrapClient
7+
from polywrap_result import Ok
8+
9+
@pytest.mark.asyncio
10+
async def test_plugin_package_invoke(get_greeting_module: PluginModule[Any, Any]):
11+
module = get_greeting_module
12+
manifest = cast(AnyWrapManifest, {})
13+
plugin_package = PluginPackage(module, manifest)
14+
wrapper = (await plugin_package.create_wrapper()).unwrap()
15+
args = {
16+
"name": "Joe"
17+
}
18+
options = InvokeOptions(
19+
uri=Uri("ens/greeting.eth"),
20+
method="greeting",
21+
args=args
22+
)
23+
24+
client = PolywrapClient()
25+
result = await wrapper.invoke(options, client)
26+
assert result, Ok("Greetings from: Joe")

packages/polywrap-plugin/tests/test_plugin_wrapper.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
1-
from typing import cast
1+
from typing import cast, Callable, Any
22

33
import pytest
44
from polywrap_core import InvokeOptions, Uri
55
from polywrap_manifest import AnyWrapManifest
6-
from polywrap_plugin import PluginWrapper
76
from polywrap_client import PolywrapClient
87
from polywrap_result import Ok
98

10-
from test_plugin_module import GreetingModule
9+
from polywrap_plugin import PluginWrapper, PluginModule
1110

1211
@pytest.mark.asyncio
13-
async def test_plugin_wrapper_invoke():
14-
module = GreetingModule({})
12+
async def test_plugin_wrapper_invoke(get_greeting_module: PluginModule[Any, Any]):
13+
module = get_greeting_module
1514
manifest = cast(AnyWrapManifest, {})
1615

1716
wrapper = PluginWrapper(module, manifest)
18-
19-
2017
args = {
2118
"name": "Joe"
2219
}

0 commit comments

Comments
 (0)