-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #148 from shunichironomura/decorator-v2
Implement a new capsula.run decorator
- Loading branch information
Showing
32 changed files
with
628 additions
and
100 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
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from capsula._capsule import CapsuleItem | ||
|
||
|
||
class ContextBase(CapsuleItem): | ||
pass |
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
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
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 |
---|---|---|
@@ -0,0 +1,132 @@ | ||
from __future__ import annotations | ||
|
||
from functools import wraps | ||
from pathlib import Path | ||
from typing import TYPE_CHECKING, Callable, Literal, Tuple, TypeVar, Union | ||
|
||
from capsula._reporter import ReporterBase | ||
from capsula.encapsulator import Encapsulator | ||
|
||
from ._backport import ParamSpec | ||
from ._context import ContextBase | ||
from ._run import CapsuleParams, FuncInfo, Run | ||
from ._watcher import WatcherBase | ||
|
||
if TYPE_CHECKING: | ||
from collections.abc import Sequence | ||
|
||
from ._backport import TypeAlias | ||
|
||
_P = ParamSpec("_P") | ||
_T = TypeVar("_T") | ||
|
||
|
||
_ContextInput: TypeAlias = Union[ | ||
ContextBase, | ||
Tuple[ContextBase, Tuple[str, ...]], | ||
Callable[[Path, Callable], Union[ContextBase, Tuple[ContextBase, Tuple[str, ...]]]], | ||
] | ||
_WatcherInput: TypeAlias = Union[ | ||
WatcherBase, | ||
Tuple[WatcherBase, Tuple[str, ...]], | ||
Callable[[Path, Callable], Union[WatcherBase, Tuple[WatcherBase, Tuple[str, ...]]]], | ||
] | ||
_ReporterInput: TypeAlias = Union[ReporterBase, Callable[[Path, Callable], ReporterBase]] | ||
|
||
|
||
def capsule( # noqa: C901 | ||
capsule_directory: Path | str | None = None, | ||
pre_run_contexts: Sequence[_ContextInput] | None = None, | ||
pre_run_reporters: Sequence[_ReporterInput] | None = None, | ||
in_run_watchers: Sequence[_WatcherInput] | None = None, | ||
post_run_contexts: Sequence[_ContextInput] | None = None, | ||
) -> Callable[[Callable[_P, _T]], Callable[_P, _T]]: | ||
if capsule_directory is None: | ||
raise NotImplementedError | ||
capsule_directory = Path(capsule_directory) | ||
|
||
assert pre_run_contexts is not None | ||
assert pre_run_reporters is not None | ||
assert in_run_watchers is not None | ||
assert post_run_contexts is not None | ||
|
||
def decorator(func: Callable[_P, _T]) -> Callable[_P, _T]: | ||
pre_run_enc = Encapsulator() | ||
for cxt in pre_run_contexts: | ||
if isinstance(cxt, ContextBase): | ||
pre_run_enc.add_context(cxt) | ||
elif isinstance(cxt, tuple): | ||
pre_run_enc.add_context(cxt[0], key=cxt[1]) | ||
else: | ||
cxt_hydrated = cxt(capsule_directory, func) | ||
if isinstance(cxt_hydrated, ContextBase): | ||
pre_run_enc.add_context(cxt_hydrated) | ||
elif isinstance(cxt_hydrated, tuple): | ||
pre_run_enc.add_context(cxt_hydrated[0], key=cxt_hydrated[1]) | ||
|
||
@wraps(func) | ||
def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> _T: | ||
capsule_directory.mkdir(parents=True, exist_ok=True) | ||
pre_run_capsule = pre_run_enc.encapsulate() | ||
for reporter in pre_run_reporters: | ||
if isinstance(reporter, ReporterBase): | ||
reporter.report(pre_run_capsule) | ||
else: | ||
reporter(capsule_directory, func).report(pre_run_capsule) | ||
|
||
return func(*args, **kwargs) | ||
|
||
return wrapper | ||
|
||
return decorator | ||
|
||
|
||
def watcher( | ||
watcher: WatcherBase | Callable[[CapsuleParams], WatcherBase], | ||
) -> Callable[[Callable[_P, _T] | Run[_P, _T]], Run[_P, _T]]: | ||
def decorator(func_or_run: Callable[_P, _T] | Run[_P, _T]) -> Run[_P, _T]: | ||
func = func_or_run.func if isinstance(func_or_run, Run) else func_or_run | ||
run = func_or_run if isinstance(func_or_run, Run) else Run(func) | ||
run.add_watcher(watcher) | ||
return run | ||
|
||
return decorator | ||
|
||
|
||
def reporter( | ||
reporter: ReporterBase | Callable[[CapsuleParams], ReporterBase], | ||
mode: Literal["pre", "in", "post", "all"], | ||
) -> Callable[[Callable[_P, _T] | Run[_P, _T]], Run[_P, _T]]: | ||
def decorator(func_or_run: Callable[_P, _T] | Run[_P, _T]) -> Run[_P, _T]: | ||
func = func_or_run.func if isinstance(func_or_run, Run) else func_or_run | ||
run = func_or_run if isinstance(func_or_run, Run) else Run(func) | ||
run.add_reporter(reporter, mode=mode) | ||
return run | ||
|
||
return decorator | ||
|
||
|
||
def context( | ||
context: ContextBase | Callable[[CapsuleParams], ContextBase], | ||
mode: Literal["pre", "post", "all"], | ||
) -> Callable[[Callable[_P, _T] | Run[_P, _T]], Run[_P, _T]]: | ||
def decorator(func_or_run: Callable[_P, _T] | Run[_P, _T]) -> Run[_P, _T]: | ||
func = func_or_run.func if isinstance(func_or_run, Run) else func_or_run | ||
run = func_or_run if isinstance(func_or_run, Run) else Run(func) | ||
run.add_context(context, mode=mode) | ||
return run | ||
|
||
return decorator | ||
|
||
|
||
def run( | ||
run_dir: Path | Callable[[FuncInfo], Path], | ||
) -> Callable[[Callable[_P, _T] | Run[_P, _T]], Run[_P, _T]]: | ||
def decorator(func_or_run: Callable[_P, _T] | Run[_P, _T]) -> Run[_P, _T]: | ||
func = func_or_run.func if isinstance(func_or_run, Run) else func_or_run | ||
run = func_or_run if isinstance(func_or_run, Run) else Run(func) | ||
run.set_run_dir(run_dir) | ||
|
||
return run | ||
|
||
return decorator |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
__all__ = ["JsonDumpReporter", "ReporterBase"] | ||
from ._base import ReporterBase | ||
from ._json import JsonDumpReporter |
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
Oops, something went wrong.