Skip to content

registry

registry #

The process's open steps, and the watchers told when one opens or closes.

registry = Registry() module-attribute #

The one registry of this process.

Registry #

Every open step of this process; watchers are called outside the lock.

Source code in capturegraph-lib/capturegraph/recipes/progress/registry.py
class Registry:
    """Every open step of this process; watchers are called outside the lock."""

    def __init__(self) -> None:
        """An empty registry with no watchers."""
        self._lock = threading.Lock()
        self._steps: dict[str, Step] = {}
        self._watchers: list[Watcher] = []

    def open(self, step: Step) -> None:
        """Register ``step`` and tell the watchers."""
        with self._lock:
            self._steps[step.id] = step
            watchers = list(self._watchers)
        for watcher in watchers:
            watcher.opened(step)

    def close(self, step: Step) -> None:
        """Forget ``step`` and tell the watchers."""
        with self._lock:
            self._steps.pop(step.id, None)
            watchers = list(self._watchers)
        for watcher in watchers:
            watcher.closed(step)

    def live(self) -> list[Step]:
        """Every open step, oldest first."""
        with self._lock:
            return sorted(self._steps.values(), key=lambda step: step.started_at)

    def watch(self, watcher: Watcher) -> None:
        """Tell ``watcher`` about every step from now on."""
        with self._lock:
            self._watchers.append(watcher)

    def unwatch(self, watcher: Watcher) -> None:
        """Stop telling ``watcher``."""
        with self._lock:
            self._watchers.remove(watcher)

__init__() #

An empty registry with no watchers.

Source code in capturegraph-lib/capturegraph/recipes/progress/registry.py
def __init__(self) -> None:
    """An empty registry with no watchers."""
    self._lock = threading.Lock()
    self._steps: dict[str, Step] = {}
    self._watchers: list[Watcher] = []

close(step) #

Forget step and tell the watchers.

Source code in capturegraph-lib/capturegraph/recipes/progress/registry.py
def close(self, step: Step) -> None:
    """Forget ``step`` and tell the watchers."""
    with self._lock:
        self._steps.pop(step.id, None)
        watchers = list(self._watchers)
    for watcher in watchers:
        watcher.closed(step)

live() #

Every open step, oldest first.

Source code in capturegraph-lib/capturegraph/recipes/progress/registry.py
def live(self) -> list[Step]:
    """Every open step, oldest first."""
    with self._lock:
        return sorted(self._steps.values(), key=lambda step: step.started_at)

open(step) #

Register step and tell the watchers.

Source code in capturegraph-lib/capturegraph/recipes/progress/registry.py
def open(self, step: Step) -> None:
    """Register ``step`` and tell the watchers."""
    with self._lock:
        self._steps[step.id] = step
        watchers = list(self._watchers)
    for watcher in watchers:
        watcher.opened(step)

unwatch(watcher) #

Stop telling watcher.

Source code in capturegraph-lib/capturegraph/recipes/progress/registry.py
def unwatch(self, watcher: Watcher) -> None:
    """Stop telling ``watcher``."""
    with self._lock:
        self._watchers.remove(watcher)

watch(watcher) #

Tell watcher about every step from now on.

Source code in capturegraph-lib/capturegraph/recipes/progress/registry.py
def watch(self, watcher: Watcher) -> None:
    """Tell ``watcher`` about every step from now on."""
    with self._lock:
        self._watchers.append(watcher)

Watcher #

Bases: Protocol

Told about every step of the process as it opens and closes.

Source code in capturegraph-lib/capturegraph/recipes/progress/registry.py
class Watcher(Protocol):
    """Told about every step of the process as it opens and closes."""

    def opened(self, step: Step) -> None:
        """``step`` has just opened."""
        ...

    def closed(self, step: Step) -> None:
        """``step`` has just closed."""
        ...

closed(step) #

step has just closed.

Source code in capturegraph-lib/capturegraph/recipes/progress/registry.py
def closed(self, step: Step) -> None:
    """``step`` has just closed."""
    ...

opened(step) #

step has just opened.

Source code in capturegraph-lib/capturegraph/recipes/progress/registry.py
def opened(self, step: Step) -> None:
    """``step`` has just opened."""
    ...