Skip to content

Commit 32e0cc9

Browse files
germa89akaszynski
andauthored
Fix/ wrong if in self.directory (#680)
* Fixed a wrong if comparison. Added setter and getter to ``self.directory``. Added also a wrapper to `CWD` so it will show a warning when the directory we are changing to does not exist. * Added test case for mapdl.cwd and mapdl.directory * Apply suggestions from code review Co-authored-by: Alex Kaszynski <[email protected]> * Added example to docstring. * Grammar fix. Co-authored-by: Alex Kaszynski <[email protected]>
1 parent 3a30f4d commit 32e0cc9

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

ansys/mapdl/core/mapdl.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import weakref
1313
import warnings
1414
import pathlib
15+
from warnings import warn
16+
from functools import wraps
1517

1618
import numpy as np
1719

@@ -2248,6 +2250,14 @@ def directory(self):
22482250
>>> mapdl.directory
22492251
'C:/temp_directory/'
22502252
2253+
Setting the directory
2254+
2255+
>>> mapdl.directory = 'C:/temp_directory/'
2256+
None
2257+
2258+
In case the directory does not exist or it is not
2259+
accessible, ``cwd`` (:func:`_MapdlCore.cwd`) will raise
2260+
a warning.
22512261
"""
22522262
# always attempt to cache the path
22532263
try:
@@ -2256,12 +2266,18 @@ def directory(self):
22562266
pass
22572267

22582268
# os independent path format
2259-
if self._path is not None:
2269+
if self._path: # self.inquire might return ''.
22602270
self._path = self._path.replace("\\", "/")
22612271
# new line to fix path issue, see #416
22622272
self._path = repr(self._path)[1:-1]
22632273
return self._path
22642274

2275+
@directory.setter
2276+
@supress_logging
2277+
def directory(self, path):
2278+
"""Change the directory using ``Mapdl.cwd``"""
2279+
self.cwd(path) # this has been wrapped in Mapdl to show a warning if the file does not exist.
2280+
22652281
@property
22662282
def _lockfile(self):
22672283
"""lockfile path"""
@@ -2473,3 +2489,13 @@ def list(self, filename, ext=""):
24732489
path = str(path) + ext
24742490
with open(path) as fid:
24752491
return fid.read()
2492+
2493+
@wraps(Commands.cwd)
2494+
def cwd(self, *args, **kwargs):
2495+
"""Wraps cwd"""
2496+
returns_ = super().cwd( *args, **kwargs)
2497+
2498+
if '*** WARNING ***' in self._response:
2499+
warn('\n' + self._response)
2500+
2501+
return returns_

tests/test_mapdl.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,3 +873,16 @@ def test_path_with_single_quote(mapdl, path_tests):
873873
with pytest.raises(RuntimeError):
874874
resp = mapdl.cwd(path_tests.path_with_single_quote)
875875
assert 'WARNING' not in resp
876+
877+
878+
@skip_in_cloud
879+
def test_cwd_directory(mapdl, tmpdir):
880+
881+
mapdl.directory = str(tmpdir)
882+
assert mapdl.directory == str(tmpdir).replace('\\', '/')
883+
884+
wrong_path = 'wrong_path'
885+
with pytest.warns(Warning) as record:
886+
mapdl.directory = wrong_path
887+
assert 'The working directory specified' in record.list[-1].message.args[0]
888+
assert 'is not a directory on' in record.list[-1].message.args[0]

0 commit comments

Comments
 (0)