Skip to content

Commit 56843f5

Browse files
committed
[OMCSession] update OMCSession* to use OMSessionABC as baseline and further cleanup
1 parent 221789b commit 56843f5

1 file changed

Lines changed: 105 additions & 14 deletions

File tree

OMPython/OMCSession.py

Lines changed: 105 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ class OMCSessionCmd:
7070
Implementation of Open Modelica Compiler API functions. Depreciated!
7171
"""
7272

73-
def __init__(self, session: OMCSession, readonly: bool = False):
74-
if not isinstance(session, OMCSession):
73+
def __init__(self, session: OMSessionABC, readonly: bool = False):
74+
if not isinstance(session, OMSessionABC):
7575
raise OMCSessionException("Invalid OMC process definition!")
7676
self._session = session
7777
self._readonly = readonly
@@ -301,7 +301,7 @@ class OMPathABC(pathlib.PurePosixPath, metaclass=abc.ABCMeta):
301301
limited compared to standard pathlib.Path objects.
302302
"""
303303

304-
def __init__(self, *path, session: OMCSession) -> None:
304+
def __init__(self, *path, session: OMSessionABC) -> None:
305305
super().__init__(*path)
306306
self._session = session
307307

@@ -611,7 +611,7 @@ def __init__(
611611
self,
612612
timeout: float = 10.00,
613613
omhome: Optional[str] = None,
614-
omc_process: Optional[OMCSession] = None,
614+
omc_process: Optional[OMCSessionABC] = None,
615615
) -> None:
616616
"""
617617
Initialisation for OMCSessionZMQ
@@ -623,7 +623,7 @@ def __init__(
623623

624624
if omc_process is None:
625625
omc_process = OMCSessionLocal(omhome=omhome, timeout=timeout)
626-
elif not isinstance(omc_process, OMCSession):
626+
elif not isinstance(omc_process, OMCSessionABC):
627627
raise OMCSessionException("Invalid definition of the OMC process!")
628628
self.omc_process = omc_process
629629

@@ -635,7 +635,7 @@ def escape_str(value: str) -> str:
635635
"""
636636
Escape a string such that it can be used as string within OMC expressions, i.e. escape all double quotes.
637637
"""
638-
return OMCSession.escape_str(value=value)
638+
return OMCSessionABC.escape_str(value=value)
639639

640640
def omcpath(self, *path) -> OMPathABC:
641641
"""
@@ -690,7 +690,7 @@ def __call__(cls, *args, **kwargs):
690690
return obj
691691

692692

693-
class OMCSessionMeta(abc.ABCMeta, PostInitCaller):
693+
class OMSessionMeta(abc.ABCMeta, PostInitCaller):
694694
"""
695695
Helper class to get a combined metaclass of ABCMeta and PostInitCaller.
696696
@@ -699,7 +699,98 @@ class OMCSessionMeta(abc.ABCMeta, PostInitCaller):
699699
"""
700700

701701

702-
class OMCSession(metaclass=OMCSessionMeta):
702+
class OMSessionABC(metaclass=OMSessionMeta):
703+
"""
704+
This class implements the basic structure a OMPython session definition needs. It provides the structure for an
705+
implementation using OMC as backend (via ZMQ) or a dummy implementation which just runs a model executable.
706+
"""
707+
708+
def __init__(
709+
self,
710+
timeout: float = 10.00,
711+
**kwargs,
712+
) -> None:
713+
"""
714+
Initialisation for OMSessionBase
715+
"""
716+
717+
# some helper data
718+
self.model_execution_windows = platform.system() == "Windows"
719+
self.model_execution_local = False
720+
721+
# store variables
722+
self._timeout = timeout
723+
724+
def __post_init__(self) -> None:
725+
"""
726+
Post initialisation method.
727+
"""
728+
729+
@staticmethod
730+
def escape_str(value: str) -> str:
731+
"""
732+
Escape a string such that it can be used as string within OMC expressions, i.e. escape all double quotes.
733+
"""
734+
return value.replace("\\", "\\\\").replace('"', '\\"')
735+
736+
@abc.abstractmethod
737+
def model_execution_prefix(self, cwd: Optional[OMPathABC] = None) -> list[str]:
738+
"""
739+
Helper function which returns a command prefix.
740+
"""
741+
742+
@abc.abstractmethod
743+
def get_version(self) -> str:
744+
"""
745+
Get the OM version.
746+
"""
747+
748+
@abc.abstractmethod
749+
def set_workdir(self, workdir: OMPathABC) -> None:
750+
"""
751+
Set the workdir for this session.
752+
"""
753+
754+
@abc.abstractmethod
755+
def omcpath(self, *path) -> OMPathABC:
756+
"""
757+
Create an OMPathBase object based on the given path segments and the current class.
758+
"""
759+
760+
@abc.abstractmethod
761+
def omcpath_tempdir(self, tempdir_base: Optional[OMCPath] = None) -> OMPathABC:
762+
"""
763+
Get a temporary directory based on the specific definition for this session.
764+
"""
765+
766+
@staticmethod
767+
def _tempdir(tempdir_base: OMPathABC) -> OMPathABC:
768+
names = [str(uuid.uuid4()) for _ in range(100)]
769+
770+
tempdir: Optional[OMPathABC] = None
771+
for name in names:
772+
# create a unique temporary directory name
773+
tempdir = tempdir_base / name
774+
775+
if tempdir.exists():
776+
continue
777+
778+
tempdir.mkdir(parents=True, exist_ok=False)
779+
break
780+
781+
if tempdir is None or not tempdir.is_dir():
782+
raise FileNotFoundError(f"Cannot create a temporary directory in {tempdir_base}!")
783+
784+
return tempdir
785+
786+
@abc.abstractmethod
787+
def sendExpression(self, expr: str, parsed: bool = True) -> Any:
788+
"""
789+
Function needed to send expressions to the OMC server via ZMQ.
790+
"""
791+
792+
793+
class OMCSessionABC(OMSessionABC, metaclass=abc.ABCMeta):
703794
"""
704795
Base class for an OMC session started via ZMQ. This class contains common functionality for all variants of an
705796
OMC session definition.
@@ -1105,7 +1196,7 @@ def _get_portfile_path(self) -> Optional[pathlib.Path]:
11051196
return portfile_path
11061197

11071198

1108-
class OMCSessionPort(OMCSession):
1199+
class OMCSessionPort(OMCSessionABC):
11091200
"""
11101201
OMCSession implementation which uses a port to connect to an already running OMC server.
11111202
"""
@@ -1118,7 +1209,7 @@ def __init__(
11181209
self._omc_port = omc_port
11191210

11201211

1121-
class OMCSessionLocal(OMCSession):
1212+
class OMCSessionLocal(OMCSessionABC):
11221213
"""
11231214
OMCSession implementation which runs the OMC server locally on the machine (Linux / Windows).
11241215
"""
@@ -1199,7 +1290,7 @@ def _omc_port_get(self) -> str:
11991290
return port
12001291

12011292

1202-
class OMCSessionDockerHelper(OMCSession):
1293+
class OMCSessionDockerABC(OMCSessionABC, metaclass=abc.ABCMeta):
12031294
"""
12041295
Base class for OMCSession implementations which run the OMC server in a Docker container.
12051296
"""
@@ -1327,7 +1418,7 @@ def model_execution_prefix(self, cwd: Optional[OMPathABC] = None) -> list[str]:
13271418
return docker_cmd
13281419

13291420

1330-
class OMCSessionDocker(OMCSessionDockerHelper):
1421+
class OMCSessionDocker(OMCSessionDockerABC):
13311422
"""
13321423
OMC process running in a Docker container.
13331424
"""
@@ -1469,7 +1560,7 @@ def _docker_omc_start(self) -> Tuple[subprocess.Popen, DockerPopen, str]:
14691560
return omc_process, docker_process, docker_cid
14701561

14711562

1472-
class OMCSessionDockerContainer(OMCSessionDockerHelper):
1563+
class OMCSessionDockerContainer(OMCSessionDockerABC):
14731564
"""
14741565
OMC process running in a Docker container (by container ID).
14751566
"""
@@ -1562,7 +1653,7 @@ def _docker_omc_start(self) -> Tuple[subprocess.Popen, DockerPopen]:
15621653
return omc_process, docker_process
15631654

15641655

1565-
class OMCSessionWSL(OMCSession):
1656+
class OMCSessionWSL(OMCSessionABC):
15661657
"""
15671658
OMC process running in Windows Subsystem for Linux (WSL).
15681659
"""

0 commit comments

Comments
 (0)