Skip to content

Commit 8d8fd87

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

2 files changed

Lines changed: 6 additions & 13 deletions

File tree

OMPython/ModelicaSystem.py

Lines changed: 5 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,8 @@ 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+
self._version = Version(version_str)
354355
# set commandLineOptions using default values or the user defined list
355356
if command_line_options is None:
356357
# set default command line options to improve the performance of linearization and to avoid recompilation if
@@ -1022,13 +1023,6 @@ def getOptimizationOptions(
10221023

10231024
raise ModelicaSystemError("Unhandled input for getOptimizationOptions()")
10241025

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-
10321026
def simulate_cmd(
10331027
self,
10341028
result_file: OMCPath,
@@ -1078,8 +1072,7 @@ def simulate_cmd(
10781072
# simulation options are not read from override file from version >= 1.26.0,
10791073
# pass them to simulation executable directly as individual arguments
10801074
# 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):
1075+
if self._version >= Version("1.26.0"):
10831076
for key, opt_value in self._simulate_options_override.items():
10841077
om_cmd.arg_set(key=key, val=str(opt_value))
10851078
override_content = (
@@ -1775,8 +1768,7 @@ def linearize(
17751768
)
17761769

17771770
# 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):
1771+
if self._version >= Version("1.26.0"):
17801772
for key, opt_value in self._linearization_options.items():
17811773
om_cmd.arg_set(key=key, val=str(opt_value))
17821774
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)