Skip to content

configure

configure #

The active scratch: a process-wide default, or one scoped to the current context.

configure installs the default every thread shares (an explicit path, else CG_SCRATCH); temporary overrides it for one with block in the calling thread or task only, so a throwaway scratch in one worker never redirects another.

scoped_scratch = ContextVar('cg_scoped_scratch', default=None) module-attribute #

The scratch overriding the default in the current context, if any.

active_scratch() #

The scratch in effect: the context's override, else the default.

The default is configured from CG_SCRATCH on first use.

Raises:

Type Description
ScratchNotConfigured

If none is configured and CG_SCRATCH is unset.

Source code in capturegraph-lib/capturegraph/recipes/scratch/configure.py
def active_scratch() -> Scratch:
    """The scratch in effect: the context's override, else the default.

    The default is configured from ``CG_SCRATCH`` on first use.

    Raises:
        ScratchNotConfigured: If none is configured and ``CG_SCRATCH`` is unset.
    """
    scoped = scoped_scratch.get()
    if scoped is not None:
        return scoped
    if _default is None:
        configure()
    assert _default is not None
    return _default

configure(path=None, *, max_size_gb=None, free_space_ratio=None) #

Open (or initialize) the process-wide default scratch.

Parameters:

Name Type Description Default
path str | Path | None

The scratch root. Defaults to CG_SCRATCH when omitted.

None
max_size_gb float | None

Hard cap on the cache's byte budget, in gigabytes.

None
free_space_ratio float | None

Fraction of releasable disk the cache may claim.

None

Returns:

Type Description
Path

The configured scratch's root directory.

Raises:

Type Description
ScratchNotConfigured

If no path is given and CG_SCRATCH is unset.

Source code in capturegraph-lib/capturegraph/recipes/scratch/configure.py
def configure(
    path: str | Path | None = None,
    *,
    max_size_gb: float | None = None,
    free_space_ratio: float | None = None,
) -> Path:
    """Open (or initialize) the process-wide default scratch.

    Args:
        path: The scratch root. Defaults to ``CG_SCRATCH`` when omitted.
        max_size_gb: Hard cap on the cache's byte budget, in gigabytes.
        free_space_ratio: Fraction of releasable disk the cache may claim.

    Returns:
        The configured scratch's root directory.

    Raises:
        ScratchNotConfigured: If no ``path`` is given and ``CG_SCRATCH`` is unset.
    """
    global _default
    resolved = path if path is not None else os.environ.get(SCRATCH_ENV)
    if resolved is None:
        raise ScratchNotConfigured(
            "No CaptureGraph scratch is configured. Set one with "
            "cg.scratch.configure(path), scope a throwaway one with "
            "cg.scratch.temporary(), or export the CG_SCRATCH environment variable."
        )
    _default = Scratch(
        Path(resolved),
        max_size_gb=max_size_gb,
        free_space_ratio=free_space_ratio,
    )
    return _default.root

current_scratch() #

The scratch in effect without configuring one; None if unset.

Source code in capturegraph-lib/capturegraph/recipes/scratch/configure.py
def current_scratch() -> Scratch | None:
    """The scratch in effect without configuring one; ``None`` if unset."""
    return scoped_scratch.get() or _default

unconfigure() #

Forget the process-wide default, as if configure had never run.

Source code in capturegraph-lib/capturegraph/recipes/scratch/configure.py
def unconfigure() -> None:
    """Forget the process-wide default, as if ``configure`` had never run."""
    global _default
    _default = None