-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(config): corrected handling of relative items in python path and …
…other variables if current folder is different then root folder of the project fixes #359
- Loading branch information
Showing
8 changed files
with
260 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import os | ||
from contextlib import AbstractContextManager | ||
from pathlib import Path | ||
from typing import Any, Callable, List, Literal, Optional, Union | ||
|
||
from .path import same_file | ||
|
||
|
||
class ChDir(AbstractContextManager[Any]): | ||
def __init__( | ||
self, path: "Union[str, os.PathLike[str], None]", verbose_callback: Optional[Callable[[str], None]] = None | ||
) -> None: | ||
self.path = path | ||
self._old_cwd: List[Optional[Path]] = [] | ||
self._verbose_callback = verbose_callback | ||
|
||
def __enter__(self) -> Optional[Path]: | ||
result = Path.cwd() | ||
|
||
if self.path is None or (self._old_cwd and same_file(self.path, Path.cwd())): | ||
self._old_cwd.append(None) | ||
else: | ||
self._old_cwd.append(result) | ||
|
||
if self.path: | ||
if self._verbose_callback: | ||
self._verbose_callback(f"Changing directory to {self.path}") | ||
|
||
os.chdir(self.path) | ||
|
||
return result | ||
|
||
def __exit__(self, _exc_type: Any, _exc_value: Any, _traceback: Any) -> Literal[False]: | ||
old_path = self._old_cwd.pop() | ||
if old_path is not None: | ||
if self._verbose_callback: | ||
self._verbose_callback(f"Changing directory back to {old_path}") | ||
|
||
os.chdir(old_path) | ||
|
||
return False | ||
|
||
|
||
chdir = ChDir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.