Skip to content

Commit 7a61ae7

Browse files
committed
[OMCSessionBase] use OMCSessionException instead of generic Exception
1 parent e1f01b5 commit 7a61ae7

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

OMPython/OMCSession.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def ask(self, question, opt=None, parsed=True):
126126

127127
try:
128128
res = self.sendExpression(expression, parsed=parsed)
129-
except Exception:
129+
except OMCSessionException:
130130
logger.error("OMC failed: %s, %s, parsed=%s", question, opt, parsed)
131131
raise
132132

@@ -368,7 +368,7 @@ def __init__(self, readonly=False, timeout=10.00,
368368
def __del__(self):
369369
try:
370370
self.sendExpression("quit()")
371-
except Exception:
371+
except OMCSessionException:
372372
pass
373373
self._omc_log_file.close()
374374
try:
@@ -417,7 +417,8 @@ def _start_omc_process(self, timeout):
417417
pass
418418
if self._dockerCid is None:
419419
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)
421422

422423
dockerTop = None
423424
if self._docker or self._dockerContainer:
@@ -434,17 +435,16 @@ def _start_omc_process(self, timeout):
434435
try:
435436
self._omc_process = DummyPopen(int(columns[1]))
436437
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?\nLog-file says:\n{open(self._omc_log_file.name).read()}")
440441
break
441442
if self._omc_process is not None:
442443
break
443444
time.sleep(timeout / 40.0)
444445
if self._omc_process is None:
445-
446-
raise Exception("Docker top did not contain omc process %s:\n%s\nLog-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\nLog-file says:\n%s"
447+
% (self._random_string, dockerTop, open(self._omc_log_file.name).read()))
448448
return self._omc_process
449449

450450
def _getuid(self):
@@ -465,7 +465,9 @@ def _set_omc_command(self, omc_path_and_args_list):
465465
if (self._docker or self._dockerContainer) and sys.platform == "win32":
466466
extraFlags = ["-d=zmqDangerousAcceptConnectionsFromAnywhere"]
467467
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.")
469471
else:
470472
extraFlags = []
471473
if self._docker:
@@ -478,7 +480,7 @@ def _set_omc_command(self, omc_path_and_args_list):
478480
dockerNetworkStr = []
479481
extraFlags = ["-d=zmqDangerousAcceptConnectionsFromAnywhere"]
480482
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')
482484
self._dockerCidFile = self._omc_log_file.name + ".docker.cid"
483485
omcCommand = ["docker", "run", "--cidfile", self._dockerCidFile, "--rm", "--env", "USER=%s" % self._currentUser, "--user", str(self._getuid())] + self._dockerExtraArgs + dockerNetworkStr + [self._docker, self._dockerOpenModelicaPath]
484486
elif self._dockerContainer:
@@ -508,7 +510,7 @@ def _get_omhome(self, omhome: str = None):
508510
if path_to_omc is not None:
509511
return pathlib.Path(path_to_omc).parents[1]
510512

511-
raise ValueError("Cannot find OpenModelica executable, please install from openmodelica.org")
513+
raise OMCSessionException("Cannot find OpenModelica executable, please install from openmodelica.org")
512514

513515
def _get_omc_path(self) -> pathlib.Path:
514516
return self.omhome / "bin" / "omc"
@@ -539,7 +541,8 @@ def _connect_to_omc(self, timeout):
539541
name = self._omc_log_file.name
540542
self._omc_log_file.close()
541543
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}")
543546
time.sleep(timeout / 80.0)
544547

545548
self._port = self._port.replace("0.0.0.0", self._serverIPAddress)
@@ -555,7 +558,7 @@ def _connect_to_omc(self, timeout):
555558
def sendExpression(self, command, parsed=True):
556559
p = self._omc_process.poll() # check if process is running
557560
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!")
559562

560563
attempts = 0
561564
while True:
@@ -569,7 +572,7 @@ def sendExpression(self, command, parsed=True):
569572
self._omc_log_file.seek(0)
570573
log = self._omc_log_file.read()
571574
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}")
573576
time.sleep(self._timeout / 50.0)
574577
if command == "quit()":
575578
self._omc.close()

0 commit comments

Comments
 (0)