Skip to content

Commit afdaa95

Browse files
committed
rename_party-test01(all) = changes
1 parent 1472540 commit afdaa95

3 files changed

Lines changed: 64 additions & 57 deletions

File tree

OMPython/ModelicaSystem.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -142,31 +142,31 @@ def arg_set(
142142
"""
143143

144144
def override2str(
145-
key: str,
146-
val: str | bool | numbers.Number,
145+
orkey: str,
146+
orval: str | bool | numbers.Number,
147147
) -> str:
148148
"""
149149
Convert a value for 'override' to a string taking into account differences between Modelica and Python.
150150
"""
151151
# check oval for any string representations of numbers (or bool) and convert these to Python representations
152-
if isinstance(val, str):
152+
if isinstance(orval, str):
153153
try:
154-
val_evaluated = ast.literal_eval(val)
154+
val_evaluated = ast.literal_eval(orval)
155155
if isinstance(val_evaluated, (numbers.Number, bool)):
156-
val = val_evaluated
156+
orval = val_evaluated
157157
except (ValueError, SyntaxError):
158158
pass
159159

160-
if isinstance(val, str):
161-
val_str = val.strip()
162-
elif isinstance(val, bool):
163-
val_str = 'true' if val else 'false'
164-
elif isinstance(val, numbers.Number):
165-
val_str = str(val)
160+
if isinstance(orval, str):
161+
val_str = orval.strip()
162+
elif isinstance(orval, bool):
163+
val_str = 'true' if orval else 'false'
164+
elif isinstance(orval, numbers.Number):
165+
val_str = str(orval)
166166
else:
167-
raise ModelExecutionException(f"Invalid value for override key {key}: {type(val)}")
167+
raise ModelExecutionException(f"Invalid value for override key {orkey}: {type(orval)}")
168168

169-
return f"{key}={val_str}"
169+
return f"{orkey}={val_str}"
170170

171171
if not isinstance(key, str):
172172
raise ModelExecutionException(f"Invalid argument key: {repr(key)} (type: {type(key)})")
@@ -195,7 +195,7 @@ def override2str(
195195
f"(was: {repr(self._arg_override[okey])})")
196196

197197
if oval is not None:
198-
self._arg_override[okey] = override2str(key=okey, val=oval)
198+
self._arg_override[okey] = override2str(orkey=okey, orval=oval)
199199

200200
argval = ','.join(sorted(self._arg_override.values()))
201201
elif val is None:
@@ -2822,7 +2822,9 @@ def _prepare_structure_parameters(
28222822

28232823

28242824
class ModelicaSystemCmd(ModelExecutionCmd):
2825-
# TODO: docstring
2825+
"""
2826+
Compatibility class; in the new version it is renamed as MOdelExecutionCmd.
2827+
"""
28262828

28272829
def __init__(
28282830
self,

OMPython/OMCSession.py

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def wait(self, timeout):
5959
pass
6060

6161

62+
# TODO: rename to OMSessionException
6263
class OMCSessionException(Exception):
6364
"""
6465
Exception which is raised by any OMC* class.
@@ -71,6 +72,13 @@ class OMCSessionCmd:
7172
"""
7273

7374
def __init__(self, session: OMSessionABC, readonly: bool = False):
75+
warnings.warn(
76+
message="The class OMCSessionCMD is depreciated and will be removed in future versions; "
77+
"please use OMCSession*.sendExpression(...) instead!",
78+
category=DeprecationWarning,
79+
stacklevel=2,
80+
)
81+
7482
if not isinstance(session, OMSessionABC):
7583
raise OMCSessionException("Invalid OMC process definition!")
7684
self._session = session
@@ -253,7 +261,7 @@ def getClassNames(self, className=None, recursive=False, qualified=False, sort=F
253261
# conditions. This is also the reason for OMPathABC, a simple base class to be used in ModelicaSystem* classes.
254262
# Reason: before Python 3.12, pathlib.PurePosixPath can not be derived from; therefore, OMPathABC is not possible
255263
if sys.version_info < (3, 12):
256-
class OMPathCompatibility(pathlib.Path):
264+
class _OMPathCompatibility(pathlib.Path):
257265
"""
258266
Compatibility class for OMPathABC in Python < 3.12. This allows to run all code which uses OMPathABC (mainly
259267
ModelicaSystem) on these Python versions. There are remaining limitation as only local execution is possible.
@@ -264,8 +272,8 @@ def __new__(cls, *args, **kwargs):
264272
logger.warning("Python < 3.12 - using a version of class OMCPath "
265273
"based on pathlib.Path for local usage only.")
266274

267-
if cls is OMPathCompatibility:
268-
cls = OMPathCompatibilityWindows if os.name == 'nt' else OMPathCompatibilityPosix
275+
if cls is _OMPathCompatibility:
276+
cls = _OMPathCompatibilityWindows if os.name == 'nt' else _OMPathCompatibilityPosix
269277
self = cls._from_parts(args)
270278
if not self._flavour.is_supported:
271279
raise NotImplementedError(f"cannot instantiate {cls.__name__} on your system")
@@ -277,22 +285,20 @@ def size(self) -> int:
277285
"""
278286
return self.stat().st_size
279287

280-
class OMPathCompatibilityPosix(pathlib.PosixPath, OMPathCompatibility):
288+
class _OMPathCompatibilityPosix(pathlib.PosixPath, _OMPathCompatibility):
281289
"""
282290
Compatibility class for OMCPath on Posix systems (Python < 3.12)
283291
"""
284292

285-
class OMPathCompatibilityWindows(pathlib.WindowsPath, OMPathCompatibility):
293+
class _OMPathCompatibilityWindows(pathlib.WindowsPath, _OMPathCompatibility):
286294
"""
287295
Compatibility class for OMCPath on Windows systems (Python < 3.12)
288296
"""
289297

290-
OMPathABC = OMPathCompatibility
291-
OMCPath = OMPathCompatibility
292-
OMPathRunnerABC = OMPathCompatibility
293-
OMPathRunnerLocal = OMPathCompatibility
294-
OMPathRunnerBash = OMPathCompatibility
295-
298+
OMPathABC = _OMPathCompatibility
299+
OMPathRunnerABC = _OMPathCompatibility
300+
OMPathRunnerLocal = _OMPathCompatibility
301+
OMPathRunnerBash = _OMPathCompatibility
296302
else:
297303
class OMPathABC(pathlib.PurePosixPath, metaclass=abc.ABCMeta):
298304
"""
@@ -427,7 +433,7 @@ def is_absolute(self) -> bool:
427433
"""
428434
Check if the path is an absolute path. Special handling to differentiate Windows and Posix definitions.
429435
"""
430-
if isinstance(self._session, OMCSessionLocal) and platform.system() == 'Windows':
436+
if self._session.model_execution_windows and self._session.model_execution_local:
431437
return pathlib.PureWindowsPath(self.as_posix()).is_absolute()
432438
return pathlib.PurePosixPath(self.as_posix()).is_absolute()
433439

@@ -618,7 +624,7 @@ def resolve(self, strict: bool = False) -> OMPathABC:
618624

619625
def size(self) -> int:
620626
"""
621-
Get the size of the file in bytes - implementation baseon on pathlib.Path.
627+
Get the size of the file in bytes - implementation based on pathlib.Path.
622628
"""
623629
if not self.is_file():
624630
raise OMCSessionException(f"Path {self.as_posix()} is not a file!")
@@ -776,7 +782,7 @@ def resolve(self, strict: bool = False) -> OMPathABC:
776782

777783
def size(self) -> int:
778784
"""
779-
Get the size of the file in bytes - implementation baseon on pathlib.Path.
785+
Get the size of the file in bytes - implementation based on pathlib.Path.
780786
"""
781787
if not self.is_file():
782788
raise OMCSessionException(f"Path {self.as_posix()} is not a file!")
@@ -790,7 +796,7 @@ def size(self) -> int:
790796
try:
791797
return int(stdout)
792798
except ValueError as exc:
793-
raise OSError(f"Invalid return value for filesize ({self.as_posix()}): {stdout}") from exc
799+
raise OSError(f"Invalid return value for file size ({self.as_posix()}): {stdout}") from exc
794800
else:
795801
raise OSError(f"Cannot get size for file {self.as_posix()}")
796802

@@ -1225,26 +1231,6 @@ def omcpath_tempdir(self, tempdir_base: Optional[OMPathABC] = None) -> OMPathABC
12251231

12261232
return self._tempdir(tempdir_base=tempdir_base)
12271233

1228-
@staticmethod
1229-
def _tempdir(tempdir_base: OMPathABC) -> OMPathABC:
1230-
names = [str(uuid.uuid4()) for _ in range(100)]
1231-
1232-
tempdir: Optional[OMPathABC] = None
1233-
for name in names:
1234-
# create a unique temporary directory name
1235-
tempdir = tempdir_base / name
1236-
1237-
if tempdir.exists():
1238-
continue
1239-
1240-
tempdir.mkdir(parents=True, exist_ok=False)
1241-
break
1242-
1243-
if tempdir is None or not tempdir.is_dir():
1244-
raise OMCSessionException("Cannot create a temporary directory!")
1245-
1246-
return tempdir
1247-
12481234
def execute(self, command: str):
12491235
warnings.warn(
12501236
message="This function is depreciated and will be removed in future versions; "
@@ -1259,7 +1245,7 @@ def sendExpression(self, expr: str, parsed: bool = True) -> Any:
12591245
"""
12601246
Send an expression to the OMC server and return the result.
12611247
1262-
The complete error handling of the OMC result is done within this method using '"getMessagesStringInternal()'.
1248+
The complete error handling of the OMC result is done within this method using 'getMessagesStringInternal()'.
12631249
Caller should only check for OMCSessionException.
12641250
"""
12651251

@@ -1544,6 +1530,8 @@ def __init__(
15441530
raise OMCSessionException("Invalid definition of the OMC process!")
15451531
self.omc_process = omc_process
15461532

1533+
super().__init__(timeout=timeout)
1534+
15471535
def __del__(self):
15481536
if hasattr(self, 'omc_process'):
15491537
del self.omc_process
@@ -2086,16 +2074,16 @@ def _omc_port_get(self) -> str:
20862074
return port
20872075

20882076

2089-
class OMSessionRunner(OMSessionABC):
2077+
class OMSessionRunnerABC(OMSessionABC, metaclass=abc.ABCMeta):
20902078
"""
20912079
Implementation based on OMSessionABC without any use of an OMC server.
20922080
"""
20932081

20942082
def __init__(
20952083
self,
2084+
ompath_runner: Type[OMPathRunnerABC],
20962085
timeout: float = 10.0,
20972086
version: str = "1.27.0",
2098-
ompath_runner: Type[OMPathRunnerABC] = OMPathRunnerLocal,
20992087
cmd_prefix: Optional[list[str]] = None,
21002088
model_execution_local: bool = True,
21012089
) -> None:
@@ -2110,8 +2098,27 @@ def __init__(
21102098
if cmd_prefix is not None:
21112099
self._cmd_prefix = cmd_prefix
21122100

2113-
# TODO: some checking?!
2114-
# if ompath_runner == Type[OMPathRunnerBash]:
2101+
2102+
class OMSessionRunner(OMSessionRunnerABC):
2103+
"""
2104+
Implementation based on OMSessionABC without any use of an OMC server.
2105+
"""
2106+
2107+
def __init__(
2108+
self,
2109+
ompath_runner: Type[OMPathRunnerABC] = OMPathRunnerLocal,
2110+
timeout: float = 10.0,
2111+
version: str = "1.27.0",
2112+
cmd_prefix: Optional[list[str]] = None,
2113+
model_execution_local: bool = True,
2114+
) -> None:
2115+
super().__init__(
2116+
ompath_runner=ompath_runner,
2117+
timeout=timeout,
2118+
version=version,
2119+
cmd_prefix=cmd_prefix,
2120+
model_execution_local=model_execution_local,
2121+
)
21152122

21162123
def __post_init__(self) -> None:
21172124
"""

OMPython/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@
7878

7979
'OMSessionRunner',
8080

81-
'OMCSessionABC',
82-
8381
'doe_get_solutions',
8482

8583
'OMCSessionCmd',

0 commit comments

Comments
 (0)