Skip to content

Commit c6edf9b

Browse files
committed
[ModelicaSystem] use packaging.version.Version to replace parse_om_version()
1 parent 429fe87 commit c6edf9b

2 files changed

Lines changed: 8 additions & 14 deletions

File tree

OMPython/ModelicaSystem.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
import os
1212
import pathlib
1313
import queue
14-
import re
1514
import textwrap
1615
import threading
1716
from typing import Any, cast, Optional
1817
import warnings
1918
import xml.etree.ElementTree as ET
2019

2120
import numpy as np
21+
from packaging.version import Version
2222

2323
from OMPython.OMCSession import (
2424
OMCSessionException,
@@ -349,8 +349,10 @@ def __init__(
349349
else:
350350
self._session = OMCSessionLocal(omhome=omhome)
351351

352-
# get OpenModelica version
353-
self._version = self._session.sendExpression("getVersion()", parsed=True)
352+
# get OpenModelica version via getVersion(); the start of the string must be removed, i.e. 'OpenModelica '
353+
version_str = self._session.sendExpression("getVersion()", parsed=True)
354+
version_str = version_str[13:]
355+
self._version = Version(version_str)
354356
# set commandLineOptions using default values or the user defined list
355357
if command_line_options is None:
356358
# set default command line options to improve the performance of linearization and to avoid recompilation if
@@ -1022,13 +1024,6 @@ def getOptimizationOptions(
10221024

10231025
raise ModelicaSystemError("Unhandled input for getOptimizationOptions()")
10241026

1025-
def parse_om_version(self, version: str) -> tuple[int, int, int]:
1026-
match = re.search(r"v?(\d+)\.(\d+)\.(\d+)", version)
1027-
if not match:
1028-
raise ValueError(f"Version not found in: {version}")
1029-
major, minor, patch = map(int, match.groups())
1030-
return major, minor, patch
1031-
10321027
def simulate_cmd(
10331028
self,
10341029
result_file: OMCPath,
@@ -1078,8 +1073,7 @@ def simulate_cmd(
10781073
# simulation options are not read from override file from version >= 1.26.0,
10791074
# pass them to simulation executable directly as individual arguments
10801075
# see https://github.com/OpenModelica/OpenModelica/pull/14813
1081-
major, minor, patch = self.parse_om_version(self._version)
1082-
if (major, minor, patch) >= (1, 26, 0):
1076+
if self._version >= Version("1.26.0"):
10831077
for key, opt_value in self._simulate_options_override.items():
10841078
om_cmd.arg_set(key=key, val=str(opt_value))
10851079
override_content = (
@@ -1775,8 +1769,7 @@ def linearize(
17751769
)
17761770

17771771
# See comment in simulate_cmd regarding override file and OM version
1778-
major, minor, patch = self.parse_om_version(self._version)
1779-
if (major, minor, patch) >= (1, 26, 0):
1772+
if self._version >= Version("1.26.0"):
17801773
for key, opt_value in self._linearization_options.items():
17811774
om_cmd.arg_set(key=key, val=str(opt_value))
17821775
override_content = (

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ license = { file = "LICENSE" }
1717
requires-python = ">=3.10"
1818
dependencies = [
1919
"numpy",
20+
"packaging",
2021
"psutil",
2122
"pyparsing",
2223
"pyzmq",

0 commit comments

Comments
 (0)