Skip to content

Commit

Permalink
GitButler WIP Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gitbutler-client authored and shunichironomura committed Sep 23, 2024
1 parent b5939cd commit 90fce33
Show file tree
Hide file tree
Showing 7 changed files with 1,520 additions and 2 deletions.
2 changes: 2 additions & 0 deletions capsula.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
vault-dir = "vault"

[pre-run]
contexts = [
{ type = "CwdContext" },
Expand Down
12 changes: 11 additions & 1 deletion capsula/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,29 @@ class _PostRunConfig(TypedDict):
_CapsulaConfig = TypedDict(
"_CapsulaConfig",
{
"vault-dir": Path,
"pre-run": _PreRunConfig,
"in-run": _InRunConfig,
"post-run": _PostRunConfig,
},
)


# TODO: Treat relative paths in the config file as relative to project root
def load_config(path: Path) -> _CapsulaConfig:
with path.open("rb") as file:
raw_config = tomllib.load(file)

project_root = path.parent
if "vault-dir" in raw_config:
if Path(raw_config["vault-dir"]).is_absolute():
vault_dir = Path(raw_config["vault-dir"])
else:
vault_dir = project_root / Path(raw_config["vault-dir"])
else:
vault_dir = project_root / "vault"

config: _CapsulaConfig = {
"vault-dir": vault_dir,
"pre-run": {"contexts": [], "reporters": []},
"in-run": {"watchers": [], "reporters": []},
"post-run": {"contexts": [], "reporters": []},
Expand Down
13 changes: 13 additions & 0 deletions capsula/_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ def run( # noqa: C901
Path | str | None,
Doc("Path to the configuration file. If not specified, the default configuration file will be used."),
] = None,
vault_dir: Annotated[
Path | str | None,
Doc("Path to the vault directory."),
] = None,
) -> Annotated[Callable[[Callable[_P, _T] | Run[_P, _T]], Run[_P, _T]], Doc("Decorator to create a `Run` object.")]:
"""Decorator to create a `Run` object.
Expand All @@ -159,6 +163,15 @@ def run( # noqa: C901
def func() -> None: ...
```
The run directory is determined by the following priority:
1. If `run_dir` argument is set, it will be used as the run directory.
2a. If `vault_dir` argument is set, it will be used as the vault directory.
2b. If `ignore_config` argument is True and `vault-dir` field is present in the config file,
it will be used as the vault directory.
2c. The default vault directory is used.
3a. If `run_name_factory` argument is set, it will be used as the run name.
3b. The default run name factory is used.
"""
if run_dir is not _NOT_SET:
warnings.warn(
Expand Down
21 changes: 20 additions & 1 deletion capsula/_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import threading
import warnings
from collections import deque
from dataclasses import dataclass
from dataclasses import dataclass, field
from datetime import datetime, timezone
from pathlib import Path
from random import choices
Expand Down Expand Up @@ -109,6 +109,24 @@ def get_project_root(exec_info: ExecInfo | None = None) -> Path:
raise TypeError(msg)


@dataclass
class RunDto(Generic[P, T]):
pass_pre_run_capsule: bool
func: Callable[P, T] | Callable[Concatenate[Capsule, P], T]
run_dir: Path | None = None
# Deprecated. Will be removed in v0.7.0.
run_dir_generator: Callable[[FuncInfo], Path] | None = None

run_name_factory: Callable[[ExecInfo | None, str, datetime], str] | None = None
pre_run_context_generators: deque[Callable[[CapsuleParams], ContextBase]] = field(default_factory=deque)
in_run_watcher_generators: deque[Callable[[CapsuleParams], WatcherBase]] = field(default_factory=deque)
post_run_context_generators: deque[Callable[[CapsuleParams], ContextBase]] = field(default_factory=deque)
pre_run_reporter_generators: deque[Callable[[CapsuleParams], ReporterBase]] = field(default_factory=deque)
in_run_reporter_generators: deque[Callable[[CapsuleParams], ReporterBase]] = field(default_factory=deque)
post_run_reporter_generators: deque[Callable[[CapsuleParams], ReporterBase]] = field(default_factory=deque)


# TODO: Create RunDto or IncompleteRun to hold incomplete attribute data
class Run(Generic[P, T]):
_thread_local = threading.local()

Expand Down Expand Up @@ -299,6 +317,7 @@ def _instanciate_run_dir(self, func_info: FuncInfo) -> Path:
"".join(choices(ascii_letters + digits, k=4)),
datetime.now(timezone.utc),
)
# TODO: Use specified vault_dir
return get_vault_dir(func_info) / run_name

# YORE: Bump 0.7.0: Remove block.
Expand Down
1 change: 1 addition & 0 deletions capsula/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def search_for_project_root(
start: Annotated[Path | str, Doc("The start directory to search.")],
) -> Annotated[Path, Doc("The project root directory.")]:
"""Search for the project root directory by looking for pyproject.toml."""
# TODO: Allow projects without pyproject.toml file
start = Path(start)
if (start / "pyproject.toml").exists():
return start
Expand Down
13 changes: 13 additions & 0 deletions tests/test_run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import logging

import capsula

logger = logging.getLogger(__name__)


def test_default_run_dir() -> None:
@capsula.run(ignore_config=True)
def f(x: int, y: int) -> int:
return x + y

logger.info(f"Run directory: {f.run_dir}")
Loading

0 comments on commit 90fce33

Please sign in to comment.