FileContext
The FileContext
captures the file information.
It can be created using the capsula.FileContext.builder
method (recommended) or the capsula.FileContext.__init__
method.
capsula.FileContext.builder
classmethod
builder(
path: Path | str,
*,
compute_hash: bool = True,
hash_algorithm: str | None = None,
copy: bool = False,
move: bool = False,
ignore_missing: bool = False,
path_relative_to_project_root: bool = False
) -> Callable[[CapsuleParams], FileContext]
PARAMETER | DESCRIPTION |
---|---|
path |
Path to the file
TYPE:
|
compute_hash |
Whether to compute the hash of the file
TYPE:
|
hash_algorithm |
Hash algorithm to use. This will be fed to
TYPE:
|
copy |
Whether to copy the file to the run directory
TYPE:
|
move |
Whether to move the file to the run directory
TYPE:
|
ignore_missing |
Whether to ignore if the file does not exist
TYPE:
|
path_relative_to_project_root |
Whether
TYPE:
|
Source code in capsula/_context/_file.py
capsula.FileContext.__init__
__init__(
path: Path | str,
*,
compute_hash: bool = True,
hash_algorithm: str | None = None,
copy_to: (
Iterable[Path | str] | Path | str | None
) = None,
move_to: Path | str | None = None,
ignore_missing: bool = False
)
PARAMETER | DESCRIPTION |
---|---|
path |
TYPE:
|
compute_hash |
TYPE:
|
hash_algorithm |
TYPE:
|
copy_to |
TYPE:
|
move_to |
TYPE:
|
ignore_missing |
TYPE:
|
Source code in capsula/_context/_file.py
Configuration example
Via capsula.toml
[pre-run]
contexts = [
{ type = "FileContext", path = "pyproject.toml", copy = true, path_relative_to_project_root = true },
]
Via @capsula.context
decorator
@capsula.context
decorator is useful to move the output file to the run directory after the function execution.
import capsula
PROJECT_ROOT = capsula.search_for_project_root(__file__)
@capsula.run()
@capsula.context(capsula.FileContext.builder(PROJECT_ROOT / "pyproject.toml", copy=True), mode="pre")
@capsula.context(capsula.FileContext.builder("output.txt", move=True), mode="post")
def func():
with open("output.txt", "w") as output_file:
output_file.write("Hello, world!")
Output example
The following is an example of the output of the FileContext
, reported by the JsonDumpReporter
:
"file": {
"/home/nomura/ghq/github.com/shunichironomura/capsula/pyproject.toml": {
"copied_to": [
"/home/nomura/ghq/github.com/shunichironomura/capsula/vault/20240708_024409_coj0/pyproject.toml"
],
"moved_to": null,
"hash": {
"algorithm": "sha256",
"digest": "1ecab310035eea9c07fad2a8b22a16f999cd4d8c59fa1732c088f754af548ad9"
}
},
}