|
| 1 | +# Copyright (C) 2024 ANSYS, Inc. and/or its affiliates. |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +# |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in all |
| 13 | +# copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +# SOFTWARE. |
| 22 | + |
| 23 | +from typing import Optional, Protocol |
| 24 | + |
| 25 | +from ansys.mapdl.core.mapdl import MapdlBase |
| 26 | + |
| 27 | + |
| 28 | +class _Backend(Protocol): |
| 29 | + def run_command(self) -> str: ... |
| 30 | + |
| 31 | + |
| 32 | +class MapdlInProcess(MapdlBase): |
| 33 | + def __init__(self, backend: _Backend): |
| 34 | + super().__init__( |
| 35 | + loglevel="WARNING", use_vtk=False, log_apdl=None, print_com=False |
| 36 | + ) |
| 37 | + self._backend = backend |
| 38 | + self._cleanup: bool = True |
| 39 | + self._name: str = "MapdlInProcess" |
| 40 | + self._session_id: Optional[str] = None |
| 41 | + self._mute: bool = False |
| 42 | + |
| 43 | + def _run(self, command: str, verbose: bool = False, mute: bool = False) -> str: |
| 44 | + if not command.strip(): |
| 45 | + raise ValueError("Empty commands not allowed") |
| 46 | + |
| 47 | + if len(command) > 639: |
| 48 | + raise ValueError("Maximum command length mut be less than 640 characters") |
| 49 | + |
| 50 | + return self._backend.run_command(command, verbose, mute).strip() |
| 51 | + |
| 52 | + @property |
| 53 | + def name(self) -> str: |
| 54 | + return self._name |
| 55 | + |
| 56 | + @name.setter |
| 57 | + def name(self, name) -> None: |
| 58 | + self._name = name |
| 59 | + |
| 60 | + def _check_session_id(self) -> None: |
| 61 | + pass |
| 62 | + |
| 63 | + def __repr__(self): |
| 64 | + info = super().__repr__() |
| 65 | + return info |
0 commit comments