Skip to content

Commit 70bf451

Browse files
committed
[OMCPath] add copy()
1 parent 02b2805 commit 70bf451

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

OMPython/OMCSession.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,25 @@ def size(self) -> int:
418418

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

421+
def copy(self, dst: OMCPath, overwrite_ok: bool = False) -> OMCPath:
422+
"""
423+
Copy a (binary) file.
424+
"""
425+
if not isinstance(dst, OMCPath):
426+
raise OMCSessionException("Destination must be an OMCPath object!")
427+
428+
if dst.is_dir():
429+
dst /= self.name
430+
431+
if dst.is_file() and not overwrite_ok:
432+
raise OMCSessionException(f"Not overwriting file {dst.as_posix()} "
433+
"- use overwrite_ok=True to force the operation!")
434+
435+
if not self._session.sendExpression(f'copy("{self.as_posix()}", "{dst.as_posix()}")'):
436+
raise OMCSessionException(f"Could not copy {self.as_posix()} to {dst.as_posix()}!")
437+
438+
return OMCPath(dst.as_posix(), session=self._session)
439+
421440

422441
if sys.version_info < (3, 12):
423442

@@ -447,6 +466,27 @@ def size(self) -> int:
447466
"""
448467
return self.stat().st_size
449468

469+
def copy(self, dst: OMCPath, overwrite_ok: bool = False) -> bool:
470+
"""
471+
Copy a (binary) file.
472+
473+
TODO: Python 3.14 will support copy() nativley
474+
see: https://docs.python.org/3.14/library/pathlib.html#pathlib.Path.copy
475+
"""
476+
if not isinstance(dst, OMCPath):
477+
raise OMCSessionException("Destination must be an OMCPath object!")
478+
479+
if dst.is_dir():
480+
dst /= self.name
481+
482+
if dst.is_file() and not overwrite_ok:
483+
raise OMCSessionException(f"Not overwriting file {dst.as_posix()} "
484+
"- use overwrite_ok=True to force the operation!")
485+
486+
dst.write_bytes(self.read_bytes())
487+
488+
return dst.is_file()
489+
450490
class OMCPathCompatibilityPosix(pathlib.PosixPath, OMCPathCompatibility):
451491
pass
452492

0 commit comments

Comments
 (0)