77
88import abc
99import logging
10- import os
1110import pathlib
1211import platform
13- import sys
1412from typing import Any , Optional
1513import uuid
1614
@@ -26,151 +24,110 @@ class OMSessionException(Exception):
2624 """
2725
2826
29- # due to the compatibility layer to Python < 3.12, the OM(C)Path classes must be hidden behind the following if
30- # conditions. This is also the reason for OMPathABC, a simple base class to be used in ModelicaSystem* classes.
31- # Reason: before Python 3.12, pathlib.PurePosixPath can not be derived from; therefore, OMPathABC is not possible
32- if sys .version_info < (3 , 12 ):
33- class _OMPathCompatibility (pathlib .Path ):
27+ class OMPathABC (pathlib .PurePosixPath , metaclass = abc .ABCMeta ):
28+ """
29+ Implementation of a basic (PurePosix)Path object to be used within OMPython. The derived classes can use OMC as
30+ backend and - thus - work on different configurations like docker or WSL. The connection to OMC is provided via
31+ an instances of classes derived from BaseSession.
32+
33+ PurePosixPath is selected as it covers all but Windows systems (Linux, docker, WSL). However, the code is
34+ written such that possible Windows system are taken into account. Nevertheless, the overall functionality is
35+ limited compared to standard pathlib.Path objects.
36+ """
37+
38+ def __init__ (self , * path , session : OMSessionABC ) -> None :
39+ super ().__init__ (* path )
40+ self ._session = session
41+
42+ def get_session (self ) -> OMSessionABC :
43+ """
44+ Get session definition used for this instance of OMPath.
45+ """
46+ return self ._session
47+
48+ def with_segments (self , * pathsegments ) -> OMPathABC :
3449 """
35- Compatibility class for OMPathABC in Python < 3.12. This allows to run all code which uses OMPathABC (mainly
36- ModelicaSystem) on these Python versions. There are remaining limitation as only local execution is possible.
50+ Create a new OMCPath object with the given path segments.
51+
52+ The original definition of Path is overridden to ensure the session data is set.
3753 """
54+ return type (self )(* pathsegments , session = self ._session )
3855
39- # modified copy of pathlib.Path.__new__() definition
40- def __new__ (cls , * args , ** kwargs ):
41- logger .warning ("Python < 3.12 - using a version of class OMCPath "
42- "based on pathlib.Path for local usage only." )
56+ @abc .abstractmethod
57+ def is_file (self , * , follow_symlinks = True ) -> bool :
58+ """
59+ Check if the path is a regular file.
60+ """
4361
44- if cls is _OMPathCompatibility :
45- cls = _OMPathCompatibilityWindows if os .name == 'nt' else _OMPathCompatibilityPosix
46- self = cls ._from_parts (args )
47- if not self ._flavour .is_supported :
48- raise NotImplementedError (f"cannot instantiate { cls .__name__ } on your system" )
49- return self
62+ @abc .abstractmethod
63+ def is_dir (self , * , follow_symlinks : bool = True ) -> bool :
64+ """
65+ Check if the path is a directory.
66+ """
5067
51- def size ( self ) -> int :
52- """
53- Needed compatibility function to have the same interface as OMCPathReal
54- """
55- return self . stat (). st_size
68+ @ abc . abstractmethod
69+ def is_absolute ( self ) -> bool :
70+ """
71+ Check if the path is an absolute path.
72+ """
5673
57- class _OMPathCompatibilityPosix (pathlib .PosixPath , _OMPathCompatibility ):
74+ @abc .abstractmethod
75+ def read_text (self , encoding = None , errors = None , newline = None ) -> str :
5876 """
59- Compatibility class for OMCPath on Posix systems (Python < 3.12)
77+ Read the content of the file represented by this path as text.
6078 """
6179
62- class _OMPathCompatibilityWindows (pathlib .WindowsPath , _OMPathCompatibility ):
80+ @abc .abstractmethod
81+ def write_text (self , data : str , encoding = None , errors = None , newline = None ) -> int :
6382 """
64- Compatibility class for OMCPath on Windows systems (Python < 3.12)
83+ Write text data to the file represented by this path.
84+ """
85+
86+ @abc .abstractmethod
87+ def mkdir (self , mode = 0o777 , parents : bool = False , exist_ok : bool = False ) -> None :
6588 """
89+ Create a directory at the path represented by this class.
6690
67- OMPathABC = _OMPathCompatibility
68-
69- else :
70- class OMPathABC (pathlib .PurePosixPath , metaclass = abc .ABCMeta ):
71- """
72- Implementation of a basic (PurePosix)Path object to be used within OMPython. The derived classes can use OMC as
73- backend and - thus - work on different configurations like docker or WSL. The connection to OMC is provided via
74- an instances of classes derived from BaseSession.
75-
76- PurePosixPath is selected as it covers all but Windows systems (Linux, docker, WSL). However, the code is
77- written such that possible Windows system are taken into account. Nevertheless, the overall functionality is
78- limited compared to standard pathlib.Path objects.
79- """
80-
81- def __init__ (self , * path , session : OMSessionABC ) -> None :
82- super ().__init__ (* path )
83- self ._session = session
84-
85- def get_session (self ) -> OMSessionABC :
86- """
87- Get session definition used for this instance of OMPath.
88- """
89- return self ._session
90-
91- def with_segments (self , * pathsegments ) -> OMPathABC :
92- """
93- Create a new OMCPath object with the given path segments.
94-
95- The original definition of Path is overridden to ensure the session data is set.
96- """
97- return type (self )(* pathsegments , session = self ._session )
98-
99- @abc .abstractmethod
100- def is_file (self , * , follow_symlinks = True ) -> bool :
101- """
102- Check if the path is a regular file.
103- """
104-
105- @abc .abstractmethod
106- def is_dir (self , * , follow_symlinks : bool = True ) -> bool :
107- """
108- Check if the path is a directory.
109- """
110-
111- @abc .abstractmethod
112- def is_absolute (self ) -> bool :
113- """
114- Check if the path is an absolute path.
115- """
116-
117- @abc .abstractmethod
118- def read_text (self , encoding = None , errors = None , newline = None ) -> str :
119- """
120- Read the content of the file represented by this path as text.
121- """
122-
123- @abc .abstractmethod
124- def write_text (self , data : str , encoding = None , errors = None , newline = None ) -> int :
125- """
126- Write text data to the file represented by this path.
127- """
128-
129- @abc .abstractmethod
130- def mkdir (self , mode = 0o777 , parents : bool = False , exist_ok : bool = False ) -> None :
131- """
132- Create a directory at the path represented by this class.
133-
134- The argument parents with default value True exists to ensure compatibility with the fallback solution for
135- Python < 3.12. In this case, pathlib.Path is used directly and this option ensures, that missing parent
136- directories are also created.
137- """
138-
139- @abc .abstractmethod
140- def cwd (self ) -> OMPathABC : # pylint: disable=W0221 # is @classmethod in the original; see pathlib.PathBase
141- """
142- Returns the current working directory as an OMPathABC object.
143- """
144-
145- @abc .abstractmethod
146- def unlink (self , missing_ok : bool = False ) -> None :
147- """
148- Unlink (delete) the file or directory represented by this path.
149- """
150-
151- @abc .abstractmethod
152- def resolve (self , strict : bool = False ) -> OMPathABC :
153- """
154- Resolve the path to an absolute path.
155- """
156-
157- def absolute (self ) -> OMPathABC :
158- """
159- Resolve the path to an absolute path. Just a wrapper for resolve().
160- """
161- return self .resolve ()
162-
163- def exists (self ) -> bool :
164- """
165- Semi replacement for pathlib.Path.exists().
166- """
167- return self .is_file () or self .is_dir ()
168-
169- @abc .abstractmethod
170- def size (self ) -> int :
171- """
172- Get the size of the file in bytes - this is an extra function and the best we can do using OMC.
173- """
91+ The argument parents with default value True exists to ensure compatibility with the fallback solution for
92+ Python < 3.12. In this case, pathlib.Path is used directly and this option ensures, that missing parent
93+ directories are also created.
94+ """
95+
96+ @abc .abstractmethod
97+ def cwd (self ) -> OMPathABC : # pylint: disable=W0221 # is @classmethod in the original; see pathlib.PathBase
98+ """
99+ Returns the current working directory as an OMPathABC object.
100+ """
101+
102+ @abc .abstractmethod
103+ def unlink (self , missing_ok : bool = False ) -> None :
104+ """
105+ Unlink (delete) the file or directory represented by this path.
106+ """
107+
108+ @abc .abstractmethod
109+ def resolve (self , strict : bool = False ) -> OMPathABC :
110+ """
111+ Resolve the path to an absolute path.
112+ """
113+
114+ def absolute (self ) -> OMPathABC :
115+ """
116+ Resolve the path to an absolute path. Just a wrapper for resolve().
117+ """
118+ return self .resolve ()
119+
120+ def exists (self ) -> bool :
121+ """
122+ Semi replacement for pathlib.Path.exists().
123+ """
124+ return self .is_file () or self .is_dir ()
125+
126+ @abc .abstractmethod
127+ def size (self ) -> int :
128+ """
129+ Get the size of the file in bytes - this is an extra function and the best we can do using OMC.
130+ """
174131
175132
176133class PostInitCaller (type ):
0 commit comments