diff --git a/src/ansys/mapdl/core/mapdl.py b/src/ansys/mapdl/core/mapdl.py index c0e041dcb49..e345658e2b1 100644 --- a/src/ansys/mapdl/core/mapdl.py +++ b/src/ansys/mapdl/core/mapdl.py @@ -3914,25 +3914,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.get("progress_bar", False)) + 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): @@ -4059,3 +4048,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 (without dot) + """ + fname = pathlib.Path(fname) + return fname.stem, fname.suffix.replace(".", "") diff --git a/src/ansys/mapdl/core/mapdl_grpc.py b/src/ansys/mapdl/core/mapdl_grpc.py index bcecdea9768..6fa5841dee7 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 @@ -1387,23 +1388,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) @@ -1623,9 +1616,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. @@ -1663,6 +1658,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. @@ -2692,24 +2715,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.get("progress_bar", False)) + 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" + ) diff --git a/tests/test_math.py b/tests/test_math.py index a80b6d3d02c..37e99daa354 100755 --- a/tests/test_math.py +++ b/tests/test_math.py @@ -50,10 +50,10 @@ 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() + mapdl.slashsolu() def test_ones(mm):