Skip to content

Commit d368e64

Browse files
committed
[OMCSession] move OMCPathRunner* into the if clause
1 parent 4f35230 commit d368e64

1 file changed

Lines changed: 90 additions & 95 deletions

File tree

OMPython/OMCSession.py

Lines changed: 90 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@ class OMPathCompatibilityWindows(pathlib.WindowsPath, OMPathCompatibility):
289289

290290
OMPathABC = OMPathCompatibility
291291
OMCPath = OMPathCompatibility
292+
OMPathRunnerABC = OMPathCompatibility
293+
OMPathRunnerLocal = OMPathCompatibility
292294
else:
293295
class OMPathABC(pathlib.PurePosixPath, metaclass=abc.ABCMeta):
294296
"""
@@ -514,7 +516,95 @@ def size(self) -> int:
514516

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

519+
class OMPathRunnerABC(OMPathABC, metaclass=abc.ABCMeta):
520+
"""
521+
Base function for OMPath definitions *without* OMC server
522+
"""
523+
524+
def _path(self) -> pathlib.Path:
525+
return pathlib.Path(self.as_posix())
526+
527+
class _OMPathRunnerLocal(OMPathRunnerABC):
528+
"""
529+
Implementation of OMPathBase which does not use the session data at all. Thus, this implementation can run
530+
locally without any usage of OMC.
531+
532+
This class is based on OMPathBase and, therefore, on pathlib.PurePosixPath. This is working well, but it is not
533+
the correct implementation on Windows systems. To get a valid Windows representation of the path, use the
534+
conversion via pathlib.Path(<OMCPathDummy>.as_posix()).
535+
"""
536+
537+
def is_file(self) -> bool:
538+
"""
539+
Check if the path is a regular file.
540+
"""
541+
return self._path().is_file()
542+
543+
def is_dir(self) -> bool:
544+
"""
545+
Check if the path is a directory.
546+
"""
547+
return self._path().is_dir()
548+
549+
def is_absolute(self):
550+
"""
551+
Check if the path is an absolute path.
552+
"""
553+
return self._path().is_absolute()
554+
555+
def read_text(self) -> str:
556+
"""
557+
Read the content of the file represented by this path as text.
558+
"""
559+
return self._path().read_text(encoding='utf-8')
560+
561+
def write_text(self, data: str):
562+
"""
563+
Write text data to the file represented by this path.
564+
"""
565+
return self._path().write_text(data=data, encoding='utf-8')
566+
567+
def mkdir(self, parents: bool = True, exist_ok: bool = False):
568+
"""
569+
Create a directory at the path represented by this class.
570+
571+
The argument parents with default value True exists to ensure compatibility with the fallback solution for
572+
Python < 3.12. In this case, pathlib.Path is used directly and this option ensures, that missing parent
573+
directories are also created.
574+
"""
575+
return self._path().mkdir(parents=parents, exist_ok=exist_ok)
576+
577+
def cwd(self):
578+
"""
579+
Returns the current working directory as an OMPathBase object.
580+
"""
581+
return self._path().cwd()
582+
583+
def unlink(self, missing_ok: bool = False) -> None:
584+
"""
585+
Unlink (delete) the file or directory represented by this path.
586+
"""
587+
return self._path().unlink(missing_ok=missing_ok)
588+
589+
def resolve(self, strict: bool = False):
590+
"""
591+
Resolve the path to an absolute path. This is done based on available OMC functions.
592+
"""
593+
path_resolved = self._path().resolve(strict=strict)
594+
return type(self)(path_resolved, session=self._session)
595+
596+
def size(self) -> int:
597+
"""
598+
Get the size of the file in bytes - implementation baseon on pathlib.Path.
599+
"""
600+
if not self.is_file():
601+
raise OMCSessionException(f"Path {self.as_posix()} is not a file!")
602+
603+
path = self._path()
604+
return path.stat().st_size
605+
517606
OMCPath = _OMCPath
607+
OMPathRunnerLocal = _OMPathRunnerLocal
518608

519609

520610
class ModelExecutionException(Exception):
@@ -1737,101 +1827,6 @@ def _omc_port_get(self) -> str:
17371827
return port
17381828

17391829

1740-
class OMPathRunnerABC(OMPathABC, metaclass=abc.ABCMeta):
1741-
"""
1742-
Base function for OMPath definitions *without* OMC server
1743-
"""
1744-
1745-
def _path(self) -> pathlib.Path:
1746-
return pathlib.Path(self.as_posix())
1747-
1748-
1749-
class _OMPathRunnerLocal(OMPathRunnerABC):
1750-
"""
1751-
Implementation of OMPathBase which does not use the session data at all. Thus, this implementation can run locally
1752-
without any usage of OMC.
1753-
1754-
This class is based on OMPathBase and, therefore, on pathlib.PurePosixPath. This is working well, but it is not the
1755-
correct implementation on Windows systems. To get a valid Windows representation of the path, use the conversion
1756-
via pathlib.Path(<OMCPathDummy>.as_posix()).
1757-
"""
1758-
1759-
def is_file(self) -> bool:
1760-
"""
1761-
Check if the path is a regular file.
1762-
"""
1763-
return self._path().is_file()
1764-
1765-
def is_dir(self) -> bool:
1766-
"""
1767-
Check if the path is a directory.
1768-
"""
1769-
return self._path().is_dir()
1770-
1771-
def is_absolute(self):
1772-
"""
1773-
Check if the path is an absolute path.
1774-
"""
1775-
return self._path().is_absolute()
1776-
1777-
def read_text(self) -> str:
1778-
"""
1779-
Read the content of the file represented by this path as text.
1780-
"""
1781-
return self._path().read_text(encoding='utf-8')
1782-
1783-
def write_text(self, data: str):
1784-
"""
1785-
Write text data to the file represented by this path.
1786-
"""
1787-
return self._path().write_text(data=data, encoding='utf-8')
1788-
1789-
def mkdir(self, parents: bool = True, exist_ok: bool = False):
1790-
"""
1791-
Create a directory at the path represented by this class.
1792-
1793-
The argument parents with default value True exists to ensure compatibility with the fallback solution for
1794-
Python < 3.12. In this case, pathlib.Path is used directly and this option ensures, that missing parent
1795-
directories are also created.
1796-
"""
1797-
return self._path().mkdir(parents=parents, exist_ok=exist_ok)
1798-
1799-
def cwd(self):
1800-
"""
1801-
Returns the current working directory as an OMPathBase object.
1802-
"""
1803-
return self._path().cwd()
1804-
1805-
def unlink(self, missing_ok: bool = False) -> None:
1806-
"""
1807-
Unlink (delete) the file or directory represented by this path.
1808-
"""
1809-
return self._path().unlink(missing_ok=missing_ok)
1810-
1811-
def resolve(self, strict: bool = False):
1812-
"""
1813-
Resolve the path to an absolute path. This is done based on available OMC functions.
1814-
"""
1815-
path_resolved = self._path().resolve(strict=strict)
1816-
return type(self)(path_resolved, session=self._session)
1817-
1818-
def size(self) -> int:
1819-
"""
1820-
Get the size of the file in bytes - implementation baseon on pathlib.Path.
1821-
"""
1822-
if not self.is_file():
1823-
raise OMCSessionException(f"Path {self.as_posix()} is not a file!")
1824-
1825-
path = self._path()
1826-
return path.stat().st_size
1827-
1828-
1829-
if sys.version_info < (3, 12):
1830-
OMPathRunnerLocal = OMPathCompatibility
1831-
else:
1832-
OMPathRunnerLocal = _OMPathRunnerLocal # OMPathRunnerLocal
1833-
1834-
18351830
class OMSessionRunner(OMSessionABC):
18361831
"""
18371832
Implementation based on OMSessionABC without any use of an OMC server.

0 commit comments

Comments
 (0)