Server IP : 66.29.132.122 / Your IP : 3.144.254.18 Web Server : LiteSpeed System : Linux business142.web-hosting.com 4.18.0-553.lve.el8.x86_64 #1 SMP Mon May 27 15:27:34 UTC 2024 x86_64 User : admazpex ( 531) PHP Version : 7.2.34 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : OFF | Pkexec : OFF Directory : /opt/hc_python/lib/python3.8/site-packages/pre_commit/ |
Upload File : |
from __future__ import annotations import contextlib import enum import os from typing import Generator from typing import MutableMapping from typing import NamedTuple from typing import Tuple 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, None, 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)