-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: match improve interface for publishers and lift bare funcs in …
…validators
- Loading branch information
Showing
4 changed files
with
63 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,37 @@ | ||
import typing as t | ||
|
||
import pydantic | ||
|
||
from .base import Entrypoint, _get_bind_func, _unwrap_entrypoint | ||
from .base import Entrypoint | ||
|
||
|
||
def _ping() -> bool: | ||
"""A default preflight check which always returns True.""" | ||
return bool("pong") | ||
|
||
|
||
class DataPublisher(Entrypoint[t.Any], frozen=True): | ||
class DataPublisher( | ||
Entrypoint[ | ||
t.Tuple[ | ||
t.Callable[..., None], # run | ||
t.Callable[..., bool], # preflight | ||
t.Optional[t.Callable[..., None]], # success hook | ||
t.Optional[t.Callable[..., None]], # failure hook | ||
] | ||
], | ||
frozen=True, | ||
): | ||
"""A data publisher which pushes data to an operational system.""" | ||
|
||
preflight_check: t.Callable[..., bool] = _ping | ||
"""A user defined function to check if the data publisher is able to publish data""" | ||
|
||
integration_test: t.Optional[t.Callable[..., bool]] = None | ||
"""A function to test the data publisher in an integration environment""" | ||
|
||
@pydantic.field_validator("preflight_check", "integration_test", mode="before") | ||
@classmethod | ||
def _bind_ancillary(cls, value: t.Any, info: pydantic.ValidationInfo) -> t.Any: | ||
"""Bind the active workspace to the ancillary functions.""" | ||
return _get_bind_func(info)(_unwrap_entrypoint(value)) | ||
|
||
def __call__(self, *args: t.Any, **kwargs: t.Any) -> None: | ||
"""Publish the data""" | ||
if not self.preflight_check(): | ||
raise RuntimeError("Preflight check failed") | ||
return self.main(*args, **kwargs) | ||
publisher, pre, success, err = self.main(*args, **kwargs) | ||
if not pre(): | ||
raise ValueError("Preflight check failed") | ||
try: | ||
return publisher() | ||
except Exception as e: | ||
if err: | ||
err() | ||
raise e | ||
else: | ||
if success: | ||
success() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters