PHP 8.2.30
Preview: envcontext.py Size: 1.56 KB
/opt/hc_python/lib64/python3.12/site-packages/pre_commit/envcontext.py

from __future__ import annotations

import contextlib
import enum
import os
from collections.abc import Generator
from collections.abc import MutableMapping
from typing import NamedTuple
from typing import Union

_Unset = enum.Enum('_Unset', 'UNSET')
UNSET = _Unset.UNSET


class Var(NamedTuple):
    name: str
    default: str = ''


SubstitutionT = tuple[Union[str, Var], ...]
ValueT = Union[str, _Unset, SubstitutionT]
PatchesT = tuple[tuple[str, ValueT], ...]


def format_env(parts: SubstitutionT, env: MutableMapping[str, str]) -> str:
    return ''.join(
        env.get(part.name, part.default) if isinstance(part, Var) else part
        for part in parts
    )


@contextlib.contextmanager
def envcontext(
        patch: PatchesT,
        _env: MutableMapping[str, str] | None = None,
) -> Generator[None]:
    """In this context, `os.environ` is modified according to `patch`.

    `patch` is an iterable of 2-tuples (key, value):
        `key`: string
        `value`:
            - string: `environ[key] == value` inside the context.
            - UNSET: `key not in environ` inside the context.
            - template: A template is a tuple of strings and Var which will be
              replaced with the previous environment
    """
    env = os.environ if _env is None else _env
    before = dict(env)

    for k, v in patch:
        if v is UNSET:
            env.pop(k, None)
        elif isinstance(v, tuple):
            env[k] = format_env(v, before)
        else:
            env[k] = v

    try:
        yield
    finally:
        env.clear()
        env.update(before)

Directory Contents

Dirs: 5 × Files: 25

Name Size Perms Modified Actions
commands DIR
- drwxr-xr-x 2025-12-03 08:02:54
Edit Download
languages DIR
- drwxr-xr-x 2025-12-03 08:02:54
Edit Download
- drwxr-xr-x 2025-12-03 08:02:54
Edit Download
resources DIR
- drwxr-xr-x 2025-12-03 08:02:54
Edit Download
- drwxr-xr-x 2025-12-03 08:02:54
Edit Download
1.43 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
16.42 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
3.14 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
282 B lrw-r--r-- 2025-12-03 08:02:54
Edit Download
1.56 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
78 B lrw-r--r-- 2025-12-03 08:02:54
Edit Download
2.56 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
2.30 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
8.33 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
1.48 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
5.26 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
1019 B lrw-r--r-- 2025-12-03 08:02:54
Edit Download
15.54 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
911 B lrw-r--r-- 2025-12-03 08:02:54
Edit Download
2.42 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
495 B lrw-r--r-- 2025-12-03 08:02:54
Edit Download
7.43 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
4.06 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
8.27 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
6.88 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
5.42 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
561 B lrw-r--r-- 2025-12-03 08:02:54
Edit Download
1.31 KB lrw-r--r-- 2025-12-03 08:02:54
Edit Download
0 B lrw-r--r-- 2025-12-03 08:02:54
Edit Download
127 B lrw-r--r-- 2025-12-03 08:02:54
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).