@@ -126,7 +126,7 @@ def ask(self, question, opt=None, parsed=True):
126
126
127
127
try :
128
128
res = self .sendExpression (expression , parsed = parsed )
129
- except Exception :
129
+ except OMCSessionException :
130
130
logger .error ("OMC failed: %s, %s, parsed=%s" , question , opt , parsed )
131
131
raise
132
132
@@ -368,7 +368,7 @@ def __init__(self, readonly=False, timeout=10.00,
368
368
def __del__ (self ):
369
369
try :
370
370
self .sendExpression ("quit()" )
371
- except Exception :
371
+ except OMCSessionException :
372
372
pass
373
373
self ._omc_log_file .close ()
374
374
try :
@@ -417,7 +417,8 @@ def _start_omc_process(self, timeout):
417
417
pass
418
418
if self ._dockerCid is None :
419
419
logger .error ("Docker did not start. Log-file says:\n %s" % (open (self ._omc_log_file .name ).read ()))
420
- 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 )
421
422
422
423
dockerTop = None
423
424
if self ._docker or self ._dockerContainer :
@@ -434,17 +435,16 @@ def _start_omc_process(self, timeout):
434
435
try :
435
436
self ._omc_process = DummyPopen (int (columns [1 ]))
436
437
except psutil .NoSuchProcess :
437
- raise Exception (
438
- f"Could not find PID { dockerTop } - is this a docker instance spawned without --pid=host? \n "
439
- 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 ()} " )
440
441
break
441
442
if self ._omc_process is not None :
442
443
break
443
444
time .sleep (timeout / 40.0 )
444
445
if self ._omc_process is None :
445
-
446
- raise Exception ("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 ()))
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 ()))
448
448
return self ._omc_process
449
449
450
450
def _getuid (self ):
@@ -465,7 +465,9 @@ def _set_omc_command(self, omc_path_and_args_list):
465
465
if (self ._docker or self ._dockerContainer ) and sys .platform == "win32" :
466
466
extraFlags = ["-d=zmqDangerousAcceptConnectionsFromAnywhere" ]
467
467
if not self ._interactivePort :
468
- 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." )
469
471
else :
470
472
extraFlags = []
471
473
if self ._docker :
@@ -478,7 +480,7 @@ def _set_omc_command(self, omc_path_and_args_list):
478
480
dockerNetworkStr = []
479
481
extraFlags = ["-d=zmqDangerousAcceptConnectionsFromAnywhere" ]
480
482
else :
481
- 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' )
482
484
self ._dockerCidFile = self ._omc_log_file .name + ".docker.cid"
483
485
omcCommand = ["docker" , "run" , "--cidfile" , self ._dockerCidFile , "--rm" , "--env" , "USER=%s" % self ._currentUser , "--user" , str (self ._getuid ())] + self ._dockerExtraArgs + dockerNetworkStr + [self ._docker , self ._dockerOpenModelicaPath ]
484
486
elif self ._dockerContainer :
@@ -508,7 +510,7 @@ def _get_omhome(self, omhome: str = None):
508
510
if path_to_omc is not None :
509
511
return pathlib .Path (path_to_omc ).parents [1 ]
510
512
511
- raise ValueError ("Cannot find OpenModelica executable, please install from openmodelica.org" )
513
+ raise OMCSessionException ("Cannot find OpenModelica executable, please install from openmodelica.org" )
512
514
513
515
def _get_omc_path (self ) -> pathlib .Path :
514
516
return self .omhome / "bin" / "omc"
@@ -539,7 +541,8 @@ def _connect_to_omc(self, timeout):
539
541
name = self ._omc_log_file .name
540
542
self ._omc_log_file .close ()
541
543
logger .error ("OMC Server did not start. Please start it! Log-file says:\n %s" % open (name ).read ())
542
- 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}" )
543
546
time .sleep (timeout / 80.0 )
544
547
545
548
self ._port = self ._port .replace ("0.0.0.0" , self ._serverIPAddress )
@@ -555,7 +558,7 @@ def _connect_to_omc(self, timeout):
555
558
def sendExpression (self , command , parsed = True ):
556
559
p = self ._omc_process .poll () # check if process is running
557
560
if p is not None :
558
- 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! " )
559
562
560
563
attempts = 0
561
564
while True :
@@ -569,7 +572,7 @@ def sendExpression(self, command, parsed=True):
569
572
self ._omc_log_file .seek (0 )
570
573
log = self ._omc_log_file .read ()
571
574
self ._omc_log_file .close ()
572
- 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 } " )
573
576
time .sleep (self ._timeout / 50.0 )
574
577
if command == "quit()" :
575
578
self ._omc .close ()
0 commit comments