From f278b225208d897f49436a5d395767111d4c1f5b Mon Sep 17 00:00:00 2001 From: German Date: Thu, 12 Jan 2023 13:57:52 +0000 Subject: [PATCH 1/5] Standarize the file name and location functions/approaches. --- src/ansys/mapdl/core/mapdl.py | 42 ++++++++++------- src/ansys/mapdl/core/mapdl_grpc.py | 72 +++++++++++++++++------------- tests/test_mapdl.py | 25 ++++++++++- 3 files changed, 91 insertions(+), 48 deletions(-) diff --git a/src/ansys/mapdl/core/mapdl.py b/src/ansys/mapdl/core/mapdl.py index 4ee5a293493..8ea78a336e9 100644 --- a/src/ansys/mapdl/core/mapdl.py +++ b/src/ansys/mapdl/core/mapdl.py @@ -3904,25 +3904,14 @@ def file(self, fname="", ext="", **kwargs): >>> mapdl.file('/tmp/file.rst') """ - # MAPDL always adds the "rst" extension onto the name, even if already - # has one, so here we simply reconstruct it. - filename = pathlib.Path(fname + ext) + fname = self._get_file_name(fname, ext, "rst") + fname = self._get_file_path(fname, kwargs["progress_bar"]) + file_, ext_ = self._decompose_fname(fname) + return self._file(file_, ext_, **kwargs) - if not filename.exists(): - # potential that the user is relying on the default "rst" - filename_rst = filename.parent / (filename.name + ".rst") - if not filename_rst.exists(): - raise FileNotFoundError(f"Unable to locate {filename}") - - return self._file(filename, **kwargs) - - def _file(self, filename, **kwargs): + def _file(self, filename, extension, **kwargs): """Run the MAPDL ``file`` command with a proper filename.""" - filename = pathlib.Path(filename) - ext = filename.suffix - fname = str(filename).replace(ext, "") - ext = ext.replace(".", "") - return self.run(f"FILE,{fname},{ext}", **kwargs) + return self.run(f"FILE,{filename},{extension}", **kwargs) @wraps(Commands.lsread) def use(self, *args, **kwargs): @@ -4049,3 +4038,22 @@ def is_local(self): def launched(self): """Check if the MAPDL instance has been launched by PyMAPDL.""" return self._launched + + def _decompose_fname(self, fname): + """Decompose a file name (with or without path) into filename and extension. + + Parameters + ---------- + fname : str + File name with or without path. + + Returns + ------- + str + File name (without extension or path) + + str + File extension + """ + fname = pathlib.Path(fname) + return fname.stem, fname.suffix diff --git a/src/ansys/mapdl/core/mapdl_grpc.py b/src/ansys/mapdl/core/mapdl_grpc.py index 3edd80790d5..38dd0504d30 100755 --- a/src/ansys/mapdl/core/mapdl_grpc.py +++ b/src/ansys/mapdl/core/mapdl_grpc.py @@ -5,6 +5,7 @@ import glob import io import os +import pathlib import re import shutil import subprocess @@ -1390,23 +1391,15 @@ def cdread(self, option="", fname="", ext="", fnamei="", exti="", **kwargs): "Input the geometry and mesh files separately " r'with "\INPUT" or ``mapdl.input``' ) - # the old behaviour is to supplied the name and the extension separately. - # to make it easier let's going to allow names with extensions - basename = os.path.basename(fname) - if len(basename.split(".")) == 1: - # there is no extension in the main name. - if ext: - # if extension is an input as an option (old APDL style) - fname = fname + "." + ext - else: - # Using default .db - fname = fname + "." + "cdb" kwargs.setdefault("verbose", False) kwargs.setdefault("progress_bar", False) kwargs.setdefault("orig_cmd", "CDREAD") kwargs.setdefault("cd_read_option", option.upper()) + fname = self._get_file_name(fname, ext, "cdb") + fname = self._get_file_path(fname, kwargs["progress_bar"]) + self.input(fname, **kwargs) @wraps(_MapdlCore.tbft) @@ -1626,9 +1619,11 @@ def _get_file_path(self, fname, progress_bar=False): f"`fname` should be a full file path or name, not the directory '{fname}'." ) + fPath = pathlib.Path(fname) + fpath = os.path.dirname(fname) - fname = os.path.basename(fname) - fext = fname.split(".")[-1] + fname = fPath.name + fext = fPath.suffix # if there is no dirname, we are assuming the file is # in the python working directory. @@ -1666,6 +1661,34 @@ def _get_file_path(self, fname, progress_bar=False): return filename + def _get_file_name(self, fname, ext=None, default_extension=None): + """Get file name from fname and extension arguments. + + fname can be the full path. + + Parameters + ---------- + fname : str + File name (with our with extension). It can be a full path. + ext : str, optional + File extension, by default None + """ + + # the old behaviour is to supplied the name and the extension separately. + # to make it easier let's going to allow names with extensions + + if ext: + fname = fname + "." + ext + else: + basename = os.path.basename(fname) + + if len(basename.split(".")) == 1: + # there is no extension in the main name. + if default_extension: + fname = fname + "." + default_extension + + return fname + def _flush_stored(self): """Writes stored commands to an input file and runs the input file. Used with non_interactive. @@ -2695,24 +2718,13 @@ def wrinqr(self, key, **kwargs): @wraps(_MapdlCore.file) def file(self, fname="", ext="", **kwargs): """Wrap ``_MapdlCore.file`` to take advantage of the gRPC methods.""" - filename = fname + ext - if self._local: # pragma: no cover - out = super().file(fname, ext, **kwargs) - elif filename in self.list_files(): - # this file is already remote - out = self._file(filename) - else: - if not os.path.isfile(filename): - raise FileNotFoundError( - f"Unable to find '{filename}'. You may need to " - "input the full path to the file." - ) - - progress_bar = kwargs.pop("progress_bar", False) - basename = self.upload(filename, progress_bar=progress_bar) - out = self._file(basename, **kwargs) + # always check if file is present as the grpc and MAPDL errors + # are unclear + fname = self._get_file_name(fname, ext, "cdb") + fname = self._get_file_path(fname, kwargs["progress_bar"]) + file_, ext_ = self._decompose_fname(fname) - return out + return self._file(file_, ext_, **kwargs) @wraps(_MapdlCore.vget) def vget(self, par="", ir="", tstrt="", kcplx="", **kwargs): diff --git a/tests/test_mapdl.py b/tests/test_mapdl.py index 11cc4203d9c..f552fdbc2a0 100644 --- a/tests/test_mapdl.py +++ b/tests/test_mapdl.py @@ -1237,7 +1237,19 @@ def test_get_file_path(mapdl, tmpdir): fobject = tmpdir.join(fname) fobject.write("Dummy file for testing") - assert fname in mapdl._get_file_path(fobject) + assert fobject not in mapdl.list_files() + assert fobject not in os.listdir() + + mapdl._local = True + fname_ = mapdl._get_file_path(fobject) + assert fname in fname_ + assert fobject not in mapdl.list_files() + assert os.path.exists(fname_) + + mapdl._local = False + fname_ = mapdl._get_file_path(fobject) + # If we are not in local, now it should have been uploaded + assert fname in mapdl.list_files() @pytest.mark.parametrize( @@ -1868,3 +1880,14 @@ def test_avoid_non_interactive(mapdl): assert any(["comment A" in cmd for cmd in stored_commands]) assert all(["comment B" not in cmd for cmd in stored_commands]) assert any(["comment C" in cmd for cmd in stored_commands]) + + +def test_get_file_name(mapdl): + file_ = "asdf/qwert/zxcv.asd" + assert mapdl._get_file_name(file_) == file_ + assert mapdl._get_file_name(file_, "asdf") == file_ + ".asdf" + assert mapdl._get_file_name(file_, default_extension="qwer") == file_ + assert ( + mapdl._get_file_name(file_.replace(".asd", ""), default_extension="qwer") + == file_.replace(".asd", "") + ".qwer" + ) From 391630acbf521f96b27d430552bcf0d95f6267c3 Mon Sep 17 00:00:00 2001 From: German Date: Thu, 12 Jan 2023 15:15:31 +0000 Subject: [PATCH 2/5] Adding the default to 'progress_bar' --- src/ansys/mapdl/core/mapdl.py | 2 +- src/ansys/mapdl/core/mapdl_grpc.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ansys/mapdl/core/mapdl.py b/src/ansys/mapdl/core/mapdl.py index 8ea78a336e9..376f2919a89 100644 --- a/src/ansys/mapdl/core/mapdl.py +++ b/src/ansys/mapdl/core/mapdl.py @@ -3905,7 +3905,7 @@ def file(self, fname="", ext="", **kwargs): """ fname = self._get_file_name(fname, ext, "rst") - fname = self._get_file_path(fname, kwargs["progress_bar"]) + fname = self._get_file_path(fname, kwargs.get("progress_bar", False)) file_, ext_ = self._decompose_fname(fname) return self._file(file_, ext_, **kwargs) diff --git a/src/ansys/mapdl/core/mapdl_grpc.py b/src/ansys/mapdl/core/mapdl_grpc.py index 38dd0504d30..aa4c4a5a859 100755 --- a/src/ansys/mapdl/core/mapdl_grpc.py +++ b/src/ansys/mapdl/core/mapdl_grpc.py @@ -2721,7 +2721,7 @@ def file(self, fname="", ext="", **kwargs): # always check if file is present as the grpc and MAPDL errors # are unclear fname = self._get_file_name(fname, ext, "cdb") - fname = self._get_file_path(fname, kwargs["progress_bar"]) + fname = self._get_file_path(fname, kwargs.get("progress_bar", False)) file_, ext_ = self._decompose_fname(fname) return self._file(file_, ext_, **kwargs) From 3493760845cf3168046d0f7a1df7c4aca8754c36 Mon Sep 17 00:00:00 2001 From: German Date: Thu, 12 Jan 2023 17:04:42 +0100 Subject: [PATCH 3/5] small math fix --- tests/test_math.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_math.py b/tests/test_math.py index a80b6d3d02c..dccf59fdf81 100755 --- a/tests/test_math.py +++ b/tests/test_math.py @@ -50,8 +50,8 @@ def cube_with_damping(mapdl, cleared): mapdl.alphad(10) mapdl.solve() mapdl.save() - mapdl.aux2() if mapdl._distributed: + mapdl.aux2() mapdl.combine("full") mapdl.slashsolu() From 161bd09c96219a1a3566580432ef7a43ff0278c6 Mon Sep 17 00:00:00 2001 From: German Date: Tue, 17 Jan 2023 17:33:03 +0000 Subject: [PATCH 4/5] Fix tests --- src/ansys/mapdl/core/mapdl.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ansys/mapdl/core/mapdl.py b/src/ansys/mapdl/core/mapdl.py index 376f2919a89..d2e9a4bd8a4 100644 --- a/src/ansys/mapdl/core/mapdl.py +++ b/src/ansys/mapdl/core/mapdl.py @@ -4053,7 +4053,7 @@ def _decompose_fname(self, fname): File name (without extension or path) str - File extension + File extension (without dot) """ fname = pathlib.Path(fname) - return fname.stem, fname.suffix + return fname.stem, fname.suffix.replace(".", "") From a77fcc011b89ee98d51e41041e0c57b6791cbc14 Mon Sep 17 00:00:00 2001 From: German Date: Wed, 18 Jan 2023 11:49:06 +0000 Subject: [PATCH 5/5] Fix tests --- tests/test_math.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_math.py b/tests/test_math.py index dccf59fdf81..37e99daa354 100755 --- a/tests/test_math.py +++ b/tests/test_math.py @@ -53,7 +53,7 @@ def cube_with_damping(mapdl, cleared): if mapdl._distributed: mapdl.aux2() mapdl.combine("full") - mapdl.slashsolu() + mapdl.slashsolu() def test_ones(mm):