From 0221019a8733afb0aa0e88be4e2405648fa215d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sluka?= Date: Tue, 8 Apr 2025 18:01:14 +0200 Subject: [PATCH] Fix ModelicaSystem cannot load from relative path This fixes https://github.com/OpenModelica/OMPython/issues/245 --- OMPython/__init__.py | 10 +++++----- tests/test_ModelicaSystem.py | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/OMPython/__init__.py b/OMPython/__init__.py index f4a59334b..ff9b9c3e4 100644 --- a/OMPython/__init__.py +++ b/OMPython/__init__.py @@ -29,6 +29,7 @@ import pyparsing import importlib import zmq +import pathlib if sys.platform == 'darwin': @@ -700,7 +701,7 @@ def __init__(self, fileName=None, modelName=None, lmodel=None, commandLineOption self.xmlFile = None self.lmodel = lmodel # may be needed if model is derived from other model self.modelName = modelName # Model class name - self.fileName = fileName # Model file/package name + self.fileName = pathlib.Path(fileName).resolve() if fileName is not None else None # Model file/package name self.inputFlag = False # for model with input quantity self.simulationFlag = False # if the model is simulated? self.outputFlag = False @@ -710,8 +711,8 @@ def __init__(self, fileName=None, modelName=None, lmodel=None, commandLineOption self._raiseerrors = raiseerrors - if fileName is not None and not os.path.exists(self.fileName): # if file does not exist - raise IOError("File Error:" + os.path.abspath(self.fileName) + " does not exist!!!") + if fileName is not None and not self.fileName.is_file(): # if file does not exist + raise IOError(f"File Error: {self.fileName} does not exist!!!") # set default command Line Options for linearization as # linearize() will use the simulation executable and runtime @@ -741,8 +742,7 @@ def setCommandLineOptions(self, commandLineOptions: str): def loadFile(self): # load file - loadFileExp = "".join(["loadFile(", "\"", self.fileName, "\"", ")"]).replace("\\", "/") - loadMsg = self.sendExpression(loadFileExp) + loadMsg = self.sendExpression(f'loadFile("{self.fileName.as_posix()}")') # Show notification or warnings to the user when verbose=True OR if some error occurred i.e., not result if self._verbose or not loadMsg: self._check_error() diff --git a/tests/test_ModelicaSystem.py b/tests/test_ModelicaSystem.py index eb095d29c..44884a32b 100644 --- a/tests/test_ModelicaSystem.py +++ b/tests/test_ModelicaSystem.py @@ -3,6 +3,7 @@ import tempfile import shutil import os +import pathlib class ModelicaSystemTester(unittest.TestCase): @@ -76,6 +77,29 @@ def test_setSimulationOptions(self): assert d["stopTime"] == "2.1" assert d["tolerance"] == "1.2e-08" + def test_relative_path(self): + cwd = pathlib.Path.cwd() + (fd, name) = tempfile.mkstemp(dir=cwd, text=True) + try: + with os.fdopen(fd, 'w') as f: + f.write("""model M + Real x(start = 1, fixed=true); + parameter Real a = -1; +equation + der(x) = x*a; +end M; +""") + + model_file = pathlib.Path(name).relative_to(cwd) + model_relative = str(model_file) + assert "/" not in model_relative + + mod = OMPython.ModelicaSystem(model_relative, "M", raiseerrors=True) + assert float(mod.getParameters("a")[0]) == -1 + finally: + # clean up the temporary file + model_file.unlink() + if __name__ == '__main__': unittest.main()