Skip to content

Commit

Permalink
chore: uniform injector namespace access, easy to follow in ws
Browse files Browse the repository at this point in the history
  • Loading branch information
z3z1ma committed Jul 20, 2024
1 parent d3a2ec7 commit 8427192
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions src/cdf/nextgen/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@
import typing as t
from collections import ChainMap

from cdf.injector import (
ConfigResolver,
ConfigSource,
Dependency,
DependencyKey,
DependencyRegistry,
)
import cdf.injector as injector


class AbstractWorkspace(abc.ABC):
Expand All @@ -22,11 +16,11 @@ def get_environment(self) -> str:
pass

@abc.abstractmethod
def get_config_sources(self) -> t.Iterable[ConfigSource]:
def get_config_sources(self) -> t.Iterable[injector.ConfigSource]:
pass

@abc.abstractmethod
def get_services(self) -> t.Dict[DependencyKey, Dependency]:
def get_services(self) -> t.Dict[injector.DependencyKey, injector.Dependency]:
pass

@property
Expand All @@ -48,21 +42,23 @@ class Workspace(AbstractWorkspace):

def __init__(
self,
dependency_registry: DependencyRegistry = DependencyRegistry(),
config_resolver: ConfigResolver = ConfigResolver(),
dependency_registry: injector.DependencyRegistry = injector.DependencyRegistry(),
config_resolver: injector.ConfigResolver = injector.ConfigResolver(),
) -> None:
"""Initialize the workspace."""
self.injector = dependency_registry

for source in self.get_config_sources():
config_resolver.import_(source)
config_resolver.set_environment(self.get_environment())
self.add_dependency("cdf_config", Dependency.from_instance(config_resolver))
self.add_dependency(
"cdf_config", injector.Dependency.from_instance(config_resolver)
)
self.config_resolver = config_resolver

for name, definition in self.get_services().items():
if callable(definition.factory):
definition = Dependency(
definition = injector.Dependency(
config_resolver.inject_defaults(definition.factory), *definition[1:]
)
self.add_dependency(name, definition)
Expand All @@ -71,15 +67,17 @@ def get_environment(self) -> str:
"""Return the environment of the workspace."""
return os.getenv("CDF_ENVIRONMENT", "dev")

def get_config_sources(self) -> t.Iterable[ConfigSource]:
def get_config_sources(self) -> t.Iterable[injector.ConfigSource]:
"""Return a sequence of configuration sources."""
return ["cdf.toml", "cdf.yaml", "cdf.json", "~/.cdf.toml"]

def get_services(self) -> t.Dict[DependencyKey, Dependency]:
def get_services(self) -> t.Dict[injector.DependencyKey, injector.Dependency]:
"""Return a dictionary of services that the workspace provides."""
return {}

def add_dependency(self, name: DependencyKey, definition: Dependency) -> None:
def add_dependency(
self, name: injector.DependencyKey, definition: injector.Dependency
) -> None:
"""Add a dependency to the workspace DI container."""
self.injector.add_definition(name, definition)

Expand All @@ -94,11 +92,11 @@ class DataTeamWorkspace(Workspace):

def get_services(self):
return {
"a": Dependency(1),
"b": Dependency(lambda a: a + 1),
"prod_bigquery": Dependency("dwh-123"),
"sfdc": Dependency(
ConfigResolver.map_section("sfdc")(
"a": injector.Dependency(1),
"b": injector.Dependency(lambda a: a + 1),
"prod_bigquery": injector.Dependency("dwh-123"),
"sfdc": injector.Dependency(
injector.map_section("sfdc")(
lambda username: f"https://sfdc.com/{username}"
)
),
Expand All @@ -116,12 +114,12 @@ def get_config_sources(self):
# Create an instance of the workspace
datateam = DataTeamWorkspace()

@ConfigResolver.map_values(b="a.b.c")
@injector.map_values(b="a.b.c")
def c(b: int) -> int:
return b * 10

# Imperatively add dependencies
datateam.add_dependency("c", Dependency(c))
datateam.add_dependency("c", injector.Dependency(c))

def source_a(a: int, prod_bigquery: str):
print(f"Source A: {a=}, {prod_bigquery=}")
Expand Down

0 comments on commit 8427192

Please sign in to comment.