1+ """
2+ Polywrap Python Client.
3+
4+ The ClientConfigBuilder Package provides a simple interface for building a ClientConfig object,
5+ which is used to configure the Polywrap Client and its sub-components. You can use the
6+ ClientConfigBuilder to set the wrappers, packages, and other configuration options for the
7+ Polywrap Client.
8+
9+ docs.polywrap.io
10+ Copyright 2022 Polywrap
11+ """
12+
113from abc import ABC , abstractmethod
214from dataclasses import dataclass
3- from polywrap_core import Uri , IUriResolver , Env , UriWrapper ,UriPackage
4- from polywrap_uri_resolvers import IUriResolver
5- from typing import Dict , Any , List , Optional , Union
6- from polywrap_uri_resolvers import UriResolverLike
15+ from typing import Any , Dict , List , Union
16+
17+ from polywrap_core import Env , Uri , UriPackage , UriWrapper
718
8- # from polywrap_plugin import PluginPackage
19+ UriResolverLike = Union [ Uri , UriPackage , UriWrapper , List [ "UriResolverLike" ]]
920
1021
1122@dataclass (slots = True , kw_only = True )
1223class ClientConfig :
1324 """
14- This Abstract class is used to configure the polywrap client before it executes a call
15- The ClientConfig class is created and modified with the ClientConfigBuilder module
25+ Abstract class used to configure the polywrap client before it executes a call.
26+
27+ The ClientConfig class is created and modified with the ClientConfigBuilder module.
1628 """
1729
1830 envs : Dict [Uri , Dict [str , Any ]]
@@ -25,23 +37,28 @@ class ClientConfig:
2537
2638class BaseClientConfigBuilder (ABC ):
2739 """
28- An abstract base class of the `ClientConfigBuilder`, which uses the ABC module
29- to define the methods that can be used to configure the `ClientConfig` object.
40+ An abstract base class of the `ClientConfigBuilder`.
41+
42+ It uses the ABC module to define the methods that can be used to
43+ configure the `ClientConfig` object.
3044 """
3145
3246 def __init__ (self ):
47+ """Initialize the builder's config attributes with empty values."""
3348 self .config = ClientConfig (
3449 envs = {}, interfaces = {}, resolver = [], wrappers = [], packages = [], redirects = {}
3550 )
3651
3752 @abstractmethod
3853 def build (self ) -> ClientConfig :
39- """Returns a sanitized config object from the builder's config."""
40- pass
54+ """Return a sanitized config object from the builder's config."""
4155
42-
4356 def add (self , new_config : ClientConfig ):
44- """Returns a sanitized config object from the builder's config after receiving a partial `ClientConfig` object."""
57+ """
58+ Return a sanitized config object from the builder's config.
59+
60+ Input is a partial `ClientConfig` object.
61+ """
4562 if new_config .envs :
4663 self .config .envs .update (new_config .envs )
4764 if new_config .interfaces :
@@ -55,18 +72,21 @@ def add(self, new_config: ClientConfig):
5572 return self
5673
5774 def get_envs (self ) -> Dict [Uri , Dict [str , Any ]]:
58- """Returns the envs dictionary from the builder's config."""
75+ """Return the envs dictionary from the builder's config."""
5976 return self .config .envs
6077
6178 def set_env (self , env : Env , uri : Uri ):
62- """Sets the envs dictionary in the builder's config, overiding any existing values."""
79+ """Set the envs dictionary in the builder's config, overiding any existing values."""
6380 self .config .envs [uri ] = env
6481 return self
6582
6683 def add_env (self , env : Env , uri : Uri ):
67- """Adds an environment (in the form of an `Env`) for a given uri, without overwriting existing environments,
68- unless the env key already exists in the environment, then it will overwrite the existing value"""
84+ """
85+ Add an environment ( in the form of an `Env`) for a given uri.
6986
87+ Note it is not overwriting existing environments, unless the
88+ env key already exists in the environment, then it will overwrite the existing value.
89+ """
7090 if uri in self .config .envs .keys ():
7191 for key in env .keys ():
7292 self .config .envs [uri ][key ] = env [key ]
@@ -75,19 +95,15 @@ def add_env(self, env: Env, uri: Uri):
7595 return self
7696
7797 def add_envs (self , envs : List [Env ], uri : Uri = None ):
78- """
79- Adds a list of environments (each in the form of an `Env`) for a given uri
80- """
98+ """Add a list of environments (each in the form of an `Env`) for a given uri."""
8199 for env in envs :
82100 self .add_env (env , uri )
83101 return self
84102
85103 def add_interface_implementations (
86104 self , interface_uri : Uri , implementations_uris : List [Uri ]
87105 ):
88- """
89- Adds a list of implementations (each in the form of an `Uri`) for a given interface
90- """
106+ """Add a list of implementations (each in the form of an `Uri`) for a given interface."""
91107 if interface_uri is None :
92108 raise ValueError ()
93109 if interface_uri in self .config .interfaces .keys ():
@@ -98,67 +114,50 @@ def add_interface_implementations(
98114 self .config .interfaces [interface_uri ] = implementations_uris
99115 return self
100116
101- def add_wrapper (self , wrapper_uri : Uri ):
102- """
103- Adds a wrapper to the list of wrappers
104- """
117+ def add_wrapper (self , wrapper_uri : UriWrapper ):
118+ """Add a wrapper to the list of wrappers."""
105119 self .config .wrappers .append (wrapper_uri )
106120 return self
107121
108- def add_wrappers (self , wrappers_uris : List [Uri ]):
109- """
110- Adds a list of wrappers to the list of wrappers
111- """
122+ def add_wrappers (self , wrappers_uris : List [UriWrapper ]):
123+ """Add a list of wrappers to the list of wrappers."""
112124 for wrapper_uri in wrappers_uris :
113125 self .add_wrapper (wrapper_uri )
114126 return self
115127
116- def remove_wrapper (self , wrapper_uri : Uri ):
117- """
118- Removes a wrapper from the list of wrappers
119- """
128+ def remove_wrapper (self , wrapper_uri : UriWrapper ):
129+ """Remove a wrapper from the list of wrappers."""
120130 self .config .wrappers .remove (wrapper_uri )
121131 return self
122132
123133 def set_package (self , uri_package : UriPackage ):
124- """
125- Sets the package in the builder's config, overiding any existing values.
126- """
134+ """Set the package in the builder's config, overiding any existing values."""
127135 self .config .packages = [uri_package ]
128136 return self
129137
130138 def add_package (self , uri_package : UriPackage ):
131- """
132- Adds a package to the list of packages
133- """
139+ """Add a package to the list of packages."""
134140 self .config .packages .append (uri_package )
135141 return self
142+
136143 def add_packages (self , uri_packages : List [UriPackage ]):
137- """
138- Adds a list of packages to the list of packages
139- """
144+ """Add a list of packages to the list of packages."""
140145 for uri_package in uri_packages :
141146 self .add_package (uri_package )
142147 return self
143148
144149 def remove_package (self , uri_package : UriPackage ):
145- """
146- Removes a package from the list of packages
147- """
150+ """Remove a package from the list of packages."""
148151 self .config .packages .remove (uri_package )
149152 return self
150153
151154 def set_resolver (self , uri_resolver : UriResolverLike ):
152- """
153- Sets a single resolver for the `ClientConfig` object before it is built
154- """
155+ """Set a single resolver for the `ClientConfig` object."""
155156 self .config .resolver = [uri_resolver ]
156157 return self
157158
158159 def add_resolver (self , resolver : UriResolverLike ):
159- """
160- Adds a resolver to the list of resolvers
161- """
160+ """Add a resolver to the list of resolvers."""
162161 if self .config .resolver is None :
163162 raise ValueError (
164163 "This resolver is not set. Please set a resolver before adding resolvers."
@@ -167,45 +166,43 @@ def add_resolver(self, resolver: UriResolverLike):
167166 return self
168167
169168 def add_resolvers (self , resolvers_list : List [UriResolverLike ]):
170- """
171- Adds a list of resolvers to the list of resolvers
172- """
169+ """Add a list of resolvers to the list of resolvers."""
173170 for resolver in resolvers_list :
174171 self .add_resolver (resolver )
175172 return self
176173
177174 def set_uri_redirect (self , uri_from : Uri , uri_to : Uri ):
178175 """
179- Sets an uri redirect, from one uri to another
180- If there was a redirect previously listed, it's changed to the new one
181- """
176+ Set an uri redirect, from one uri to another.
177+
178+ If there was a redirect previously listed, it's changed to the new one.
179+ """
182180 self .config .redirects [uri_from ] = uri_to
183181 return self
184-
182+
185183 def remove_uri_redirect (self , uri_from : Uri ):
186- """
187- Removes an uri redirect, from one uri to another
188- """
184+ """Remove an uri redirect, from one uri to another."""
189185 self .config .redirects .pop (uri_from )
190186 return self
191187
192- def set_uri_redirects (self , redirects : List [Dict [Uri ,Uri ]]):
193- """
194- Sets various Uri redirects from a list simultaneously
195- """
188+ def set_uri_redirects (self , redirects : List [Dict [Uri , Uri ]]):
189+ """Set various Uri redirects from a list simultaneously."""
196190 count = 0
197191 for redir in redirects :
198- for k , v in redir .items ():
199- self .set_uri_redirect (k , v )
192+ for key , value in redir .items ():
193+ self .set_uri_redirect (key , value )
200194 count += 1
201195 return self
202196
203197
204198class ClientConfigBuilder (BaseClientConfigBuilder ):
199+ """
200+ A class that can build the `ClientConfig` object.
201+
202+ This class inherits the `BaseClientConfigBuilder` class,
203+ and adds the `build` method
204+ """
205205
206206 def build (self ) -> ClientConfig :
207- """
208- Returns a sanitized config object from the builder's config.
209- """
207+ """Return a sanitized config object from the builder's config."""
210208 return self .config
211-
0 commit comments