From 95d9920131022ff52636d7fc99b86f49a0f09c29 Mon Sep 17 00:00:00 2001 From: syntron Date: Fri, 31 Oct 2025 16:55:48 +0100 Subject: [PATCH 1/7] [OMCPath] update docstring --- OMPython/OMCSession.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/OMPython/OMCSession.py b/OMPython/OMCSession.py index 47e2fd74..923eee62 100644 --- a/OMPython/OMCSession.py +++ b/OMPython/OMCSession.py @@ -273,8 +273,11 @@ def getClassNames(self, className=None, recursive=False, qualified=False, sort=F class OMCPathReal(pathlib.PurePosixPath): """ - Implementation of a basic Path object which uses OMC as backend. The connection to OMC is provided via a + Implementation of a basic (PurePosix)Path object which uses OMC as backend. The connection to OMC is provided via a OMCSessionZMQ session object. + + PurePosixPath is selected to cover usage of OMC in docker or via WSL. Usage of specialised function could result in + errors as well as usage on a Windows system due to slightly different definitions (PureWindowsPath). """ def __init__(self, *path, session: OMCSessionZMQ) -> None: From 0cf3810dc2b60d08b703c49d64013ef22bee3e94 Mon Sep 17 00:00:00 2001 From: syntron Date: Fri, 31 Oct 2025 16:56:09 +0100 Subject: [PATCH 2/7] [OMCPath] add definition of is_absolute(); consider Windows systems --- OMPython/OMCSession.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/OMPython/OMCSession.py b/OMPython/OMCSession.py index 923eee62..ce9d198a 100644 --- a/OMPython/OMCSession.py +++ b/OMPython/OMCSession.py @@ -304,6 +304,15 @@ def is_dir(self, *, follow_symlinks=True) -> bool: """ return self._session.sendExpression(f'directoryExists("{self.as_posix()}")') + def is_absolute(self): + """ + Check if the path is an absolute path considering the possibility that we are running locally on Windows. This + case needs special handling as the definition of is_absolute() differs. + """ + if isinstance(self._session, OMCProcessLocal) and platform.system() == 'Windows': + return pathlib.PureWindowsPath(self.as_posix()).is_absolute() + return super().is_absolute() + def read_text(self, encoding=None, errors=None, newline=None) -> str: """ Read the content of the file represented by this path as text. From 2336febe25e0f5e7b6d0594b711a687c28915871 Mon Sep 17 00:00:00 2001 From: syntron Date: Fri, 31 Oct 2025 16:56:51 +0100 Subject: [PATCH 3/7] [OMCPath] improve write_text(); need special handling for double quotes --- OMPython/OMCSession.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/OMPython/OMCSession.py b/OMPython/OMCSession.py index ce9d198a..be3e2b85 100644 --- a/OMPython/OMCSession.py +++ b/OMPython/OMCSession.py @@ -330,10 +330,11 @@ def write_text(self, data: str, encoding=None, errors=None, newline=None): definitions. """ if not isinstance(data, str): - raise TypeError('data must be str, not %s' % - data.__class__.__name__) + raise TypeError(f"data must be str, not {data.__class__.__name__}") - return self._session.sendExpression(f'writeFile("{self.as_posix()}", "{data}", false)') + self._session.sendExpression(f'writeFile("{self.as_posix()}", "{data.replace('"', '\\"')}", false);') + + return len(data) def mkdir(self, mode=0o777, parents=False, exist_ok=False): """ From ef51d2f59ad457e0df4e6e0da6786c882501dceb Mon Sep 17 00:00:00 2001 From: syntron Date: Fri, 31 Oct 2025 16:57:52 +0100 Subject: [PATCH 4/7] [OMCPath] update resolve() --- OMPython/OMCSession.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/OMPython/OMCSession.py b/OMPython/OMCSession.py index be3e2b85..5b11f7d4 100644 --- a/OMPython/OMCSession.py +++ b/OMPython/OMCSession.py @@ -370,15 +370,20 @@ def resolve(self, strict: bool = False): raise OMCSessionException(f"Path {self.as_posix()} does not exist!") if self.is_file(): - omcpath = self._omc_resolve(self.parent.as_posix()) / self.name + pathstr_resolved = self._omc_resolve(self.parent.as_posix()) + omcpath_resolved = self._session.omcpath(pathstr_resolved) / self.name elif self.is_dir(): - omcpath = self._omc_resolve(self.as_posix()) + pathstr_resolved = self._omc_resolve(self.as_posix()) + omcpath_resolved = self._session.omcpath(pathstr_resolved) else: raise OMCSessionException(f"Path {self.as_posix()} is neither a file nor a directory!") - return omcpath + if not omcpath_resolved.is_file() and not omcpath_resolved.is_dir(): + raise OMCSessionException(f"OMCPath resolve failed for {self.as_posix()} - path does not exist!") - def _omc_resolve(self, pathstr: str): + return omcpath_resolved + + def _omc_resolve(self, pathstr: str) -> str: """ Internal function to resolve the path of the OMCPath object using OMC functions *WITHOUT* changing the cwd within OMC. @@ -392,15 +397,10 @@ def _omc_resolve(self, pathstr: str): result_parts = result.split('\n') pathstr_resolved = result_parts[1] pathstr_resolved = pathstr_resolved[1:-1] # remove quotes - - omcpath_resolved = self._session.omcpath(pathstr_resolved) except OMCSessionException as ex: raise OMCSessionException(f"OMCPath resolve failed for {pathstr}!") from ex - if not omcpath_resolved.is_file() and not omcpath_resolved.is_dir(): - raise OMCSessionException(f"OMCPath resolve failed for {pathstr} - path does not exist!") - - return omcpath_resolved + return pathstr_resolved def absolute(self): """ From 63116ee6126e6ca96f8f33d061dc68df7d66fb92 Mon Sep 17 00:00:00 2001 From: syntron Date: Fri, 31 Oct 2025 16:58:12 +0100 Subject: [PATCH 5/7] [OMCPath] prevent usage of stat() - not implemented using OMC --- OMPython/OMCSession.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/OMPython/OMCSession.py b/OMPython/OMCSession.py index 5b11f7d4..1fc23557 100644 --- a/OMPython/OMCSession.py +++ b/OMPython/OMCSession.py @@ -428,6 +428,13 @@ def size(self) -> int: raise OMCSessionException(f"Error reading file size for path {self.as_posix()}!") + def stat(self): + """ + The function stat() cannot be implemented using OMC. + """ + raise NotImplementedError("The function stat() cannot be implemented using OMC; " + "use size() to get the file size.") + if sys.version_info < (3, 12): From 19d0443de5ef1c8f1692609f9bd9035c3325b8ed Mon Sep 17 00:00:00 2001 From: syntron Date: Fri, 31 Oct 2025 19:50:17 +0100 Subject: [PATCH 6/7] add missing import --- OMPython/OMCSession.py | 1 + 1 file changed, 1 insertion(+) diff --git a/OMPython/OMCSession.py b/OMPython/OMCSession.py index 1fc23557..6b6fca0b 100644 --- a/OMPython/OMCSession.py +++ b/OMPython/OMCSession.py @@ -39,6 +39,7 @@ import logging import os import pathlib +import platform import psutil import pyparsing import re From 76cbc37cde0c8c1f4e766ca6350694be523d9811 Mon Sep 17 00:00:00 2001 From: syntron Date: Fri, 31 Oct 2025 19:22:26 +0100 Subject: [PATCH 7/7] [OMCPathReal] fix flake8 OMPython/OMCSession.py:339:110: E999 SyntaxError: f-string: unmatched '(' --- OMPython/OMCSession.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/OMPython/OMCSession.py b/OMPython/OMCSession.py index 6b6fca0b..eefc284e 100644 --- a/OMPython/OMCSession.py +++ b/OMPython/OMCSession.py @@ -333,7 +333,8 @@ def write_text(self, data: str, encoding=None, errors=None, newline=None): if not isinstance(data, str): raise TypeError(f"data must be str, not {data.__class__.__name__}") - self._session.sendExpression(f'writeFile("{self.as_posix()}", "{data.replace('"', '\\"')}", false);') + data_omc = data.replace('"', '\\"') + self._session.sendExpression(f'writeFile("{self.as_posix()}", "{data_omc}", false);') return len(data)