1+ """This module contains the Polywrap client implementation."""
12from __future__ import annotations
23
34import json
1314 GetManifestOptions ,
1415 InvokerOptions ,
1516 IUriResolutionContext ,
16- UriPackage ,
17- UriResolver ,
18- UriWrapper ,
1917 TryResolveUriOptions ,
2018 Uri ,
19+ UriPackage ,
2120 UriPackageOrWrapper ,
21+ UriResolver ,
22+ UriWrapper ,
2223 Wrapper ,
2324)
2425from polywrap_manifest import AnyWrapManifest
2829
2930@dataclass (slots = True , kw_only = True )
3031class PolywrapClientConfig (ClientConfig ):
31- pass
32+ """Defines the config type for the Polywrap client.
33+
34+ Attributes:
35+ envs (Dict[Uri, Env]): Dictionary of environments \
36+ where key is URI and value is env.
37+ interfaces (Dict[Uri, List[Uri]]): Dictionary of interfaces \
38+ and their implementations where key is interface URI \
39+ and value is list of implementation URIs.
40+ resolver (UriResolver): URI resolver.
41+ """
3242
3343
3444class PolywrapClient (Client ):
45+ """Defines the Polywrap client.
46+
47+ Attributes:
48+ _config (PolywrapClientConfig): The client configuration.
49+ """
50+
3551 _config : PolywrapClientConfig
3652
3753 def __init__ (self , config : PolywrapClientConfig ):
54+ """Initialize a new PolywrapClient instance.
55+
56+ Args:
57+ config (PolywrapClientConfig): The polywrap client config.
58+ """
3859 self ._config = config
3960
4061 def get_config (self ):
62+ """Get the client configuration."""
4163 return self ._config
4264
4365 def get_uri_resolver (self ) -> UriResolver :
66+ """Get the URI resolver."""
4467 return self ._config .resolver
4568
4669 def get_envs (self ) -> Dict [Uri , Env ]:
70+ """Get the dictionary of environment variables."""
4771 envs : Dict [Uri , Env ] = self ._config .envs
4872 return envs
4973
5074 def get_interfaces (self ) -> Dict [Uri , List [Uri ]]:
75+ """Get the interfaces."""
5176 interfaces : Dict [Uri , List [Uri ]] = self ._config .interfaces
5277 return interfaces
5378
5479 def get_implementations (self , uri : Uri ) -> Union [List [Uri ], None ]:
80+ """Get the implementations for the given interface URI."""
5581 interfaces : Dict [Uri , List [Uri ]] = self .get_interfaces ()
5682 return interfaces .get (uri )
5783
5884 def get_env_by_uri (self , uri : Uri ) -> Union [Env , None ]:
85+ """Get the environment variables for the given URI."""
5986 return self ._config .envs .get (uri )
6087
6188 async def get_file (self , uri : Uri , options : GetFileOptions ) -> Union [bytes , str ]:
89+ """Get the file from the given wrapper URI."""
6290 loaded_wrapper = await self .load_wrapper (uri )
6391 return await loaded_wrapper .get_file (options )
6492
6593 async def get_manifest (
6694 self , uri : Uri , options : Optional [GetManifestOptions ] = None
6795 ) -> AnyWrapManifest :
96+ """Get the manifest from the given wrapper URI."""
6897 loaded_wrapper = await self .load_wrapper (uri )
6998 return loaded_wrapper .get_manifest ()
7099
71100 async def try_resolve_uri (
72101 self , options : TryResolveUriOptions [UriPackageOrWrapper ]
73102 ) -> UriPackageOrWrapper :
103+ """Try to resolve the given URI."""
74104 uri = options .uri
75105 uri_resolver = self ._config .resolver
76106 resolution_context = options .resolution_context or UriResolutionContext ()
@@ -82,6 +112,7 @@ async def load_wrapper(
82112 uri : Uri ,
83113 resolution_context : Optional [IUriResolutionContext [UriPackageOrWrapper ]] = None ,
84114 ) -> Wrapper [UriPackageOrWrapper ]:
115+ """Load the wrapper for the given URI."""
85116 resolution_context = resolution_context or UriResolutionContext ()
86117
87118 uri_package_or_wrapper = await self .try_resolve_uri (
@@ -101,13 +132,19 @@ async def load_wrapper(
101132 f"""
102133 Error resolving URI "{ uri .uri } "
103134 URI not found
104- Resolution Stack: { json .dumps (build_clean_uri_history (resolution_context .get_history ()), indent = 2 )}
135+ Resolution Stack: {
136+ json .dumps (
137+ build_clean_uri_history (
138+ resolution_context .get_history ()
139+ ), indent = 2
140+ )
141+ }
105142 """
106143 )
107144 )
108145
109-
110146 async def invoke (self , options : InvokerOptions [UriPackageOrWrapper ]) -> Any :
147+ """Invoke the given wrapper URI."""
111148 resolution_context = options .resolution_context or UriResolutionContext ()
112149 wrapper = await self .load_wrapper (
113150 options .uri , resolution_context = resolution_context
0 commit comments