@@ -75,6 +75,10 @@ def wait(self, timeout):
7575 return self .process .wait (timeout = timeout )
7676
7777
78+ class OMCSessionException (Exception ):
79+ pass
80+
81+
7882class OMCSessionBase (metaclass = abc .ABCMeta ):
7983
8084 def __init__ (self , readonly = False ):
@@ -122,7 +126,7 @@ def ask(self, question, opt=None, parsed=True):
122126
123127 try :
124128 res = self .sendExpression (expression , parsed = parsed )
125- except Exception :
129+ except OMCSessionException :
126130 logger .error ("OMC failed: %s, %s, parsed=%s" , question , opt , parsed )
127131 raise
128132
@@ -364,14 +368,15 @@ def __init__(self, readonly=False, timeout=10.00,
364368 def __del__ (self ):
365369 try :
366370 self .sendExpression ("quit()" )
367- except Exception :
371+ except OMCSessionException :
368372 pass
369373 self ._omc_log_file .close ()
370374 try :
371375 self ._omc_process .wait (timeout = 2.0 )
372- except Exception :
376+ except subprocess . TimeoutExpired :
373377 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 )
375380 self ._omc_process .kill ()
376381 self ._omc_process .wait ()
377382
@@ -401,18 +406,19 @@ def _start_omc_process(self, timeout):
401406 try :
402407 with open (self ._dockerCidFile , "r" ) as fin :
403408 self ._dockerCid = fin .read ().strip ()
404- except Exception :
409+ except IOError :
405410 pass
406411 if self ._dockerCid :
407412 break
408413 time .sleep (timeout / 40.0 )
409414 try :
410415 os .remove (self ._dockerCidFile )
411- except Exception :
416+ except FileNotFoundError :
412417 pass
413418 if self ._dockerCid is None :
414419 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 )
416422
417423 dockerTop = None
418424 if self ._docker or self ._dockerContainer :
@@ -429,17 +435,16 @@ def _start_omc_process(self, timeout):
429435 try :
430436 self ._omc_process = DummyPopen (int (columns [1 ]))
431437 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 ()} " )
435441 break
436442 if self ._omc_process is not None :
437443 break
438444 time .sleep (timeout / 40.0 )
439445 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 ()))
443448 return self ._omc_process
444449
445450 def _getuid (self ):
@@ -460,7 +465,9 @@ def _set_omc_command(self, omc_path_and_args_list):
460465 if (self ._docker or self ._dockerContainer ) and sys .platform == "win32" :
461466 extraFlags = ["-d=zmqDangerousAcceptConnectionsFromAnywhere" ]
462467 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." )
464471 else :
465472 extraFlags = []
466473 if self ._docker :
@@ -473,7 +480,7 @@ def _set_omc_command(self, omc_path_and_args_list):
473480 dockerNetworkStr = []
474481 extraFlags = ["-d=zmqDangerousAcceptConnectionsFromAnywhere" ]
475482 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' )
477484 self ._dockerCidFile = self ._omc_log_file .name + ".docker.cid"
478485 omcCommand = ["docker" , "run" , "--cidfile" , self ._dockerCidFile , "--rm" , "--env" , "USER=%s" % self ._currentUser , "--user" , str (self ._getuid ())] + self ._dockerExtraArgs + dockerNetworkStr + [self ._docker , self ._dockerOpenModelicaPath ]
479486 elif self ._dockerContainer :
@@ -503,7 +510,7 @@ def _get_omhome(self, omhome: str = None):
503510 if path_to_omc is not None :
504511 return pathlib .Path (path_to_omc ).parents [1 ]
505512
506- raise ValueError ("Cannot find OpenModelica executable, please install from openmodelica.org" )
513+ raise OMCSessionException ("Cannot find OpenModelica executable, please install from openmodelica.org" )
507514
508515 def _get_omc_path (self ) -> pathlib .Path :
509516 return self .omhome / "bin" / "omc"
@@ -516,9 +523,10 @@ def _connect_to_omc(self, timeout):
516523 while True :
517524 if self ._dockerCid :
518525 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 ()
520528 break
521- except Exception :
529+ except subprocess . CalledProcessError :
522530 pass
523531 else :
524532 if os .path .isfile (self ._port_file ):
@@ -533,7 +541,8 @@ def _connect_to_omc(self, timeout):
533541 name = self ._omc_log_file .name
534542 self ._omc_log_file .close ()
535543 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}" )
537546 time .sleep (timeout / 80.0 )
538547
539548 self ._port = self ._port .replace ("0.0.0.0" , self ._serverIPAddress )
@@ -549,7 +558,7 @@ def _connect_to_omc(self, timeout):
549558 def sendExpression (self , command , parsed = True ):
550559 p = self ._omc_process .poll () # check if process is running
551560 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! " )
553562
554563 attempts = 0
555564 while True :
@@ -563,7 +572,7 @@ def sendExpression(self, command, parsed=True):
563572 self ._omc_log_file .seek (0 )
564573 log = self ._omc_log_file .read ()
565574 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 } " )
567576 time .sleep (self ._timeout / 50.0 )
568577 if command == "quit()" :
569578 self ._omc .close ()
0 commit comments