Skip to content

Commit d7e7981

Browse files
germa89pre-commit-ci[bot]pyansys-ci-bot
authored
refactor: removing-run_as_prep7-in-favour-of-run_as (#3551)
* refactor: using a common wrapper based on context manager * refactor: replace elsewhere * ci: auto fixes from pre-commit.com hooks. for more information, see https://pre-commit.ci * chore: adding changelog file 3551.added.md [dependabot-skip] * feat: making sure we are properly getting the mapdl object in the wrappers --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: pyansys-ci-bot <[email protected]>
1 parent 2696095 commit d7e7981

File tree

6 files changed

+32
-24
lines changed

6 files changed

+32
-24
lines changed

doc/changelog.d/3551.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
refactor: removing-`run_as_prep7`-in-favour-of-`run_as`

src/ansys/mapdl/core/mapdl_core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
last_created,
7373
random_string,
7474
requires_package,
75-
run_as_prep7,
75+
run_as,
7676
supress_logging,
7777
)
7878

@@ -1574,7 +1574,7 @@ def open_apdl_log(
15741574
)
15751575

15761576
@supress_logging
1577-
@run_as_prep7
1577+
@run_as("PREP7")
15781578
def _generate_iges(self):
15791579
"""Save IGES geometry representation to disk"""
15801580
filename = os.path.join(self.directory, "_tmp.iges")

src/ansys/mapdl/core/mapdl_geometry.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
if TYPE_CHECKING: # pragma: no cover
3737
from pyiges import Iges
3838

39-
from ansys.mapdl.core.misc import requires_package, run_as_prep7, supress_logging
39+
from ansys.mapdl.core.misc import requires_package, run_as, supress_logging
4040
from ansys.mapdl.core.plotting.theme import MapdlTheme
4141

4242
VALID_SELECTION_TYPE = ["S", "R", "A", "U"]
@@ -614,7 +614,7 @@ def get_areas(
614614
return areas
615615

616616
@supress_logging
617-
@run_as_prep7
617+
@run_as("PREP7")
618618
@requires_package("pyvista")
619619
def generate_surface(
620620
self,

src/ansys/mapdl/core/mapdl_grpc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
last_created,
9191
only_numbers_and_dots,
9292
random_string,
93-
run_as_prep7,
93+
run_as,
9494
supress_logging,
9595
)
9696
from ansys.mapdl.core.parameters import interp_star_status
@@ -2961,7 +2961,7 @@ def __str__(self):
29612961
return info
29622962

29632963
@supress_logging
2964-
@run_as_prep7
2964+
@run_as("PREP7")
29652965
def _generate_iges(self):
29662966
"""Save IGES geometry representation to disk"""
29672967
basename = "_tmp.iges"

src/ansys/mapdl/core/misc.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,15 @@ def supress_logging(func):
136136

137137
@wraps(func)
138138
def wrapper(*args, **kwargs):
139+
from ansys.mapdl.core.mapdl import MapdlBase
140+
139141
mapdl = args[0]
142+
if not issubclass(type(mapdl), (MapdlBase)):
143+
# Assuming we are on a module object.
144+
mapdl = mapdl._mapdl
145+
if not issubclass(type(mapdl), (MapdlBase)):
146+
raise Exception("This wrapper cannot access MAPDL object")
147+
140148
prior_log_level = mapdl._log.level
141149
if prior_log_level != "CRITICAL":
142150
mapdl._set_log_level("CRITICAL")
@@ -151,28 +159,27 @@ def wrapper(*args, **kwargs):
151159
return wrapper
152160

153161

154-
def run_as_prep7(func):
162+
def run_as(routine: ROUTINES):
155163
"""Run a MAPDL method at PREP7 and always revert to the prior processor"""
156164

157-
@wraps(func)
158-
def wrapper(*args, **kwargs):
159-
mapdl = args[0]
160-
if hasattr(mapdl, "_mapdl"):
161-
mapdl = mapdl._mapdl
162-
prior_processor = mapdl.parameters.routine
163-
if prior_processor != "PREP7":
164-
mapdl.prep7()
165+
def decorator(function):
166+
@wraps(function)
167+
def wrapper(self, *args, **kwargs):
168+
from ansys.mapdl.core.mapdl import MapdlBase
165169

166-
out = func(*args, **kwargs)
170+
mapdl = self
171+
if not issubclass(type(mapdl), (MapdlBase)):
172+
# Assuming we are on a module object.
173+
mapdl = mapdl._mapdl
174+
if not issubclass(type(mapdl), (MapdlBase)):
175+
raise Exception("This wrapper cannot access MAPDL object")
167176

168-
if prior_processor == "Begin level":
169-
mapdl.finish()
170-
elif prior_processor != "PREP7":
171-
mapdl.run("/%s" % prior_processor)
177+
with mapdl.run_as_routine(routine.upper()):
178+
return function(self, *args, **kwargs)
172179

173-
return out
180+
return wrapper
174181

175-
return wrapper
182+
return decorator
176183

177184

178185
def threaded(func):

tests/test_misc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
load_file,
3737
no_return,
3838
requires_package,
39-
run_as_prep7,
39+
run_as,
4040
)
4141
from conftest import requires
4242

@@ -103,7 +103,7 @@ def test_run_as_prep7(mapdl, cleared):
103103
mapdl.post1()
104104
assert "POST1" in mapdl.parameters.routine
105105

106-
@run_as_prep7
106+
@run_as("PREP7")
107107
def fun(
108108
mapdl,
109109
): # This function is for mapdl methods, hence we have to pass the MAPDL instance somehow.

0 commit comments

Comments
 (0)