@@ -75,6 +75,10 @@ def wait(self, timeout):
75
75
return self .process .wait (timeout = timeout )
76
76
77
77
78
+ class OMCSessionException (Exception ):
79
+ pass
80
+
81
+
78
82
class OMCSessionBase (metaclass = abc .ABCMeta ):
79
83
80
84
def __init__ (self , readonly = False ):
@@ -122,7 +126,7 @@ def ask(self, question, opt=None, parsed=True):
122
126
123
127
try :
124
128
res = self .sendExpression (expression , parsed = parsed )
125
- except Exception :
129
+ except OMCSessionException :
126
130
logger .error ("OMC failed: %s, %s, parsed=%s" , question , opt , parsed )
127
131
raise
128
132
@@ -364,14 +368,15 @@ def __init__(self, readonly=False, timeout=10.00,
364
368
def __del__ (self ):
365
369
try :
366
370
self .sendExpression ("quit()" )
367
- except Exception :
371
+ except OMCSessionException :
368
372
pass
369
373
self ._omc_log_file .close ()
370
374
try :
371
375
self ._omc_process .wait (timeout = 2.0 )
372
- except Exception :
376
+ except subprocess . TimeoutExpired :
373
377
if self ._omc_process :
374
- logger .warning ("OMC did not exit after being sent the quit() command; killing the process with pid=%s" , self ._omc_process .pid )
378
+ logger .warning ("OMC did not exit after being sent the quit() command; "
379
+ "killing the process with pid=%s" , self ._omc_process .pid )
375
380
self ._omc_process .kill ()
376
381
self ._omc_process .wait ()
377
382
@@ -401,18 +406,19 @@ def _start_omc_process(self, timeout):
401
406
try :
402
407
with open (self ._dockerCidFile , "r" ) as fin :
403
408
self ._dockerCid = fin .read ().strip ()
404
- except Exception :
409
+ except IOError :
405
410
pass
406
411
if self ._dockerCid :
407
412
break
408
413
time .sleep (timeout / 40.0 )
409
414
try :
410
415
os .remove (self ._dockerCidFile )
411
- except Exception :
416
+ except FileNotFoundError :
412
417
pass
413
418
if self ._dockerCid is None :
414
419
logger .error ("Docker did not start. Log-file says:\n %s" % (open (self ._omc_log_file .name ).read ()))
415
- raise Exception ("Docker did not start (timeout=%f might be too short especially if you did not docker pull the image before this command)." % timeout )
420
+ raise OMCSessionException ("Docker did not start (timeout=%f might be too short especially if you did "
421
+ "not docker pull the image before this command)." % timeout )
416
422
417
423
dockerTop = None
418
424
if self ._docker or self ._dockerContainer :
@@ -429,17 +435,16 @@ def _start_omc_process(self, timeout):
429
435
try :
430
436
self ._omc_process = DummyPopen (int (columns [1 ]))
431
437
except psutil .NoSuchProcess :
432
- raise Exception (
433
- f"Could not find PID { dockerTop } - is this a docker instance spawned without --pid=host? \n "
434
- f"Log -file says:\n { open (self ._omc_log_file .name ).read ()} " )
438
+ raise OMCSessionException (
439
+ f"Could not find PID { dockerTop } - is this a docker instance spawned "
440
+ f"without --pid=host? \n Log -file says:\n { open (self ._omc_log_file .name ).read ()} " )
435
441
break
436
442
if self ._omc_process is not None :
437
443
break
438
444
time .sleep (timeout / 40.0 )
439
445
if self ._omc_process is None :
440
-
441
- raise Exception ("Docker top did not contain omc process %s:\n %s\n Log-file says:\n %s"
442
- % (self ._random_string , dockerTop , open (self ._omc_log_file .name ).read ()))
446
+ raise OMCSessionException ("Docker top did not contain omc process %s:\n %s\n Log-file says:\n %s"
447
+ % (self ._random_string , dockerTop , open (self ._omc_log_file .name ).read ()))
443
448
return self ._omc_process
444
449
445
450
def _getuid (self ):
@@ -460,7 +465,9 @@ def _set_omc_command(self, omc_path_and_args_list):
460
465
if (self ._docker or self ._dockerContainer ) and sys .platform == "win32" :
461
466
extraFlags = ["-d=zmqDangerousAcceptConnectionsFromAnywhere" ]
462
467
if not self ._interactivePort :
463
- raise Exception ("docker on Windows requires knowing which port to connect to. For dockerContainer=..., the container needs to have already manually exposed this port when it was started (-p 127.0.0.1:n:n) or you get an error later." )
468
+ raise OMCSessionException ("docker on Windows requires knowing which port to connect to. For "
469
+ "dockerContainer=..., the container needs to have already manually exposed "
470
+ "this port when it was started (-p 127.0.0.1:n:n) or you get an error later." )
464
471
else :
465
472
extraFlags = []
466
473
if self ._docker :
@@ -473,7 +480,7 @@ def _set_omc_command(self, omc_path_and_args_list):
473
480
dockerNetworkStr = []
474
481
extraFlags = ["-d=zmqDangerousAcceptConnectionsFromAnywhere" ]
475
482
else :
476
- raise Exception ('dockerNetwork was set to %s, but only \" host\" or \" separate\" is allowed' )
483
+ raise OMCSessionException ('dockerNetwork was set to %s, but only \" host\" or \" separate\" is allowed' )
477
484
self ._dockerCidFile = self ._omc_log_file .name + ".docker.cid"
478
485
omcCommand = ["docker" , "run" , "--cidfile" , self ._dockerCidFile , "--rm" , "--env" , "USER=%s" % self ._currentUser , "--user" , str (self ._getuid ())] + self ._dockerExtraArgs + dockerNetworkStr + [self ._docker , self ._dockerOpenModelicaPath ]
479
486
elif self ._dockerContainer :
@@ -503,7 +510,7 @@ def _get_omhome(self, omhome: str = None):
503
510
if path_to_omc is not None :
504
511
return pathlib .Path (path_to_omc ).parents [1 ]
505
512
506
- raise ValueError ("Cannot find OpenModelica executable, please install from openmodelica.org" )
513
+ raise OMCSessionException ("Cannot find OpenModelica executable, please install from openmodelica.org" )
507
514
508
515
def _get_omc_path (self ) -> pathlib .Path :
509
516
return self .omhome / "bin" / "omc"
@@ -516,9 +523,10 @@ def _connect_to_omc(self, timeout):
516
523
while True :
517
524
if self ._dockerCid :
518
525
try :
519
- self ._port = subprocess .check_output (["docker" , "exec" , self ._dockerCid , "cat" , self ._port_file ], stderr = subprocess .DEVNULL ).decode ().strip ()
526
+ self ._port = subprocess .check_output (["docker" , "exec" , self ._dockerCid , "cat" , self ._port_file ],
527
+ stderr = subprocess .DEVNULL ).decode ().strip ()
520
528
break
521
- except Exception :
529
+ except subprocess . CalledProcessError :
522
530
pass
523
531
else :
524
532
if os .path .isfile (self ._port_file ):
@@ -533,7 +541,8 @@ def _connect_to_omc(self, timeout):
533
541
name = self ._omc_log_file .name
534
542
self ._omc_log_file .close ()
535
543
logger .error ("OMC Server did not start. Please start it! Log-file says:\n %s" % open (name ).read ())
536
- raise Exception (f"OMC Server did not start (timeout={ timeout } ). Could not open file { self ._port_file } " )
544
+ raise OMCSessionException (f"OMC Server did not start (timeout={ timeout } ). "
545
+ "Could not open file {self._port_file}" )
537
546
time .sleep (timeout / 80.0 )
538
547
539
548
self ._port = self ._port .replace ("0.0.0.0" , self ._serverIPAddress )
@@ -549,7 +558,7 @@ def _connect_to_omc(self, timeout):
549
558
def sendExpression (self , command , parsed = True ):
550
559
p = self ._omc_process .poll () # check if process is running
551
560
if p is not None :
552
- raise Exception ("Process Exited, No connection with OMC. Create a new instance of OMCSessionZMQ" )
561
+ raise OMCSessionException ("Process Exited, No connection with OMC. Create a new instance of OMCSessionZMQ! " )
553
562
554
563
attempts = 0
555
564
while True :
@@ -563,7 +572,7 @@ def sendExpression(self, command, parsed=True):
563
572
self ._omc_log_file .seek (0 )
564
573
log = self ._omc_log_file .read ()
565
574
self ._omc_log_file .close ()
566
- raise Exception (f"No connection with OMC (timeout={ self ._timeout } ). Log-file says: \n { log } " )
575
+ raise OMCSessionException (f"No connection with OMC (timeout={ self ._timeout } ). Log-file says: \n { log } " )
567
576
time .sleep (self ._timeout / 50.0 )
568
577
if command == "quit()" :
569
578
self ._omc .close ()
0 commit comments