Skip to content

Commit faef876

Browse files
committed
refactor: client-config-builder package
1 parent b56c6b2 commit faef876

19 files changed

Lines changed: 592 additions & 241 deletions
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""This package contains modules related to client config builder."""
22

3-
from .client_config_builder import *
3+
from .polywrap_client_config_builder import *

packages/polywrap-client-config-builder/polywrap_client_config_builder/client_config_builder.py

Lines changed: 0 additions & 205 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from .base_configure import *
2+
from .env_configure import *
3+
from .interface_configure import *
4+
from .package_configure import *
5+
from .redirect_configure import *
6+
from .resolver_configure import *
7+
from .wrapper_configure import *
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from ..types import BuilderConfig, ClientConfigBuilder
2+
3+
4+
class BaseConfigure(ClientConfigBuilder):
5+
"""BaseConfigure is the base class for builder configures.
6+
7+
Attributes:
8+
config (BuilderConfig): The internal configuration.
9+
"""
10+
11+
config: BuilderConfig
12+
13+
def add(self, config: BuilderConfig) -> ClientConfigBuilder:
14+
"""Add the values from the given config to the builder's config."""
15+
if config.envs:
16+
self.config.envs.update(config.envs)
17+
if config.interfaces:
18+
self.config.interfaces.update(config.interfaces)
19+
if config.redirects:
20+
self.config.redirects.update(config.redirects)
21+
if config.resolvers:
22+
self.config.resolvers.extend(config.resolvers)
23+
if config.wrappers:
24+
self.config.wrappers.update(config.wrappers)
25+
if config.packages:
26+
self.config.packages.update(config.packages)
27+
return self
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from typing import Dict, List, Union
2+
3+
from polywrap_core import Env, Uri
4+
5+
from ..types import ClientConfigBuilder
6+
7+
8+
class EnvConfigure(ClientConfigBuilder):
9+
"""Allows configuring the environment variables."""
10+
11+
def get_env(self, uri: Uri) -> Union[Env, None]:
12+
"""Return the env for the given uri."""
13+
return self.config.envs.get(uri)
14+
15+
def get_envs(self) -> Dict[Uri, Env]:
16+
"""Return the envs from the builder's config."""
17+
return self.config.envs
18+
19+
def set_env(self, uri: Uri, env: Env) -> ClientConfigBuilder:
20+
"""Set the env by uri in the builder's config, overiding any existing values."""
21+
self.config.envs[uri] = env
22+
return self
23+
24+
def set_envs(self, uri_envs: Dict[Uri, Env]) -> ClientConfigBuilder:
25+
"""Set the envs in the builder's config, overiding any existing values."""
26+
self.config.envs.update(uri_envs)
27+
return self
28+
29+
def add_env(self, uri: Uri, env: Env) -> ClientConfigBuilder:
30+
"""Add an env for the given uri.
31+
32+
If an Env is already associated with the uri, it is modified.
33+
"""
34+
if self.config.envs.get(uri):
35+
for key in self.config.envs[uri]:
36+
self.config.envs[uri][key] = env[key]
37+
else:
38+
self.config.envs[uri] = env
39+
return self
40+
41+
def add_envs(self, uri_envs: Dict[Uri, Env]) -> ClientConfigBuilder:
42+
"""Add a list of envs to the builder's config."""
43+
for uri, env in uri_envs.items():
44+
self.add_env(uri, env)
45+
return self
46+
47+
def remove_env(self, uri: Uri) -> ClientConfigBuilder:
48+
"""Remove the env for the given uri."""
49+
self.config.envs.pop(uri, None)
50+
return self
51+
52+
def remove_envs(self, uris: List[Uri]) -> ClientConfigBuilder:
53+
"""Remove the envs for the given uris."""
54+
for uri in uris:
55+
self.remove_env(uri)
56+
return self
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from typing import Dict, List, Union
2+
3+
from polywrap_core import Uri
4+
5+
from ..types import ClientConfigBuilder
6+
7+
8+
class InterfaceConfigure(ClientConfigBuilder):
9+
"""Allows configuring the interface-implementations."""
10+
11+
def get_interfaces(self) -> Dict[Uri, List[Uri]]:
12+
"""Return all registered interface and its implementations\
13+
from the builder's config."""
14+
return self.config.interfaces
15+
16+
def get_interface_implementations(self, uri: Uri) -> Union[List[Uri], None]:
17+
"""Return the interface for the given uri."""
18+
return self.config.interfaces.get(uri)
19+
20+
def add_interface_implementations(
21+
self, interface_uri: Uri, implementations_uris: List[Uri]
22+
) -> ClientConfigBuilder:
23+
"""Add a list of implementation URIs for the given interface URI to the builder's config."""
24+
if interface_uri in self.config.interfaces.keys():
25+
self.config.interfaces[interface_uri].extend(implementations_uris)
26+
else:
27+
self.config.interfaces[interface_uri] = implementations_uris
28+
return self
29+
30+
def remove_interface_implementations(
31+
self, interface_uri: Uri, implementations_uris: List[Uri]
32+
) -> ClientConfigBuilder:
33+
"""Remove the implementations for the given interface uri."""
34+
self.config.interfaces[interface_uri] = [
35+
uri
36+
for uri in self.config.interfaces[interface_uri]
37+
if uri not in implementations_uris
38+
]
39+
return self
40+
41+
def remove_interface(self, interface_uri: Uri) -> ClientConfigBuilder:
42+
"""Remove the interface for the given uri."""
43+
self.config.interfaces.pop(interface_uri, None)
44+
return self
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from typing import Dict, List, Union
2+
3+
from polywrap_core import Uri, UriPackageOrWrapper, WrapPackage
4+
5+
from ..types import ClientConfigBuilder
6+
7+
8+
class PackageConfigure(ClientConfigBuilder):
9+
"""Allows configuring the WRAP packages."""
10+
11+
def get_package(self, uri: Uri) -> Union[WrapPackage[UriPackageOrWrapper], None]:
12+
"""Return the package for the given uri."""
13+
return self.config.packages.get(uri)
14+
15+
def get_packages(self) -> Dict[Uri, WrapPackage[UriPackageOrWrapper]]:
16+
"""Return the packages from the builder's config."""
17+
return self.config.packages
18+
19+
def set_package(
20+
self, uri: Uri, package: WrapPackage[UriPackageOrWrapper]
21+
) -> ClientConfigBuilder:
22+
"""Set the package by uri in the builder's config, overiding any existing values."""
23+
self.config.packages[uri] = package
24+
return self
25+
26+
def set_packages(
27+
self, uri_packages: Dict[Uri, WrapPackage[UriPackageOrWrapper]]
28+
) -> ClientConfigBuilder:
29+
"""Set the packages in the builder's config, overiding any existing values."""
30+
self.config.packages.update(uri_packages)
31+
return self
32+
33+
def remove_package(self, uri: Uri) -> ClientConfigBuilder:
34+
"""Remove the package for the given uri."""
35+
self.config.packages.pop(uri, None)
36+
return self
37+
38+
def remove_packages(self, uris: List[Uri]) -> ClientConfigBuilder:
39+
"""Remove the packages for the given uris."""
40+
for uri in uris:
41+
self.remove_package(uri)
42+
return self

0 commit comments

Comments
 (0)