Skip to content

Commit 8f95985

Browse files
committed
Merge branch 'ModelicaSystemCmd_arg_set' into merge_me
2 parents 1ffbfad + 801685f commit 8f95985

1 file changed

Lines changed: 31 additions & 18 deletions

File tree

OMPython/ModelicaSystem.py

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -131,28 +131,45 @@ def arg_set(self, key: str, val: Optional[str | dict[str, Any]] = None) -> None:
131131
"""
132132
Set one argument for the executable model.
133133
134-
Parameters
135-
----------
136-
key : str
137-
val : str, None
138-
"""
134+
Args:
135+
key: identifier / argument name to be used for the call of the model executable.
136+
val: value for the given key; None for no value and for key == 'override' a dictionary can be used which
137+
indicates variables to override
138+
"""
139+
140+
def override2str(okey: str, oval: Any) -> str:
141+
"""
142+
Convert a value for 'override' to a string taking into account differences between Modelica and Python.
143+
"""
144+
if isinstance(oval, str):
145+
oval_str = f"\"{oval.strip()}\"" # TODO: use shlex.quote()?
146+
elif isinstance(oval, bool):
147+
oval_str = 'true' if oval else 'false'
148+
elif isinstance(oval, numbers.Number):
149+
oval_str = str(oval)
150+
else:
151+
raise ModelicaSystemError(f"Invalid value for override key {okey}: {type(oval)}")
152+
153+
return f"{okey}={oval_str}"
154+
139155
if not isinstance(key, str):
140156
raise ModelicaSystemError(f"Invalid argument key: {repr(key)} (type: {type(key)})")
141157
key = key.strip()
142-
if val is None:
143-
argval = None
144-
elif isinstance(val, str):
145-
argval = val.strip()
146-
elif isinstance(val, numbers.Number):
147-
argval = str(val)
148-
elif key == 'override' and isinstance(val, dict):
158+
159+
if key == 'override' and isinstance(val, dict):
149160
for okey in val:
150-
if not isinstance(okey, str) or not isinstance(val[okey], (str, numbers.Number)):
161+
if not isinstance(okey, str) or not isinstance(val[okey], (str, bool, numbers.Number)):
151162
raise ModelicaSystemError("Invalid argument for 'override': "
152163
f"{repr(okey)} = {repr(val[okey])}")
153164
self._arg_override[okey] = val[okey]
154165

155-
argval = ','.join([f"{okey}={str(self._arg_override[okey])}" for okey in self._arg_override])
166+
argval = ','.join([override2str(okey=okey, oval=oval) for okey, oval in self._arg_override.items()])
167+
elif val is None:
168+
argval = None
169+
elif isinstance(val, str):
170+
argval = val.strip()
171+
elif isinstance(val, numbers.Number):
172+
argval = str(val)
156173
else:
157174
raise ModelicaSystemError(f"Invalid argument value for {repr(key)}: {repr(val)} (type: {type(val)})")
158175

@@ -173,10 +190,6 @@ def arg_get(self, key: str) -> Optional[str | dict]:
173190
def args_set(self, args: dict[str, Optional[str | dict[str, Any]]]) -> None:
174191
"""
175192
Define arguments for the model executable.
176-
177-
Parameters
178-
----------
179-
args : dict[str, Optional[str | dict[str, str]]]
180193
"""
181194
for arg in args:
182195
self.arg_set(key=arg, val=args[arg])

0 commit comments

Comments
 (0)