| 1 |
"""Cross-python stdlib shims.""" |
| 2 |
|
| 3 |
import os |
| 4 |
import sys |
| 5 |
from contextlib import contextmanager |
| 6 |
from pathlib import Path |
| 7 |
from typing import Iterator |
| 8 |
|
| 9 |
if sys.version_info >= (3, 11): |
| 10 |
from contextlib import chdir as chdir_cm |
| 11 |
from tomllib import loads as load_toml_from_string |
| 12 |
else: |
| 13 |
from tomli import loads as load_toml_from_string |
| 14 |
|
| 15 |
@contextmanager # type: ignore[no-redef] |
| 16 |
def chdir_cm(path: "os.PathLike[str]") -> Iterator[None]: |
| 17 |
"""Temporarily change the current directory, recovering on exit.""" |
| 18 |
original_wd = Path.cwd() |
| 19 |
os.chdir(path) |
| 20 |
try: |
| 21 |
yield |
| 22 |
finally: |
| 23 |
os.chdir(original_wd) |
| 24 |
|
| 25 |
|
| 26 |
__all__ = ("chdir_cm", "load_toml_from_string") # noqa: WPS410 |