Skip to content

Commit d6eb0e3

Browse files
committed
[OMCSession] update OMCSession* to use OMSessionABC as baseline and further cleanup
1 parent 9a42128 commit d6eb0e3

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: OMCSessionABC, readonly: bool = False):
74+
if not isinstance(session, OMCSessionABC):
7575
raise OMCSessionException("Invalid OMC process definition!")
7676
self._session = session
7777
self._readonly = readonly
@@ -266,7 +266,7 @@ class OMPathABC(pathlib.PurePosixPath, metaclass=abc.ABCMeta):
266266
compared to standard pathlib.Path objects.
267267
"""
268268

269-
def __init__(self, *path, session: OMCSession) -> None:
269+
def __init__(self, *path, session: OMSessionABC) -> None:
270270
super().__init__(*path)
271271
self._session = session
272272

@@ -617,7 +617,7 @@ def __init__(
617617
self,
618618
timeout: float = 10.00,
619619
omhome: Optional[str] = None,
620-
omc_process: Optional[OMCSession] = None,
620+
omc_process: Optional[OMCSessionABC] = None,
621621
) -> None:
622622
"""
623623
Initialisation for OMCSessionZMQ
@@ -629,7 +629,7 @@ def __init__(
629629

630630
if omc_process is None:
631631
omc_process = OMCSessionLocal(omhome=omhome, timeout=timeout)
632-
elif not isinstance(omc_process, OMCSession):
632+
elif not isinstance(omc_process, OMCSessionABC):
633633
raise OMCSessionException("Invalid definition of the OMC process!")
634634
self.omc_process = omc_process
635635

@@ -641,7 +641,7 @@ def escape_str(value: str) -> str:
641641
"""
642642
Escape a string such that it can be used as string within OMC expressions, i.e. escape all double quotes.
643643
"""
644-
return OMCSession.escape_str(value=value)
644+
return OMCSessionABC.escape_str(value=value)
645645

646646
def omcpath(self, *path) -> OMPathABC:
647647
"""
@@ -696,7 +696,7 @@ def __call__(cls, *args, **kwargs):
696696
return obj
697697

698698

699-
class OMCSessionMeta(abc.ABCMeta, PostInitCaller):
699+
class OMSessionMeta(abc.ABCMeta, PostInitCaller):
700700
"""
701701
Helper class to get a combined metaclass of ABCMeta and PostInitCaller.
702702
@@ -705,7 +705,98 @@ class OMCSessionMeta(abc.ABCMeta, PostInitCaller):
705705
"""
706706

707707

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

10771168

1078-
class OMCSessionPort(OMCSession):
1169+
class OMCSessionPort(OMCSessionABC):
10791170
"""
10801171
OMCSession implementation which uses a port to connect to an already running OMC server.
10811172
"""
@@ -1088,7 +1179,7 @@ def __init__(
10881179
self._omc_port = omc_port
10891180

10901181

1091-
class OMCSessionLocal(OMCSession):
1182+
class OMCSessionLocal(OMCSessionABC):
10921183
"""
10931184
OMCSession implementation which runs the OMC server locally on the machine (Linux / Windows).
10941185
"""
@@ -1174,7 +1265,7 @@ def _omc_port_get(self) -> str:
11741265
return port
11751266

11761267

1177-
class OMCSessionDockerHelper(OMCSession):
1268+
class OMCSessionDockerABC(OMCSessionABC, metaclass=abc.ABCMeta):
11781269
"""
11791270
Base class for OMCSession implementations which run the OMC server in a Docker container.
11801271
"""
@@ -1305,7 +1396,7 @@ def model_execution_prefix(self, cwd: Optional[OMPathABC] = None) -> list[str]:
13051396
return docker_cmd
13061397

13071398

1308-
class OMCSessionDocker(OMCSessionDockerHelper):
1399+
class OMCSessionDocker(OMCSessionDockerABC):
13091400
"""
13101401
OMC process running in a Docker container.
13111402
"""
@@ -1448,7 +1539,7 @@ def _docker_omc_start(self) -> Tuple[subprocess.Popen, DockerPopen, str]:
14481539
return omc_process, docker_process, docker_cid
14491540

14501541

1451-
class OMCSessionDockerContainer(OMCSessionDockerHelper):
1542+
class OMCSessionDockerContainer(OMCSessionDockerABC):
14521543
"""
14531544
OMC process running in a Docker container (by container ID).
14541545
"""
@@ -1541,7 +1632,7 @@ def _docker_omc_start(self) -> Tuple[subprocess.Popen, DockerPopen]:
15411632
return omc_process, docker_process
15421633

15431634

1544-
class OMCSessionWSL(OMCSession):
1635+
class OMCSessionWSL(OMCSessionABC):
15451636
"""
15461637
OMC process running in Windows Subsystem for Linux (WSL).
15471638
"""

0 commit comments

Comments
 (0)