Skip to content

mapdl.use uploads macro file to remote instance #1509

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/ansys/mapdl/core/_commands/apdl/macro_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,4 +566,6 @@ def use(
"""
command = f"*USE,{name},{arg1},{arg2},{arg3},{arg4},{arg5},{arg6},{arg7},{arg8},{arg9},{ar10},{ar11},{ar12},{ar13},{ar14},{ag15},{ar16},{ar17},{ar18}"
with self.non_interactive:
return self.run(command, **kwargs)
self.run(command, **kwargs)

return self._response # returning last response
37 changes: 37 additions & 0 deletions src/ansys/mapdl/core/mapdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -3741,3 +3741,40 @@ def _file(self, filename, **kwargs):
fname = str(filename).replace(ext, "")
ext = ext.replace(".", "")
return self.run(f"FILE,{fname},{ext}", **kwargs)

@wraps(Commands.lsread)
def use(self, *args, **kwargs):
# Because of `name` can be a macro file or a macro block on a macro library
# file, we are going to test if the file exists locally first, then remote,
# and if not, silently assume that it is a macro in a macro library.
# I do not think there is a way to check if the macro exists before use it.
name = kwargs.get("name", args[0])
base_name = os.path.basename(name)

# Check if it is a file local
if os.path.exists(name):
self.upload(name)

elif base_name in self.list_files():
# the file exists in the MAPDL working directory, so do nothing.
pass

else:
if os.path.dirname(name):
# It seems you provided a path (or something like that)
raise FileNotFoundError(
f"The name supplied to 'mapdl.use' ('{name}') is not a file in the Python "
"working directory, nor in the MAPDL working directory. "
)
# Preferring logger.warning over warn (from warnings), since it is less intrusive.
self._log.warning(
f"The name supplied to 'mapdl.use' ('{name}') is not a file in the Python "
"working directory, nor in the MAPDL working directory. "
"PyMAPDL will assume it is a macro block inside a macro library "
"file previously defined using 'mapdl.ulib'."
)
# If MAPDL cannot find named macro file, it will throw a runtime error.

# Updating arg since the path is not needed anymore.
args = (base_name, args[1:])
return super().use(*args, **kwargs)
23 changes: 23 additions & 0 deletions tests/test_mapdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1571,3 +1571,26 @@ def test_get_fallback(mapdl, cleared):

with pytest.raises(ValueError, match="There are no ELEMENTS defined"):
mapdl.get_value("elem", 0, "num", "maxd")


def test_use_uploading(mapdl, cleared, tmpdir):
mymacrofile_name = "mymacrofile.mac"
mymacrofile = tmpdir.join(mymacrofile_name)
with open(mymacrofile, "w") as fid:
fid.write("/prep7\n/eof")

assert mymacrofile_name not in mapdl.list_files()
out = mapdl.use(mymacrofile)
assert f"USE MACRO FILE {mymacrofile_name}" in out
assert mymacrofile_name in mapdl.list_files()

os.remove(mymacrofile)
out = mapdl.use(mymacrofile)

# Raises an error.
with pytest.raises(RuntimeError):
mapdl.use("myinexistentmacro.mac")

# Raise an error
with pytest.raises(FileNotFoundError):
mapdl.use("asdf/myinexistentmacro.mac")