diff --git a/doc/changelog.d/3220.added.md b/doc/changelog.d/3220.added.md new file mode 100644 index 0000000000..23012e612b --- /dev/null +++ b/doc/changelog.d/3220.added.md @@ -0,0 +1 @@ +refactor: clean mapdl inprocess and move mute to MapdlCore \ No newline at end of file diff --git a/src/ansys/mapdl/core/mapdl_console.py b/src/ansys/mapdl/core/mapdl_console.py index 88c480fb69..b39fa57fea 100644 --- a/src/ansys/mapdl/core/mapdl_console.py +++ b/src/ansys/mapdl/core/mapdl_console.py @@ -117,6 +117,7 @@ def __init__( self._auto_continue = True self._continue_on_error = False self._process = None + self._name = None self._launch(start_parm) super().__init__( loglevel=loglevel, @@ -315,7 +316,7 @@ def kill(self): self._log.warning("Unable to kill process %d", self._process.pid) self._log.debug("Killed process %d", self._process.pid) - @property + @MapdlBase.name.getter def name(self): """Instance unique identifier.""" if not self._name: diff --git a/src/ansys/mapdl/core/mapdl_core.py b/src/ansys/mapdl/core/mapdl_core.py index 27f521f78f..e4502ece48 100644 --- a/src/ansys/mapdl/core/mapdl_core.py +++ b/src/ansys/mapdl/core/mapdl_core.py @@ -236,7 +236,6 @@ def __init__( ): """Initialize connection with MAPDL.""" atexit.register(self.__del__) # registering to exit properly - self._name = None # For naming the instance. self._show_matplotlib_figures = True # for testing self._query = None self._exited: bool = False @@ -253,6 +252,7 @@ def __init__( self._file_type_for_plots = file_type_for_plots self._default_file_type_for_plots = file_type_for_plots self._version = None # cached version + self._mute = False if _HAS_PYVISTA: if use_vtk is not None: # pragma: no cover @@ -330,6 +330,9 @@ def __init__( self._info = Information(self) + def _after_run(self, _command: str) -> None: + pass + @property def allow_ignore(self): """Invalid commands will be ignored rather than exceptions @@ -373,6 +376,9 @@ def allow_ignore(self, value): ) self._ignore_errors = bool(value) + def _before_run(self, _command: str) -> None: + pass + @property def chain_commands(self): """Chain several mapdl commands. @@ -2156,14 +2162,6 @@ def run( self._stored_commands.append(command) return - # Actually sending the message - if self._session_id is not None: - self._check_session_id() - else: - # For some reason the session hasn't been created - if self.is_grpc: - self._create_session() - if mute is None: if hasattr(self, "mute"): mute = self.mute @@ -2225,6 +2223,8 @@ def run( # Edge case. `\title, 'par=1234' ` self._check_parameter_name(param_name) + self._before_run(command) + short_cmd = parse_to_short_cmd(command) text = self._run(command, verbose=verbose, mute=mute) @@ -2236,9 +2236,7 @@ def run( self.show(self.default_file_type_for_plots) text = self._run(command, verbose=verbose, mute=mute) - if command[:4].upper() == "/CLE" and self.is_grpc: - # We have reset the database, so we need to create a new session id - self._create_session() + self._after_run(command) if mute: return diff --git a/src/ansys/mapdl/core/mapdl_grpc.py b/src/ansys/mapdl/core/mapdl_grpc.py index f6d4442068..903622b609 100644 --- a/src/ansys/mapdl/core/mapdl_grpc.py +++ b/src/ansys/mapdl/core/mapdl_grpc.py @@ -356,6 +356,7 @@ def __init__( remove_temp_dir_on_exit = remove_temp_files remove_temp_files = None + self._name: Optional[str] = None self._session_id_: Optional[str] = None self._checking_session_id_: bool = False self.__distributed: Optional[bool] = None @@ -411,7 +412,6 @@ def __init__( self._health_response_queue: Optional["Queue"] = None self._exiting: bool = False self._exited: Optional[bool] = None - self._mute: bool = False self._db: Optional[MapdlDb] = None self.__server_version: Optional[str] = None self._state: Optional[grpc.Future] = None @@ -489,6 +489,18 @@ def __init__( self._create_session() + def _after_run(self, command: str) -> None: + if command[:4].upper() == "/CLE": + # We have reset the database, so we need to create a new session id + self._create_session() + + def _before_run(self, _command: str) -> None: + if self._session_id is not None: + self._check_session_id() + else: + # For some reason the session hasn't been created + self._create_session() + def _create_process_stds_queue(self, process=None): from ansys.mapdl.core.launcher import ( _create_queue_for_std, # Avoid circular import error @@ -2955,7 +2967,7 @@ def cmatrix( # non-interactive and there's no output to return super().cmatrix(symfac, condname, numcond, grndkey, capname, **kwargs) - @property + @MapdlBase.name.getter def name(self) -> str: """Instance unique identifier.""" if not self._name: diff --git a/src/ansys/mapdl/core/mapdl_inprocess.py b/src/ansys/mapdl/core/mapdl_inprocess.py index 4ff91dad1a..2fb86f4d88 100644 --- a/src/ansys/mapdl/core/mapdl_inprocess.py +++ b/src/ansys/mapdl/core/mapdl_inprocess.py @@ -20,25 +20,32 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. -from typing import Optional, Protocol +from typing import Protocol from ansys.mapdl.core.mapdl import MapdlBase class _Backend(Protocol): - def run_command(self) -> str: ... + def run_command(self, command: str, verbose: bool, mute: bool) -> str: ... + + def input_file( + self, + filename: str, + extension: str, + directory: str, + line: int, + log: int, + mute: bool, + ) -> str: ... class MapdlInProcess(MapdlBase): - def __init__(self, backend: _Backend): + def __init__(self, in_process_backend: _Backend): super().__init__( loglevel="WARNING", use_vtk=False, log_apdl=None, print_com=False ) - self._backend = backend + self._in_process_backend = in_process_backend self._cleanup: bool = True - self._name: str = "MapdlInProcess" - self._session_id: Optional[str] = None - self._mute: bool = False def _run(self, command: str, verbose: bool = False, mute: bool = False) -> str: if not command.strip(): @@ -47,19 +54,22 @@ def _run(self, command: str, verbose: bool = False, mute: bool = False) -> str: if len(command) > 639: raise ValueError("Maximum command length mut be less than 640 characters") - return self._backend.run_command(command, verbose, mute).strip() - - @property - def name(self) -> str: - return self._name - - @name.setter - def name(self, name) -> None: - self._name = name + return self._in_process_backend.run_command(command, verbose, mute).strip() - def _check_session_id(self) -> None: - pass + def input( + self, + fname: str = "", + ext: str = "", + dir_: str = "", + line: str = "", + log: str = "", + mute: bool = False, + **_, + ): + return self._in_process_backend.input_file( + fname, ext, dir_, int(line or 0), int(log or 0), mute + ) - def __repr__(self): - info = super().__repr__() - return info + @MapdlBase.name.getter + def name(self) -> str: + return "MapdlInProcess"