Skip to content

Commit a4a9162

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

2 files changed

Lines changed: 8 additions & 13 deletions

File tree

OMPython/ModelicaSystem.py

Lines changed: 7 additions & 13 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,
@@ -350,7 +350,10 @@ def __init__(
350350
self._session = OMCSessionLocal(omhome=omhome)
351351

352352
# get OpenModelica version
353-
self._version = self._session.sendExpression("getVersion()", parsed=True)
353+
version_str = self._session.sendExpression("getVersion()", parsed=True)
354+
# remove 'OpenModelica ' at the start of the string and possible development ids (i.e. '~dev-26-g10bb273')
355+
version_str = version_str[13:version_str.find('~')]
356+
self._version = Version(version_str)
354357
# set commandLineOptions using default values or the user defined list
355358
if command_line_options is None:
356359
# set default command line options to improve the performance of linearization and to avoid recompilation if
@@ -1022,13 +1025,6 @@ def getOptimizationOptions(
10221025

10231026
raise ModelicaSystemError("Unhandled input for getOptimizationOptions()")
10241027

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-
10321028
def simulate_cmd(
10331029
self,
10341030
result_file: OMCPath,
@@ -1078,8 +1074,7 @@ def simulate_cmd(
10781074
# simulation options are not read from override file from version >= 1.26.0,
10791075
# pass them to simulation executable directly as individual arguments
10801076
# 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):
1077+
if self._version >= Version("1.26.0"):
10831078
for key, opt_value in self._simulate_options_override.items():
10841079
om_cmd.arg_set(key=key, val=str(opt_value))
10851080
override_content = (
@@ -1775,8 +1770,7 @@ def linearize(
17751770
)
17761771

17771772
# 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):
1773+
if self._version >= Version("1.26.0"):
17801774
for key, opt_value in self._linearization_options.items():
17811775
om_cmd.arg_set(key=key, val=str(opt_value))
17821776
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)