Skip to content

Commit

Permalink
Detaching file location input from Mapdl.input (#783)
Browse files Browse the repository at this point in the history
* Added `_get_file_path` function.

* Added test function.

* Variable name fixed.

* Apply suggestions from code review

Co-authored-by: Alex Kaszynski <[email protected]>

* 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 <[email protected]>
  • Loading branch information
germa89 and akaszynski authored Dec 28, 2021
1 parent ab900f8 commit 651bec5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 32 deletions.
80 changes: 48 additions & 32 deletions ansys/mapdl/core/mapdl_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions tests/test_mapdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 651bec5

Please sign in to comment.