Skip to content

Commit 57d13ba

Browse files
committed
Merge branch 'improve_OMCPath' into ModelicaSystem_relative_path
2 parents b6414b9 + 76cbc37 commit 57d13ba

1 file changed

Lines changed: 36 additions & 14 deletions

File tree

OMPython/OMCSession.py

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import logging
4040
import os
4141
import pathlib
42+
import platform
4243
import psutil
4344
import pyparsing
4445
import re
@@ -273,8 +274,11 @@ def getClassNames(self, className=None, recursive=False, qualified=False, sort=F
273274

274275
class OMCPathReal(pathlib.PurePosixPath):
275276
"""
276-
Implementation of a basic Path object which uses OMC as backend. The connection to OMC is provided via a
277+
Implementation of a basic (PurePosix)Path object which uses OMC as backend. The connection to OMC is provided via a
277278
OMCSessionZMQ session object.
279+
280+
PurePosixPath is selected to cover usage of OMC in docker or via WSL. Usage of specialised function could result in
281+
errors as well as usage on a Windows system due to slightly different definitions (PureWindowsPath).
278282
"""
279283

280284
def __init__(self, *path, session: OMCSessionZMQ) -> None:
@@ -301,6 +305,15 @@ def is_dir(self, *, follow_symlinks=True) -> bool:
301305
"""
302306
return self._session.sendExpression(f'directoryExists("{self.as_posix()}")')
303307

308+
def is_absolute(self):
309+
"""
310+
Check if the path is an absolute path considering the possibility that we are running locally on Windows. This
311+
case needs special handling as the definition of is_absolute() differs.
312+
"""
313+
if isinstance(self._session, OMCProcessLocal) and platform.system() == 'Windows':
314+
return pathlib.PureWindowsPath(self.as_posix()).is_absolute()
315+
return super().is_absolute()
316+
304317
def read_text(self, encoding=None, errors=None, newline=None) -> str:
305318
"""
306319
Read the content of the file represented by this path as text.
@@ -318,10 +331,12 @@ def write_text(self, data: str, encoding=None, errors=None, newline=None):
318331
definitions.
319332
"""
320333
if not isinstance(data, str):
321-
raise TypeError('data must be str, not %s' %
322-
data.__class__.__name__)
334+
raise TypeError(f"data must be str, not {data.__class__.__name__}")
335+
336+
data_omc = data.replace('"', '\\"')
337+
self._session.sendExpression(f'writeFile("{self.as_posix()}", "{data_omc}", false);')
323338

324-
return self._session.sendExpression(f'writeFile("{self.as_posix()}", "{data}", false)')
339+
return len(data)
325340

326341
def mkdir(self, mode=0o777, parents=False, exist_ok=False):
327342
"""
@@ -357,15 +372,20 @@ def resolve(self, strict: bool = False):
357372
raise OMCSessionException(f"Path {self.as_posix()} does not exist!")
358373

359374
if self.is_file():
360-
omcpath = self._omc_resolve(self.parent.as_posix()) / self.name
375+
pathstr_resolved = self._omc_resolve(self.parent.as_posix())
376+
omcpath_resolved = self._session.omcpath(pathstr_resolved) / self.name
361377
elif self.is_dir():
362-
omcpath = self._omc_resolve(self.as_posix())
378+
pathstr_resolved = self._omc_resolve(self.as_posix())
379+
omcpath_resolved = self._session.omcpath(pathstr_resolved)
363380
else:
364381
raise OMCSessionException(f"Path {self.as_posix()} is neither a file nor a directory!")
365382

366-
return omcpath
383+
if not omcpath_resolved.is_file() and not omcpath_resolved.is_dir():
384+
raise OMCSessionException(f"OMCPath resolve failed for {self.as_posix()} - path does not exist!")
385+
386+
return omcpath_resolved
367387

368-
def _omc_resolve(self, pathstr: str):
388+
def _omc_resolve(self, pathstr: str) -> str:
369389
"""
370390
Internal function to resolve the path of the OMCPath object using OMC functions *WITHOUT* changing the cwd
371391
within OMC.
@@ -379,15 +399,10 @@ def _omc_resolve(self, pathstr: str):
379399
result_parts = result.split('\n')
380400
pathstr_resolved = result_parts[1]
381401
pathstr_resolved = pathstr_resolved[1:-1] # remove quotes
382-
383-
omcpath_resolved = self._session.omcpath(pathstr_resolved)
384402
except OMCSessionException as ex:
385403
raise OMCSessionException(f"OMCPath resolve failed for {pathstr}!") from ex
386404

387-
if not omcpath_resolved.is_file() and not omcpath_resolved.is_dir():
388-
raise OMCSessionException(f"OMCPath resolve failed for {pathstr} - path does not exist!")
389-
390-
return omcpath_resolved
405+
return pathstr_resolved
391406

392407
def absolute(self):
393408
"""
@@ -415,6 +430,13 @@ def size(self) -> int:
415430

416431
raise OMCSessionException(f"Error reading file size for path {self.as_posix()}!")
417432

433+
def stat(self):
434+
"""
435+
The function stat() cannot be implemented using OMC.
436+
"""
437+
raise NotImplementedError("The function stat() cannot be implemented using OMC; "
438+
"use size() to get the file size.")
439+
418440

419441
if sys.version_info < (3, 12):
420442

0 commit comments

Comments
 (0)