Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Default cases to the current script's directory" #224

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,12 @@ result = differential_evolution(cost, bounds=[(-1, 1)], workers=AsyncFoamCase.ma

```python
#!/usr/bin/env python3
from pathlib import Path
from foamlib import FoamCase

this_case = FoamCase()
case = FoamCase(Path(__file__).parent)
# Any additional configuration here
this_case.run()
case.run()
```

## Documentation
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. If None, use the current script's directory (if applicable), or the current working directory.
:param path: The path to the case directory.
"""

class TimeDirectory(FoamCaseRunBase.TimeDirectory):
Expand Down
12 changes: 3 additions & 9 deletions foamlib/_cases/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@


class FoamCaseBase(Sequence["FoamCaseBase.TimeDirectory"]):
def __init__(self, path: Union["os.PathLike[str]", str] = Path()):
self.path = Path(path).absolute()

class TimeDirectory(Set[FoamFieldFile]):
"""
An OpenFOAM time directory in a case.
Expand Down Expand Up @@ -92,15 +95,6 @@ def __repr__(self) -> str:
def __str__(self) -> str:
return str(self.path)

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.cwd()
else:
self.path = Path(path).absolute()

@property
def _times(self) -> Sequence["FoamCaseBase.TimeDirectory"]:
times = []
Expand Down
2 changes: 1 addition & 1 deletion foamlib/_cases/_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,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. If None, use the current script's directory (if applicable), or the current working directory.
:param path: The path to the case directory.
"""

class TimeDirectory(FoamCaseRunBase.TimeDirectory):
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 foamlib import FoamCase\nFoamCase().run(parallel=False)"
"#!/usr/bin/env python3\nfrom pathlib import Path\nfrom foamlib import FoamCase\nFoamCase(Path(__file__).parent).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 foamlib import FoamCase\nFoamCase().clean()"
"#!/usr/bin/env python3\nfrom pathlib import Path\nfrom foamlib import FoamCase\nFoamCase(Path(__file__).parent).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 foamlib import FoamCase\nFoamCase().run(parallel=False)"
"#!/usr/bin/env python3\nfrom pathlib import Path\nfrom foamlib import FoamCase\nFoamCase(Path(__file__).parent).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 foamlib import FoamCase\nFoamCase().clean()"
"#!/usr/bin/env python3\nfrom pathlib import Path\nfrom foamlib import FoamCase\nFoamCase(Path(__file__).parent).clean()"
)
clean.chmod(clean.stat().st_mode | stat.S_IEXEC)

Expand Down
Loading