From 794d871a79c1a17b3b5e1239a424831c0a62faae Mon Sep 17 00:00:00 2001 From: syntron Date: Fri, 31 Oct 2025 16:55:48 +0100 Subject: [PATCH 1/6] [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 a7c67841..57cc9aca 100644 --- a/OMPython/OMCSession.py +++ b/OMPython/OMCSession.py @@ -276,8 +276,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 c952f0aefc41cfc6f8ee4e6c2107fc49836ff188 Mon Sep 17 00:00:00 2001 From: syntron Date: Fri, 31 Oct 2025 16:56:09 +0100 Subject: [PATCH 2/6] [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 57cc9aca..01ae8949 100644 --- a/OMPython/OMCSession.py +++ b/OMPython/OMCSession.py @@ -307,6 +307,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 a832bb887448b53a529c738537a8324e357059eb Mon Sep 17 00:00:00 2001 From: syntron Date: Fri, 31 Oct 2025 16:56:51 +0100 Subject: [PATCH 3/6] [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 01ae8949..8fba08d7 100644 --- a/OMPython/OMCSession.py +++ b/OMPython/OMCSession.py @@ -333,10 +333,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 c94ea4960aa0372f3c763ba255c3711cb34fbbea Mon Sep 17 00:00:00 2001 From: syntron Date: Fri, 31 Oct 2025 16:57:52 +0100 Subject: [PATCH 4/6] [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 8fba08d7..72519af1 100644 --- a/OMPython/OMCSession.py +++ b/OMPython/OMCSession.py @@ -373,15 +373,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. @@ -395,15 +400,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 069784016ef9c32a9e3f7ee7b5c924e68ceb69ff Mon Sep 17 00:00:00 2001 From: syntron Date: Fri, 31 Oct 2025 16:58:12 +0100 Subject: [PATCH 5/6] [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 72519af1..ef8f875b 100644 --- a/OMPython/OMCSession.py +++ b/OMPython/OMCSession.py @@ -431,6 +431,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 eea89758c7886742e5a6204dcc4d8cceca548a3b Mon Sep 17 00:00:00 2001 From: syntron Date: Fri, 31 Oct 2025 19:22:26 +0100 Subject: [PATCH 6/6] [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 ef8f875b..c0a57357 100644 --- a/OMPython/OMCSession.py +++ b/OMPython/OMCSession.py @@ -335,7 +335,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)