Skip to content

Commit e9a62d4

Browse files
committed
refactor: plugin package
1 parent d01e57a commit e9a62d4

6 files changed

Lines changed: 11 additions & 18 deletions

File tree

packages/polywrap-plugin/polywrap_plugin/module.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,11 @@ def __init__(self, config: TConfig):
1717
def set_env(self, env: Dict[str, Any]) -> None:
1818
self.env = env
1919

20-
async def _wrap_invoke(self, method: str, args: Dict[str, Any], client: Invoker) -> Result[TResult]:
20+
async def __wrap_invoke__(self, method: str, args: Dict[str, Any], client: Invoker) -> Result[TResult]:
2121
methods: List[str] = [name for name in dir(self) if name == method]
2222

23-
if len(methods) == 0:
23+
if not methods:
2424
return Err(Exception(f"{method} is not defined in plugin"))
2525

2626
callable_method = getattr(self, method)
27-
28-
if not callable(callable_method):
29-
return Err(Exception(f"{method} is an attribute, not a method"))
30-
31-
return Ok(callable_method(args, client))
27+
return Ok(callable_method(args, client)) if callable(callable_method) else Err(Exception(f"{method} is an attribute, not a method"))

packages/polywrap-plugin/polywrap_plugin/wrapper.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,13 @@ def __init__(self, module: PluginModule[TConfig, TResult], manifest: AnyWrapMani
2323
async def invoke(
2424
self, options: InvokeOptions, invoker: Invoker
2525
) -> Result[InvocableResult]:
26-
env = options.env if options.env else {}
26+
env = options.env or {}
2727
self.module.set_env(env)
2828

29-
decoded_args: Union[Dict[str, Any], bytes] = options.args if options.args else {}
29+
args: Union[Dict[str, Any], bytes] = options.args or {}
30+
decoded_args: Dict[str, Any] = msgpack_decode(args) if isinstance(args, bytes) else args
3031

31-
if isinstance(decoded_args, bytes):
32-
decoded_args = msgpack_decode(decoded_args)
33-
34-
result: Result[TResult] = await self.module._wrap_invoke(options.method, decoded_args, invoker) # type: ignore
32+
result: Result[TResult] = await self.module.__wrap_invoke__(options.method, decoded_args, invoker)
3533

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

packages/polywrap-plugin/tests/conftest.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@ def __init__(self, config: None):
1313
def greeting(self, args: Dict[str, Any], client: Invoker):
1414
return f"Greetings from: {args['name']}"
1515

16-
instance = GreetingModule(None)
17-
return instance
16+
return GreetingModule(None)

packages/polywrap-plugin/tests/test_plugin_module.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ async def test_plugin_module(get_greeting_module: PluginModule[None, str]):
1111
module = get_greeting_module
1212

1313
client = PolywrapClient()
14-
result = await module._wrap_invoke("greeting", { "name": "Joe" }, client) # type: ignore
14+
result = await module.__wrap_invoke__("greeting", { "name": "Joe" }, client)
1515
assert result, Ok("Greetings from: Joe")

packages/polywrap-plugin/tests/test_plugin_package.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, cast
1+
from typing import cast
22

33
import pytest
44
from polywrap_core import InvokeOptions, Uri, AnyWrapManifest

packages/polywrap-plugin/tests/test_plugin_wrapper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import cast, Callable, Any
1+
from typing import cast
22

33
import pytest
44
from polywrap_core import InvokeOptions, Uri

0 commit comments

Comments
 (0)