Skip to content

Commit

Permalink
feat: add placeholder for dynamic cli gen and inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
z3z1ma committed Jul 20, 2024
1 parent b766a9b commit 0017529
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
5 changes: 2 additions & 3 deletions src/cdf/injector/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
from cdf.injector.registry import (
GLOBAL_REGISTRY,
Dependency,
DependencyKey,
DependencyRegistry,
Lifecycle,
StringOrKey,
)

__all__ = [
Expand All @@ -22,11 +22,10 @@
"ConfigSource",
"Dependency",
"DependencyRegistry",
"StringOrKey",
"DependencyKey",
"add_custom_converter",
"remove_converter",
"Lifecycle",
"StringOrKey",
"GLOBAL_REGISTRY",
"load_file",
"map_section",
Expand Down
16 changes: 8 additions & 8 deletions src/cdf/injector/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def __str__(self) -> str:
return f"{self.name}: {self.type_name}"

def __repr__(self) -> str:
return f"<DependencyKey {self}>"
return f"<TypedKey {self}>"

def __eq__(self, other: t.Any) -> bool:
"""Two keys are equal if their names and base types match.
Expand All @@ -74,7 +74,7 @@ def __hash__(self) -> int:
return hash((self.name, _get_effective_type(self.type_)))


StringOrKey = t.Union[str, t.Tuple[str, t.Type[t.Any]], TypedKey]
DependencyKey = t.Union[str, t.Tuple[str, t.Type[t.Any]], TypedKey]
"""A string or a typed key."""


Expand Down Expand Up @@ -187,7 +187,7 @@ def dependencies(self) -> ChainMap[t.Any, Dependency]:

def add(
self,
name_or_key: StringOrKey,
name_or_key: DependencyKey,
factory: t.Any,
lifecycle: t.Optional[Lifecycle] = None,
override: bool = False,
Expand Down Expand Up @@ -232,7 +232,7 @@ def add(

def add_definition(
self,
name_or_key: StringOrKey,
name_or_key: DependencyKey,
definition: Dependency,
override: bool = False,
) -> None:
Expand All @@ -246,7 +246,7 @@ def add_definition(
**definition.init_args[1],
)

def remove(self, name_or_key: StringOrKey) -> None:
def remove(self, name_or_key: DependencyKey) -> None:
"""Remove a dependency by name or key from the container."""
key = _normalize_key(name_or_key)
if isinstance(key, str):
Expand All @@ -266,11 +266,11 @@ def clear(self) -> None:
self._untyped_dependencies.clear()
self._singletons.clear()

def has(self, name_or_key: StringOrKey) -> bool:
def has(self, name_or_key: DependencyKey) -> bool:
"""Check if a dependency is registered."""
return name_or_key in self.dependencies

def get(self, name_or_key: StringOrKey, must_exist: bool = False) -> t.Any:
def get(self, name_or_key: DependencyKey, must_exist: bool = False) -> t.Any:
"""Get a dependency"""
key = _normalize_key(name_or_key)
if isinstance(key, str):
Expand Down Expand Up @@ -428,6 +428,6 @@ def __setstate__(self, state: t.Dict[str, t.Any]) -> None:
"DependencyRegistry",
"Dependency",
"Lifecycle",
"StringOrKey",
"DependencyKey",
"GLOBAL_REGISTRY",
]
38 changes: 34 additions & 4 deletions src/cdf/nextgen/workspace.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import abc
import os
import string
import typing as t
Expand All @@ -10,12 +11,39 @@
ConfigResolver,
ConfigSource,
Dependency,
DependencyKey,
DependencyRegistry,
StringOrKey,
)


class Workspace:
class AbstractWorkspace(abc.ABC):
name: str
version: str = "0.1.0"

@abc.abstractmethod
def get_environment(self) -> str:
pass

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

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

@property
def cli(self) -> t.Callable:
import click

@click.command()
def entrypoint():
click.echo(f"Hello, {self.name} {self.version}!")

return entrypoint


class Workspace(AbstractWorkspace):
"""A CDF workspace that allows for dependency injection."""

name: str
Expand Down Expand Up @@ -50,11 +78,11 @@ def get_config_sources(self) -> t.Iterable[ConfigSource]:
"""Return a sequence of configuration sources."""
return ["cdf.toml", "cdf.yaml", "cdf.json", "~/.cdf.toml"]

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

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

Expand Down Expand Up @@ -105,3 +133,5 @@ def source_a(a: int, prod_bigquery: str):
print(datateam.config_resolver["sfdc.username"])

print(datateam.injector.get_or_raise("sfdc"))

datateam.cli()

0 comments on commit 0017529

Please sign in to comment.