From 82b778881b42cf247ffe9dfc7c77b03271af988a Mon Sep 17 00:00:00 2001 From: Gabriel Gerlero Date: Sun, 6 Oct 2024 00:59:29 -0300 Subject: [PATCH] Default cases to the current script's directory --- README.md | 3 +-- foamlib/_cases/_async.py | 2 +- foamlib/_cases/_base.py | 10 ++++++++-- foamlib/_cases/_run.py | 4 ++-- foamlib/_cases/_sync.py | 6 +----- tests/test_cases/test_cavity.py | 4 ++-- tests/test_cases/test_cavity_async.py | 4 ++-- 7 files changed, 17 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index c1dcb97..069f7ef 100644 --- a/README.md +++ b/README.md @@ -126,10 +126,9 @@ result = differential_evolution(cost, bounds=[(-1, 1)], workers=AsyncFoamCase.ma ```python #!/usr/bin/env python3 -from pathlib import Path from foamlib import FoamCase -case = FoamCase(Path(__file__).parent) +case = FoamCase() # Any additional configuration here case.run() ``` diff --git a/foamlib/_cases/_async.py b/foamlib/_cases/_async.py index 9a359a9..7677877 100644 --- a/foamlib/_cases/_async.py +++ b/foamlib/_cases/_async.py @@ -41,7 +41,7 @@ class AsyncFoamCase(FoamCaseRunBase): Access the time directories of the case as a sequence, e.g. `case[0]` or `case[-1]`. - :param path: The path to the case directory. + :param path: The path to the case directory. If None, use the current script's directory (if available) or the current working directory. """ class TimeDirectory(FoamCaseRunBase.TimeDirectory): diff --git a/foamlib/_cases/_base.py b/foamlib/_cases/_base.py index bec3307..d3bb58c 100644 --- a/foamlib/_cases/_base.py +++ b/foamlib/_cases/_base.py @@ -25,8 +25,14 @@ class FoamCaseBase(Sequence["FoamCaseBase.TimeDirectory"]): - def __init__(self, path: Union["os.PathLike[str]", str] = Path()): - self.path = Path(path).absolute() + def __init__(self, path: Optional[Union["os.PathLike[str]", str]] = None): + if path is None: + if sys.argv[0]: + self.path = Path(sys.argv[0]).absolute().parent + else: + self.path = Path().absolute() + else: + self.path = Path(path).absolute() class TimeDirectory(Set[FoamFieldFile]): """ diff --git a/foamlib/_cases/_run.py b/foamlib/_cases/_run.py index 30ec8ac..2750875 100644 --- a/foamlib/_cases/_run.py +++ b/foamlib/_cases/_run.py @@ -203,7 +203,7 @@ def __clean_script(self) -> Optional[Path]: else: return None - if sys.argv and Path(sys.argv[0]).absolute() == script.absolute(): + if sys.argv[0] and Path(sys.argv[0]).absolute() == script.absolute(): return None return script @@ -236,7 +236,7 @@ def __run_script(self, *, parallel: Optional[bool]) -> Optional[Path]: else: return None - if sys.argv and Path(sys.argv[0]).absolute() == script.absolute(): + if sys.argv[0] and Path(sys.argv[0]).absolute() == script.absolute(): return None return script diff --git a/foamlib/_cases/_sync.py b/foamlib/_cases/_sync.py index 0903eeb..8dcb84b 100644 --- a/foamlib/_cases/_sync.py +++ b/foamlib/_cases/_sync.py @@ -1,7 +1,6 @@ import os import shutil import sys -from pathlib import Path from types import TracebackType from typing import Any, Callable, Optional, Type, Union, overload @@ -30,7 +29,7 @@ class FoamCase(FoamCaseRunBase): Access the time directories of the case as a sequence, e.g. `case[0]` or `case[-1]`. - :param path: The path to the case directory. + :param path: The path to the case directory. If None, use the current script's directory (if available) or the current working directory. """ class TimeDirectory(FoamCaseRunBase.TimeDirectory): @@ -49,9 +48,6 @@ def cell_centers(self) -> FoamFieldFile: return calls.value - def __init__(self, path: Union["os.PathLike[str]", str] = Path()): - super().__init__(path) - @staticmethod def _run( cmd: Union[Sequence[Union[str, "os.PathLike[str]"]], str], diff --git a/tests/test_cases/test_cavity.py b/tests/test_cases/test_cavity.py index ec62ecf..e007a3a 100644 --- a/tests/test_cases/test_cavity.py +++ b/tests/test_cases/test_cavity.py @@ -27,7 +27,7 @@ def cavity(request: pytest.FixtureRequest) -> "Generator[FoamCase]": assert not run.exists() assert not (clone.path / "Allrun").exists() run.write_text( - "#!/usr/bin/env python3\nfrom pathlib import Path\nfrom foamlib import FoamCase\nFoamCase(Path(__file__).parent).run(parallel=False)" + "#!/usr/bin/env python3\nfrom foamlib import FoamCase\nFoamCase().run(parallel=False)" ) run.chmod(run.stat().st_mode | stat.S_IEXEC) @@ -35,7 +35,7 @@ def cavity(request: pytest.FixtureRequest) -> "Generator[FoamCase]": assert not clean.exists() assert not (clone.path / "Allclean").exists() clean.write_text( - "#!/usr/bin/env python3\nfrom pathlib import Path\nfrom foamlib import FoamCase\nFoamCase(Path(__file__).parent).clean()" + "#!/usr/bin/env python3\nfrom foamlib import FoamCase\nFoamCase().clean()" ) clean.chmod(clean.stat().st_mode | stat.S_IEXEC) diff --git a/tests/test_cases/test_cavity_async.py b/tests/test_cases/test_cavity_async.py index 151d74f..d5a1f31 100644 --- a/tests/test_cases/test_cavity_async.py +++ b/tests/test_cases/test_cavity_async.py @@ -28,7 +28,7 @@ async def cavity(request: pytest.FixtureRequest) -> "AsyncGenerator[AsyncFoamCas assert not run.exists() assert not (clone.path / "Allrun").exists() run.write_text( - "#!/usr/bin/env python3\nfrom pathlib import Path\nfrom foamlib import FoamCase\nFoamCase(Path(__file__).parent).run(parallel=False)" + "#!/usr/bin/env python3\nfrom foamlib import FoamCase\nFoamCase().run(parallel=False)" ) run.chmod(run.stat().st_mode | stat.S_IEXEC) @@ -36,7 +36,7 @@ async def cavity(request: pytest.FixtureRequest) -> "AsyncGenerator[AsyncFoamCas assert not clean.exists() assert not (clone.path / "Allclean").exists() clean.write_text( - "#!/usr/bin/env python3\nfrom pathlib import Path\nfrom foamlib import FoamCase\nFoamCase(Path(__file__).parent).clean()" + "#!/usr/bin/env python3\nfrom foamlib import FoamCase\nFoamCase().clean()" ) clean.chmod(clean.stat().st_mode | stat.S_IEXEC)