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