Total Complexity | 1 |
Total Lines | 32 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | # Copyright Pincer 2021-Present |
||
|
|||
2 | # Full MIT License can be found in `LICENSE` at the project root. |
||
3 | |||
4 | from __future__ import annotations |
||
5 | |||
6 | from contextlib import contextmanager |
||
7 | from os import getcwd, chdir as os_chdir |
||
8 | from typing import TYPE_CHECKING |
||
9 | |||
10 | if TYPE_CHECKING: |
||
11 | from typing import Generator, Any |
||
12 | |||
13 | |||
14 | @contextmanager |
||
15 | def chdir(path: str) -> Generator[str, Any, None]: |
||
16 | """Change the cwd to the given path as a context manager. |
||
17 | |||
18 | Parameters |
||
19 | ---------- |
||
20 | path: |
||
21 | Path to change to. |
||
22 | |||
23 | Yields |
||
24 | ------ |
||
25 | Generator[:class:`str`, Any, None] |
||
26 | """ |
||
27 | current_path = getcwd() |
||
28 | os_chdir(path) |
||
29 | |||
30 | yield current_path |
||
31 | os_chdir(current_path) |
||
32 |