Skip to content

Commit

Permalink
Default cases to the current script's directory
Browse files Browse the repository at this point in the history
  • Loading branch information
gerlero committed Oct 6, 2024
1 parent 6a0a1d7 commit 82b7788
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 16 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
```
Expand Down
2 changes: 1 addition & 1 deletion foamlib/_cases/_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
10 changes: 8 additions & 2 deletions foamlib/_cases/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]):
"""
Expand Down
4 changes: 2 additions & 2 deletions foamlib/_cases/_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions foamlib/_cases/_sync.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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):
Expand All @@ -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],
Expand Down
4 changes: 2 additions & 2 deletions tests/test_cases/test_cavity.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ 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)

clean = clone.path / "clean"
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)

Expand Down
4 changes: 2 additions & 2 deletions tests/test_cases/test_cavity_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ 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)

clean = clone.path / "clean"
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)

Expand Down

0 comments on commit 82b7788

Please sign in to comment.