Skip to content

Commit

Permalink
Change config name to plural
Browse files Browse the repository at this point in the history
  • Loading branch information
shunichironomura committed Feb 25, 2024
1 parent 14f9513 commit 11f56eb
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 32 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -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:
Expand Down
12 changes: 6 additions & 6 deletions capsula.toml
Original file line number Diff line number Diff line change
@@ -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 },
Expand All @@ -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" }]
4 changes: 2 additions & 2 deletions capsula/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 15 additions & 15 deletions capsula/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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

Expand Down
6 changes: 3 additions & 3 deletions capsula/_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down

0 comments on commit 11f56eb

Please sign in to comment.