3
3
Definition of an OMC session.
4
4
"""
5
5
6
+ from __future__ import annotations
7
+
6
8
__license__ = """
7
9
This file is part of OpenModelica.
8
10
33
35
"""
34
36
35
37
import shutil
36
- import abc
37
38
import getpass
38
39
import logging
39
40
import json
@@ -80,41 +81,23 @@ class OMCSessionException(Exception):
80
81
pass
81
82
82
83
83
- class OMCSessionBase ( metaclass = abc . ABCMeta ) :
84
+ class OMCSessionCmd :
84
85
85
- def __init__ (self , readonly = False ):
86
+ def __init__ (self , session : OMCSessionZMQ , readonly : Optional [bool ] = False ):
87
+ if not isinstance (session , OMCSessionZMQ ):
88
+ raise OMCSessionException ("Invalid session definition!" )
89
+ self ._session = session
86
90
self ._readonly = readonly
87
91
self ._omc_cache = {}
88
92
89
- def execute (self , command ):
90
- warnings .warn ("This function is depreciated and will be removed in future versions; "
91
- "please use sendExpression() instead" , DeprecationWarning , stacklevel = 1 )
92
-
93
- return self .sendExpression (command , parsed = False )
94
-
95
- @abc .abstractmethod
96
- def sendExpression (self , command , parsed = True ):
97
- """
98
- Sends an expression to the OpenModelica. The return type is parsed as if the
99
- expression was part of the typed OpenModelica API (see ModelicaBuiltin.mo).
100
- * Integer and Real are returned as Python numbers
101
- * Strings, enumerations, and typenames are returned as Python strings
102
- * Arrays, tuples, and MetaModelica lists are returned as tuples
103
- * Records are returned as dicts (the name of the record is lost)
104
- * Booleans are returned as True or False
105
- * NONE() is returned as None
106
- * SOME(value) is returned as value
107
- """
108
- pass
109
-
110
93
def _ask (self , question : str , opt : Optional [list [str ]] = None , parsed : Optional [bool ] = True ):
111
94
112
95
if opt is None :
113
96
expression = question
114
97
elif isinstance (opt , list ):
115
98
expression = f"{ question } ({ ',' .join ([str (x ) for x in opt ])} )"
116
99
else :
117
- raise Exception (f"Invalid definition of options for { repr (question )} : { repr (opt )} " )
100
+ raise OMCSessionException (f"Invalid definition of options for { repr (question )} : { repr (opt )} " )
118
101
119
102
p = (expression , parsed )
120
103
@@ -126,10 +109,9 @@ def _ask(self, question: str, opt: Optional[list[str]] = None, parsed: Optional[
126
109
logger .debug ('OMC ask: %s (parsed=%s)' , expression , parsed )
127
110
128
111
try :
129
- res = self .sendExpression (expression , parsed = parsed )
130
- except OMCSessionException :
131
- logger .error ("OMC failed: %s, %s, parsed=%s" , question , opt , parsed )
132
- raise
112
+ res = self ._session .sendExpression (expression , parsed = parsed )
113
+ except OMCSessionException as ex :
114
+ raise OMCSessionException ("OMC _ask() failed: %s (parsed=%s)" , expression , parsed ) from ex
133
115
134
116
# save response
135
117
self ._omc_cache [p ] = res
@@ -201,9 +183,11 @@ def getClassComment(self, className):
201
183
try :
202
184
return self ._ask (question = 'getClassComment' , opt = [className ])
203
185
except pyparsing .ParseException as ex :
204
- logger .warning ("Method 'getClassComment' failed for %s" , className )
205
- logger . warning ( 'OMTypedParser error: %s' , ex .msg )
186
+ logger .warning ("Method 'getClassComment(%s) ' failed; OMTypedParser error: %s" ,
187
+ className , ex .msg )
206
188
return 'No description available'
189
+ except OMCSessionException :
190
+ raise
207
191
208
192
def getNthComponent (self , className , comp_id ):
209
193
""" returns with (type, name, description) """
@@ -232,13 +216,18 @@ def getParameterNames(self, className):
232
216
logger .warning ('OMPython error: %s' , ex )
233
217
# FIXME: OMC returns with a different structure for empty parameter set
234
218
return []
219
+ except OMCSessionException :
220
+ raise
235
221
236
222
def getParameterValue (self , className , parameterName ):
237
223
try :
238
224
return self ._ask (question = 'getParameterValue' , opt = [className , parameterName ])
239
225
except pyparsing .ParseException as ex :
240
- logger .warning ('OMTypedParser error: %s' , ex .msg )
226
+ logger .warning ("Method 'getParameterValue(%s, %s)' failed; OMTypedParser error: %s" ,
227
+ className , parameterName , ex .msg )
241
228
return ""
229
+ except OMCSessionException :
230
+ raise
242
231
243
232
def getComponentModifierNames (self , className , componentName ):
244
233
return self ._ask (question = 'getComponentModifierNames' , opt = [className , componentName ])
@@ -273,26 +262,22 @@ def getNthComponentModification(self, className, comp_id):
273
262
# end getClassNames;
274
263
def getClassNames (self , className = None , recursive = False , qualified = False , sort = False , builtin = False ,
275
264
showProtected = False ):
276
- value = self ._ask (question = 'getClassNames' ,
277
- opt = [className ] if className else [] + [f'recursive={ str (recursive ).lower ()} ' ,
278
- f'qualified={ str (qualified ).lower ()} ' ,
279
- f'sort={ str (sort ).lower ()} ' ,
280
- f'builtin={ str (builtin ).lower ()} ' ,
281
- f'showProtected={ str (showProtected ).lower ()} ' ]
282
- )
283
- return value
265
+ opt = [className ] if className else [] + [f'recursive={ str (recursive ).lower ()} ' ,
266
+ f'qualified={ str (qualified ).lower ()} ' ,
267
+ f'sort={ str (sort ).lower ()} ' ,
268
+ f'builtin={ str (builtin ).lower ()} ' ,
269
+ f'showProtected={ str (showProtected ).lower ()} ' ]
270
+ return self ._ask (question = 'getClassNames' , opt = opt )
284
271
285
272
286
- class OMCSessionZMQ ( OMCSessionBase ) :
273
+ class OMCSessionZMQ :
287
274
288
- def __init__ (self , readonly = False , timeout = 10.00 ,
275
+ def __init__ (self , timeout = 10.00 ,
289
276
docker = None , dockerContainer = None , dockerExtraArgs = None , dockerOpenModelicaPath = "omc" ,
290
277
dockerNetwork = None , port = None , omhome : str = None ):
291
278
if dockerExtraArgs is None :
292
279
dockerExtraArgs = []
293
280
294
- super ().__init__ (readonly = readonly )
295
-
296
281
self .omhome = self ._get_omhome (omhome = omhome )
297
282
298
283
self ._omc_process = None
@@ -530,11 +515,20 @@ def _connect_to_omc(self, timeout):
530
515
self ._omc .setsockopt (zmq .IMMEDIATE , True ) # Queue messages only to completed connections
531
516
self ._omc .connect (self ._port )
532
517
518
+ def execute (self , command ):
519
+ warnings .warn ("This function is depreciated and will be removed in future versions; "
520
+ "please use sendExpression() instead" , DeprecationWarning , stacklevel = 1 )
521
+
522
+ return self .sendExpression (command , parsed = False )
523
+
533
524
def sendExpression (self , command , parsed = True ):
534
525
p = self ._omc_process .poll () # check if process is running
535
526
if p is not None :
536
527
raise OMCSessionException ("Process Exited, No connection with OMC. Create a new instance of OMCSessionZMQ!" )
537
528
529
+ if self ._omc is None :
530
+ raise OMCSessionException ("No OMC running. Create a new instance of OMCSessionZMQ!" )
531
+
538
532
attempts = 0
539
533
while True :
540
534
try :
0 commit comments