diff --git a/README.md b/README.md index e1cad3df..7d994215 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ Prepare a `capsula.toml` file in the root directory of your project. An example ```toml [pre-run] # Contexts to be captured before the execution of the decorated function/CLI command. -context = [ +contexts = [ { type = "CommandContext", command = "poetry check --lock" }, { type = "CommandContext", command = "pip freeze --exclude-editable > requirements.txt" }, { type = "FileContext", path = "pyproject.toml", copy = true }, @@ -35,19 +35,19 @@ context = [ { type = "CpuContext" }, ] # Reporter to be used to report the captured contexts. -reporter = [{ type = "JsonDumpReporter" }] +reporters = [{ type = "JsonDumpReporter" }] [in-run] # Watchers to be used during the execution of the decorated function/CLI command. -watcher = [{ type = "UncaughtExceptionWatcher" }, { type = "TimeWatcher" }] +watchers = [{ type = "UncaughtExceptionWatcher" }, { type = "TimeWatcher" }] # Reporter to be used to report the execution status. -reporter = [{ type = "JsonDumpReporter" }] +reporters = [{ type = "JsonDumpReporter" }] [post-run] # Contexts to be captured after the execution of the decorated function/CLI command. -context = [{ type = "FileContext", path = "examples/pi.txt", move = true }] +contexts = [{ type = "FileContext", path = "examples/pi.txt", move = true }] # Reporter to be used to report the captured contexts. -reporter = [{ type = "JsonDumpReporter" }] +reporters = [{ type = "JsonDumpReporter" }] ``` Then, all you need to do is decorate your Python function with the `@capsula.run` decorator and specify the `load_from_config` argument as `True`. The following is an example of a Python script that estimates the value of π using the Monte Carlo method: diff --git a/capsula.toml b/capsula.toml index ef6e8e41..2d1ccff1 100644 --- a/capsula.toml +++ b/capsula.toml @@ -1,5 +1,5 @@ [pre-run] -context = [ +contexts = [ { type = "CommandContext", command = "poetry check --lock" }, { type = "CommandContext", command = "pip freeze --exclude-editable > requirements.txt" }, { type = "FileContext", path = "pyproject.toml", copy = true }, @@ -8,12 +8,12 @@ context = [ { type = "CwdContext" }, { type = "CpuContext" }, ] -reporter = [{ type = "JsonDumpReporter" }] +reporters = [{ type = "JsonDumpReporter" }] [in-run] -watcher = [{ type = "UncaughtExceptionWatcher" }, { type = "TimeWatcher" }] -reporter = [{ type = "JsonDumpReporter" }] +watchers = [{ type = "UncaughtExceptionWatcher" }, { type = "TimeWatcher" }] +reporters = [{ type = "JsonDumpReporter" }] [post-run] -context = [{ type = "FileContext", path = "examples/pi.txt", move = true }] -reporter = [{ type = "JsonDumpReporter" }] +contexts = [{ type = "FileContext", path = "examples/pi.txt", move = true }] +reporters = [{ type = "JsonDumpReporter" }] diff --git a/capsula/_cli.py b/capsula/_cli.py index dc7cdc12..9600a9ef 100644 --- a/capsula/_cli.py +++ b/capsula/_cli.py @@ -37,8 +37,8 @@ def enc( config = load_config(get_default_config_path()) enc = capsula.Encapsulator() phase_key: Literal["pre-run", "post-run"] = f"{phase.value}-run" # type: ignore[assignment] - contexts = config[phase_key]["context"] - reporters = config[phase_key]["reporter"] + contexts = config[phase_key]["contexts"] + reporters = config[phase_key]["reporters"] exec_info = None run_dir = generate_default_run_dir(exec_info=exec_info) diff --git a/capsula/_config.py b/capsula/_config.py index ef0a2149..a2d152c9 100644 --- a/capsula/_config.py +++ b/capsula/_config.py @@ -31,18 +31,18 @@ def _construct_reporter(raw_config: MutableMapping[str, Any]) -> Callable[[Capsu class _PreRunConfig(TypedDict): - context: list[Callable[[CapsuleParams], ContextBase] | ContextBase] - reporter: list[Callable[[CapsuleParams], ReporterBase] | ReporterBase] + contexts: list[Callable[[CapsuleParams], ContextBase] | ContextBase] + reporters: list[Callable[[CapsuleParams], ReporterBase] | ReporterBase] class _InRunConfig(TypedDict): - watcher: list[Callable[[CapsuleParams], WatcherBase] | WatcherBase] - reporter: list[Callable[[CapsuleParams], ReporterBase] | ReporterBase] + watchers: list[Callable[[CapsuleParams], WatcherBase] | WatcherBase] + reporters: list[Callable[[CapsuleParams], ReporterBase] | ReporterBase] class _PostRunConfig(TypedDict): - context: list[Callable[[CapsuleParams], ContextBase] | ContextBase] - reporter: list[Callable[[CapsuleParams], ReporterBase] | ReporterBase] + contexts: list[Callable[[CapsuleParams], ContextBase] | ContextBase] + reporters: list[Callable[[CapsuleParams], ReporterBase] | ReporterBase] _CapsulaConfig = TypedDict( @@ -60,20 +60,20 @@ def load_config(path: Path) -> _CapsulaConfig: raw_config = tomllib.load(file) config: _CapsulaConfig = { - "pre-run": {"context": [], "reporter": []}, - "in-run": {"watcher": [], "reporter": []}, - "post-run": {"context": [], "reporter": []}, + "pre-run": {"contexts": [], "reporters": []}, + "in-run": {"watchers": [], "reporters": []}, + "post-run": {"contexts": [], "reporters": []}, } if (pre_run := raw_config.get("pre-run")) is not None: - config["pre-run"]["context"] = list(map(_construct_context, pre_run.get("context", []))) - config["pre-run"]["reporter"] = list(map(_construct_reporter, pre_run.get("reporter", []))) + config["pre-run"]["contexts"] = list(map(_construct_context, pre_run.get("contexts", []))) + config["pre-run"]["reporters"] = list(map(_construct_reporter, pre_run.get("reporters", []))) if (in_run := raw_config.get("in-run")) is not None: - config["in-run"]["watcher"] = list(map(_construct_watcher, in_run.get("watcher", []))) - config["in-run"]["reporter"] = list(map(_construct_reporter, in_run.get("reporter", []))) + config["in-run"]["watchers"] = list(map(_construct_watcher, in_run.get("watchers", []))) + config["in-run"]["reporters"] = list(map(_construct_reporter, in_run.get("reporters", []))) if (post_run := raw_config.get("post-run")) is not None: - config["post-run"]["context"] = list(map(_construct_context, post_run.get("context", []))) - config["post-run"]["reporter"] = list(map(_construct_reporter, post_run.get("reporter", []))) + config["post-run"]["contexts"] = list(map(_construct_context, post_run.get("contexts", []))) + config["post-run"]["reporters"] = list(map(_construct_reporter, post_run.get("reporters", []))) return config diff --git a/capsula/_decorator.py b/capsula/_decorator.py index 1f6e4d1e..cbf08b5d 100644 --- a/capsula/_decorator.py +++ b/capsula/_decorator.py @@ -71,13 +71,13 @@ def decorator(func_or_run: Callable[_P, _T] | Run[_P, _T]) -> Run[_P, _T]: phase_key = f"{phase}-run" if phase_key not in config: continue - for context in config[phase_key].get("context", []): # type: ignore[literal-required] + for context in config[phase_key].get("contexts", []): # type: ignore[literal-required] assert phase in {"pre", "post"}, f"Invalid phase for context: {phase}" run.add_context(context, mode=phase) # type: ignore[arg-type] - for watcher in config[phase_key].get("watcher", []): # type: ignore[literal-required] + for watcher in config[phase_key].get("watchers", []): # type: ignore[literal-required] assert phase == "in", "Watcher can only be added to the in-run phase." run.add_watcher(watcher) - for reporter in config[phase_key].get("reporter", []): # type: ignore[literal-required] + for reporter in config[phase_key].get("reporters", []): # type: ignore[literal-required] assert phase in {"pre", "in", "post"}, f"Invalid phase for reporter: {phase}" run.add_reporter(reporter, mode=phase) # type: ignore[arg-type]