From 651bec52a27abfdd1e704e1f8d2bef16f6797cc1 Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Tue, 28 Dec 2021 14:49:48 +0100 Subject: [PATCH] Detaching file location input from Mapdl.input (#783) * Added `_get_file_path` function. * Added test function. * Variable name fixed. * Apply suggestions from code review Co-authored-by: Alex Kaszynski * Fixing input. * Fixing test unit. * Fixed unit test. * Fixing the _get_file_path function. * Fixing the _get_file_path function. * Improving docstring * Improving docstring * Fixed unit test. * Fixed _get_path... * Fix ... again. Co-authored-by: Alex Kaszynski --- ansys/mapdl/core/mapdl_grpc.py | 80 ++++++++++++++++++++-------------- tests/test_mapdl.py | 8 ++++ 2 files changed, 56 insertions(+), 32 deletions(-) diff --git a/ansys/mapdl/core/mapdl_grpc.py b/ansys/mapdl/core/mapdl_grpc.py index fbe36257c5..3f35fc2d43 100755 --- a/ansys/mapdl/core/mapdl_grpc.py +++ b/ansys/mapdl/core/mapdl_grpc.py @@ -1128,38 +1128,7 @@ def input( """ # always check if file is present as the grpc and MAPDL errors # are unclear - if self._local: - if os.path.isdir(fname): - raise ValueError(f"`fname` should be a full file path or name, not the directory '{fname}'.") - else: - # It must be a file! - if os.path.isfile(fname): - # And it exist! - filename = os.path.join(os.getcwd(), fname) - elif fname in self.list_files(): # - # It exists in the Mapdl working directory - filename = os.path.join(self.directory, fname) - elif os.path.dirname(fname): - raise ValueError(f"'{fname}' appears to be an incomplete directory path rather than a filename.") - else: - # Finally - raise FileNotFoundError(f"Unable to locate filename '{fname}'") - - else: - if not os.path.dirname(fname): - # might be trying to run a local file. Check if the - # file exists remotely. - if fname not in self.list_files(): - self.upload(fname, progress_bar=progress_bar) - filename = fname - else: - # upload the file if it exists locally - if os.path.isfile(fname): - self.upload(fname, progress_bar=progress_bar) - filename = os.path.basename(fname) - else: - # Otherwise, it must be remote. Use full path. - filename = fname + filename = self._get_file_path(fname, progress_bar) if time_step_stream is not None: if time_step_stream <= 0: @@ -1227,6 +1196,53 @@ def input( # otherwise, read remote file return self._download_as_raw(tmp_out).decode("latin-1") + def _get_file_path(self, fname, progress_bar=False): + """Find files in the Python and MAPDL working directories. + + **The priority is for the Python directory.** + + Hence if the same file is in the Python directory and in the MAPDL directory, + PyMAPDL will upload a copy from the Python directory to the MAPDL directory, + overwriting the MAPDL directory copy. + """ + + if os.path.isdir(fname): + raise ValueError(f"`fname` should be a full file path or name, not the directory '{fname}'.") + + fpath = os.path.dirname(fname) + fname = os.path.basename(fname) + fext = fname.split('.')[-1] + ffullpath = os.path.join(fpath, fname) + + if os.path.exists(ffullpath) and self._local: + return ffullpath + + if self._local: + if os.path.isfile(fname): + # And it exists + filename = os.path.join(os.getcwd(), fname) + elif fname in self.list_files(): + # It exists in the Mapdl working directory + filename = os.path.join(self.directory, fname) + else: + # Finally + raise FileNotFoundError(f"Unable to locate filename '{fname}'") + + else: # Non-local + # upload the file if it exists locally + if os.path.isfile(ffullpath): + self.upload(ffullpath, progress_bar=progress_bar) + filename = fname + + elif fname in self.list_files(): + # It exists in the Mapdl working directory + filename = fname + + else: + raise FileNotFoundError(f"Unable to locate filename '{fname}'") + + return filename + def _flush_stored(self): """Writes stored commands to an input file and runs the input file. Used with non_interactive. diff --git a/tests/test_mapdl.py b/tests/test_mapdl.py index 7cfa911320..54a2e411a9 100644 --- a/tests/test_mapdl.py +++ b/tests/test_mapdl.py @@ -995,6 +995,14 @@ def test_inquire(mapdl): assert float(mapdl.inquire("", 'exist', jobname, 'lock')) in [0, 1] +def test_get_file_path(mapdl, tmpdir): + fname = 'dummy.txt' + fobject = tmpdir.join(fname) + fobject.write("Dummy file for testing") + + assert fname in mapdl._get_file_path(fobject) + + @pytest.mark.parametrize("option2,option3,option4", [('expdata.dat', '', ''), ('expdata', '.dat', ''), ('expdata', 'dat', 'DIR')]) def test_tbft(mapdl, option2, option3, option4): try: