snakeoil.contexts module

Various with-statement context utilities.

class snakeoil.contexts.GitStash(path: str | PathLike[str], pathspecs: Iterable[str] = (), staged=False)[source]

Bases: AbstractContextManager

Context manager for stashing untracked or modified/uncommitted files.

snakeoil.contexts.chdir(path: str) chdir[source]
snakeoil.contexts.os_environ(*remove: str, **update: str) Iterator[None][source]

Mangle the os.environ dictionary and revert on exit.

This is explicitly not thread safe. It however takes steps to mutate os.environ rather than replacing the instance, so any references held to os.environ will be updated.

Parameters:
  • remove – variables to remove

  • update – variable -> value mapping to add or alter

snakeoil.contexts.patch(target, new)[source]

Simplified module monkey patching via context manager.

Parameters:
  • target – Target class or object.

  • new – Object or value to replace the target with.

snakeoil.contexts.syspath(path: str, condition: bool = True, position: int = 0)[source]

Context manager that mangles sys.path and then reverts on exit.

Parameters:
  • path – The directory path to add to sys.path.

  • condition – Optional boolean that decides whether sys.path is mangled or not, defaults to being enabled.

  • position – Optional integer that is the place where the path is inserted in sys.path, defaults to prepending.