Skip to content

Commit 098b89a

Browse files
committed
[ModelicaSystemCmd] cleanup - do not define (unused / not useable) class
1 parent a631e52 commit 098b89a

3 files changed

Lines changed: 57 additions & 101 deletions

File tree

OMPython/ModelicaSystem.py

Lines changed: 47 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@
33
Definition of main class to run Modelica simulations - ModelicaSystem.
44
"""
55

6-
from __future__ import annotations
7-
86
import logging
97
import numbers
108
import os
119
import pathlib
12-
import platform
1310
from typing import Any, Optional
1411
import warnings
1512

@@ -94,7 +91,7 @@ def simulate_cmd( # type: ignore[override]
9491
simargs = {}
9592

9693
if simflags is not None:
97-
simargs_extra = ModelicaSystemCmd.parse_simflags(simflags=simflags)
94+
simargs_extra = parse_simflags(simflags=simflags)
9895
simargs = simargs | simargs_extra
9996

10097
return super().simulate_cmd(
@@ -116,7 +113,7 @@ def simulate( # type: ignore[override]
116113
simargs = {}
117114

118115
if simflags is not None:
119-
simargs_extra = ModelicaSystemCmd.parse_simflags(simflags=simflags)
116+
simargs_extra = parse_simflags(simflags=simflags)
120117
simargs = simargs | simargs_extra
121118

122119
return super().simulate(
@@ -137,7 +134,7 @@ def linearize( # type: ignore[override]
137134
simargs = {}
138135

139136
if simflags is not None:
140-
simargs_extra = ModelicaSystemCmd.parse_simflags(simflags=simflags)
137+
simargs_extra = parse_simflags(simflags=simflags)
141138
simargs = simargs | simargs_extra
142139

143140
return super().linearize(
@@ -372,95 +369,52 @@ class ModelicaSystemDoE(ModelicaDoEOMC):
372369
@depreciated_class(msg="Please use class ModelExecutionCmd instead!")
373370
class ModelicaSystemCmd(ModelExecutionCmd):
374371
"""
375-
Compatibility class; in the new version it is renamed as ModelExecutionCmd.
372+
Compatibility class; not much content.
376373
377-
This class is only defined for the unit tests - it is NOT used within ModelicaSystem of v4.0.0!
374+
Missing definitions:
375+
* get_exe() - see self.definition.cmd_model_executable
376+
* get_cmd() - use self.get_cmd_args() or self.definition().get_cmd()
377+
* run() - use self.definition().run()
378378
"""
379379

380-
def __init__(
381-
self,
382-
runpath: pathlib.Path,
383-
modelname: str,
384-
timeout: float = 10.0,
385-
) -> None:
386-
super().__init__(
387-
runpath=runpath,
388-
timeout=timeout,
389-
cmd_prefix=[],
390-
model_name=modelname,
391-
)
392-
393-
def get_exe(self) -> pathlib.Path:
394-
"""Get the path to the compiled model executable."""
395-
396-
path_run = pathlib.Path(self._runpath)
397-
if platform.system() == "Windows":
398-
path_exe = path_run / f"{self._model_name}.exe"
399-
else:
400-
path_exe = path_run / self._model_name
401-
402-
if not path_exe.exists():
403-
raise ModelicaSystemError(f"Application file path not found: {path_exe}")
404-
405-
return path_exe
406-
407-
def get_cmd(self) -> list:
408-
"""
409-
Get a list with the path to the executable and all command line args.
410-
411-
This can later be used as an argument for subprocess.run().
412-
"""
413-
414-
cmdl = [self.get_exe().as_posix()] + self.get_cmd_args()
415-
416-
return cmdl
417-
418-
def run(self) -> int:
419-
cmd_definition = self.definition()
420-
try:
421-
returncode = cmd_definition.run()
422-
except ModelExecutionException as exc:
423-
raise ModelicaSystemError(f"Cannot execute model: {exc}") from exc
424-
return returncode
425-
426-
@staticmethod
427-
def parse_simflags(simflags: str) -> dict[str, Optional[str | dict[str, Any] | numbers.Number]]:
428-
"""
429-
Parse a simflag definition; this is deprecated!
430380

431-
The return data can be used as input for self.args_set().
432-
"""
433-
warnings.warn(
434-
message="The argument 'simflags' is depreciated and will be removed in future versions; "
435-
"please use 'simargs' instead",
436-
category=DeprecationWarning,
437-
stacklevel=2,
438-
)
381+
def parse_simflags(simflags: str) -> dict[str, Optional[str | dict[str, Any] | numbers.Number]]:
382+
"""
383+
Parse a simflag definition; this is deprecated!
439384
440-
simargs: dict[str, Optional[str | dict[str, Any] | numbers.Number]] = {}
441-
442-
args = [s for s in simflags.split(' ') if s]
443-
for arg in args:
444-
if arg[0] != '-':
445-
raise ModelExecutionException(f"Invalid simulation flag: {arg}")
446-
arg = arg[1:]
447-
parts = arg.split('=')
448-
if len(parts) == 1:
449-
simargs[parts[0]] = None
450-
elif parts[0] == 'override':
451-
override = '='.join(parts[1:])
452-
453-
override_dict = {}
454-
for item in override.split(','):
455-
kv = item.split('=')
456-
if not 0 < len(kv) < 3:
457-
raise ModelExecutionException(f"Invalid value for '-override': {override}")
458-
if kv[0]:
459-
try:
460-
override_dict[kv[0]] = kv[1]
461-
except (KeyError, IndexError) as ex:
462-
raise ModelExecutionException(f"Invalid value for '-override': {override}") from ex
463-
464-
simargs[parts[0]] = override_dict
465-
466-
return simargs
385+
The return data can be used as input for self.args_set().
386+
"""
387+
warnings.warn(
388+
message="The argument 'simflags' is depreciated and will be removed in future versions; "
389+
"please use 'simargs' instead",
390+
category=DeprecationWarning,
391+
stacklevel=2,
392+
)
393+
394+
simargs: dict[str, Optional[str | dict[str, Any] | numbers.Number]] = {}
395+
396+
args = [s for s in simflags.split(' ') if s]
397+
for arg in args:
398+
if arg[0] != '-':
399+
raise ModelExecutionException(f"Invalid simulation flag: {arg}")
400+
arg = arg[1:]
401+
parts = arg.split('=')
402+
if len(parts) == 1:
403+
simargs[parts[0]] = None
404+
elif parts[0] == 'override':
405+
override = '='.join(parts[1:])
406+
407+
override_dict = {}
408+
for item in override.split(','):
409+
kv = item.split('=')
410+
if not 0 < len(kv) < 3:
411+
raise ModelExecutionException(f"Invalid value for '-override': {override}")
412+
if kv[0]:
413+
try:
414+
override_dict[kv[0]] = kv[1]
415+
except (KeyError, IndexError) as ex:
416+
raise ModelExecutionException(f"Invalid value for '-override': {override}") from ex
417+
418+
simargs[parts[0]] = override_dict
419+
420+
return simargs

OMPython/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@
6161
# the imports below are compatibility functionality (OMPython v4.0.0)
6262
from OMPython.ModelicaSystem import (
6363
ModelicaSystem,
64-
ModelicaSystemCmd,
6564
ModelicaSystemDoE,
65+
parse_simflags,
6666
)
6767
from OMPython.OMCSession import (
6868
OMCSessionCmd,
@@ -109,9 +109,9 @@
109109
'OMPathRunnerLocal',
110110
'OMSessionRunner',
111111

112-
'ModelicaSystemCmd',
113112
'ModelicaSystem',
114113
'ModelicaSystemDoE',
114+
'parse_simflags',
115115

116116
'OMCSessionCmd',
117117

tests_v400/test_ModelicaSystemCmd.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ def model_firstorder(tmp_path):
1818
@pytest.fixture
1919
def mscmd_firstorder(model_firstorder):
2020
mod = OMPython.ModelicaSystem(fileName=model_firstorder.as_posix(), modelName="M")
21-
mscmd = OMPython.ModelicaSystemCmd(runpath=mod.getWorkDirectory(), modelname=mod._model_name)
21+
mscmd = OMPython.ModelExecutionCmd(
22+
runpath=mod.getWorkDirectory(),
23+
model_name=mod._model_name,
24+
cmd_prefix=[],
25+
)
2226
return mscmd
2327

2428

@@ -30,10 +34,9 @@ def test_simflags(mscmd_firstorder):
3034
"override": {'b': 2}
3135
})
3236
with pytest.deprecated_call():
33-
mscmd.args_set(args=mscmd.parse_simflags(simflags="-noEventEmit -noRestart -override=a=1,x=3"))
37+
mscmd.args_set(args=OMPython.parse_simflags(simflags="-noEventEmit -noRestart -override=a=1,x=3"))
3438

35-
assert mscmd.get_cmd() == [
36-
mscmd.get_exe().as_posix(),
39+
assert mscmd.get_cmd_args() == [
3740
'-noEventEmit',
3841
'-noRestart',
3942
'-override=a=1,b=2,x=3',
@@ -43,8 +46,7 @@ def test_simflags(mscmd_firstorder):
4346
"override": {'b': None},
4447
})
4548

46-
assert mscmd.get_cmd() == [
47-
mscmd.get_exe().as_posix(),
49+
assert mscmd.get_cmd_args() == [
4850
'-noEventEmit',
4951
'-noRestart',
5052
'-override=a=1,x=3',

0 commit comments

Comments
 (0)