Skip to content

Commit

Permalink
refactor(MFFileMgmt): simplify strip_model_relative_path
Browse files Browse the repository at this point in the history
  • Loading branch information
wpbonelli committed Mar 16, 2023
1 parent 0513c6b commit a6d20ac
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 31 deletions.
1 change: 0 additions & 1 deletion .github/workflows/commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ on:
- master
- develop
- ci-diagnose*
- test*
pull_request:
branches:
- master
Expand Down
42 changes: 30 additions & 12 deletions autotest/test_mf6.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import platform
from os.path import join
from pathlib import Path
from shutil import copytree, which
Expand Down Expand Up @@ -278,18 +279,24 @@ def to_os_sep(s):
return s.replace("\\", os.sep).replace("/", os.sep)


def test_load_namefile_with_filenames(function_tmpdir, example_data_path):
def test_load_and_run_sim_when_namefile_uses_filenames(
function_tmpdir, example_data_path
):
ws = function_tmpdir / "ws"
ml_name = "freyberg"
nam_name = "mfsim.nam"
nam_path = ws / nam_name
copytree(example_data_path / f"mf6-{ml_name}", ws)

sim = MFSimulation.load(
nam_name, sim_ws=example_data_path / f"mf6-{ml_name}"
)
sim = MFSimulation.load(nam_name, sim_ws=ws)
sim.check()
success, buff = sim.run_simulation(report=True)
assert success


@pytest.mark.skip(reason="abs paths not supported yet")
def test_load_namefile_with_abs_paths(function_tmpdir, example_data_path):
def test_load_and_run_sim_when_namefile_uses_abs_paths(
function_tmpdir, example_data_path
):
ws = function_tmpdir / "ws"
ml_name = "freyberg"
nam_name = "mfsim.nam"
Expand All @@ -305,14 +312,18 @@ def test_load_namefile_with_abs_paths(function_tmpdir, example_data_path):
l = l.replace(
pattern, str(ws.absolute()) + os.sep + pattern
)
f.write(l + os.linesep)
f.write(l)

sim = MFSimulation.load(nam_name, sim_ws=ws)
sim.check()
success, buff = sim.run_simulation(report=True)
assert success


@pytest.mark.parametrize("sep", ["msdos", "posix"])
def test_load_namefile_with_rel_paths(function_tmpdir, example_data_path, sep):
@pytest.mark.parametrize("sep", ["win", "posix"])
def test_load_sim_when_namefile_uses_rel_paths(
function_tmpdir, example_data_path, sep
):
ws = function_tmpdir / "ws"
ml_name = "freyberg"
nam_name = "mfsim.nam"
Expand All @@ -325,23 +336,30 @@ def test_load_namefile_with_rel_paths(function_tmpdir, example_data_path, sep):
for l in lines:
pattern = f"{ml_name}."
if pattern in l:
if sep == "msdos":
if sep == "win":
l = to_msdos_sep(
l.replace(
pattern, "../" + ws.name + "/" + ml_name + "."
)
)
else:
l = to_msdos_sep(
l = to_posix_sep(
l.replace(
pattern, "../" + ws.name + "/" + ml_name + "."
)
)
f.write(l + os.linesep)
f.write(l)

sim = MFSimulation.load(nam_name, sim_ws=ws)
sim.check()

# only run the simulation if separator matches the current OS
# (modflow6 doesn't handle inappropriate seps like FloPy can)
is_win = platform.system() == "Windows"
if (sep == "win" and is_win) or (sep == "posix" and not is_win):
success, buff = sim.run_simulation(report=True)
assert success


def test_subdir(function_tmpdir):
sim = MFSimulation(sim_ws=function_tmpdir)
Expand Down
35 changes: 17 additions & 18 deletions flopy/mf6/mfbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,26 +273,25 @@ def _build_relative_path(self, model_name):
current_abs_path = self.resolve_path("", model_name, False)
return os.path.relpath(old_abs_path, current_abs_path)

def strip_model_relative_path(self, model_name, path):
def strip_model_relative_path(self, model_name, path) -> str:
"""Strip out the model relative path part of `path`. For internal
FloPy use, not intended for end user."""
new_path = path
if model_name in self.model_relative_path:
model_rel_path = self.model_relative_path[model_name]
if (
model_rel_path is not None
and len(model_rel_path) > 0
and model_rel_path != "."
):
model_rel_path_lst = model_rel_path.split(os.path.sep)
path_lst = path.split(os.path.sep)
new_path = ""
for i, mrp in enumerate(model_rel_path_lst):
if i >= len(path_lst) or mrp != path_lst[i]:
new_path = os.path.join(new_path, path_lst[i])
for rp in path_lst[len(model_rel_path_lst) :]:
new_path = os.path.join(new_path, rp)
return new_path
if model_name not in self.model_relative_path:
return path

model_rel_path = Path(self.model_relative_path[model_name])
if (
model_rel_path is None
or model_rel_path.is_absolute()
or not any(str(model_rel_path))
or str(model_rel_path) == os.curdir
):
return path

try:
return str(Path(path).relative_to(model_rel_path))
except ValueError:
return str(Path(model_rel_path) / path)

@staticmethod
def unique_file_name(file_name, lookup):
Expand Down

0 comments on commit a6d20ac

Please sign in to comment.