From 26299dc9ed8aa868b0222631ee6a015d2ab1f5d2 Mon Sep 17 00:00:00 2001 From: syntron Date: Sun, 18 Jan 2026 20:31:51 +0100 Subject: [PATCH] [OMCSession] add get_version() and get_workdir() * this prepares a version of OMCSession which is independend of OMC as these functions where the last two which needed sendExpression() in basic ModelicaSystem functionality --- OMPython/ModelicaSystem.py | 5 ++--- OMPython/OMCSession.py | 13 +++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/OMPython/ModelicaSystem.py b/OMPython/ModelicaSystem.py index 03c24b51..96c75224 100644 --- a/OMPython/ModelicaSystem.py +++ b/OMPython/ModelicaSystem.py @@ -352,7 +352,7 @@ def __init__( self._session = OMCSessionLocal(omhome=omhome) # get OpenModelica version - version_str = self.sendExpression(expr="getVersion()") + version_str = self._session.get_version() self._version = self._parse_om_version(version=version_str) # set commandLineOptions using default values or the user defined list if command_line_options is None: @@ -514,8 +514,7 @@ def setWorkDirectory(self, work_directory: Optional[str | os.PathLike] = None) - raise IOError(f"{workdir} could not be created") logger.info("Define work dir as %s", workdir) - expr = f'cd("{workdir.as_posix()}")' - self.sendExpression(expr=expr) + self._session.set_workdir(workdir=workdir) # set the class variable _work_dir ... self._work_dir = workdir diff --git a/OMPython/OMCSession.py b/OMPython/OMCSession.py index 6b5b2b3d..67fb1af2 100644 --- a/OMPython/OMCSession.py +++ b/OMPython/OMCSession.py @@ -766,6 +766,19 @@ def escape_str(value: str) -> str: """ return value.replace("\\", "\\\\").replace('"', '\\"') + def get_version(self) -> str: + """ + Get the OM version. + """ + return self.sendExpression("getVersion()", parsed=True) + + def set_workdir(self, workdir: OMCPath) -> None: + """ + Set the workdir for this session. + """ + exp = f'cd("{workdir.as_posix()}")' + self.sendExpression(exp) + def omcpath(self, *path) -> OMCPath: """ Create an OMCPath object based on the given path segments and the current OMCSession* class.