Skip to content

Commit 7f4e0a9

Browse files
committed
Use f-strings in OMCSessionZMQ
1 parent ba03555 commit 7f4e0a9

1 file changed

Lines changed: 34 additions & 33 deletions

File tree

OMPython/__init__.py

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ def _create_omc_log_file(self, suffix):
419419
else:
420420
log_filename = f"openmodelica.{self._currentUser}.{suffix}.{self._random_string}.log"
421421
# this file must be closed in the destructor
422-
self._omc_log_file = open(pathlib.Path(self._temp_dir) / log_filename, "w")
422+
self._omc_log_file = open(pathlib.Path(self._temp_dir) / log_filename, "w+")
423423

424424
def _start_omc_process(self, timeout):
425425
if sys.platform == 'win32':
@@ -469,13 +469,14 @@ def _start_omc_process(self, timeout):
469469
self._omc_process = DummyPopen(int(columns[1]))
470470
except psutil.NoSuchProcess:
471471
raise Exception(
472-
"Could not find PID %s - is this a docker instance spawned without --pid=host?\n"
473-
"Log-file says:\n%s" % (self._random_string, open(self._omc_log_file.name).read()))
472+
f"Could not find PID {dockerTop} - is this a docker instance spawned without --pid=host?\n"
473+
f"Log-file says:\n{open(self._omc_log_file.name).read()}")
474474
break
475475
if self._omc_process is not None:
476476
break
477477
time.sleep(timeout / 40.0)
478478
if self._omc_process is None:
479+
479480
raise Exception("Docker top did not contain omc process %s:\n%s\nLog-file says:\n%s"
480481
% (self._random_string, dockerTop, open(self._omc_log_file.name).read()))
481482
return self._omc_process
@@ -579,11 +580,11 @@ def _connect_to_omc(self, timeout):
579580
name = self._omc_log_file.name
580581
self._omc_log_file.close()
581582
logger.error("OMC Server did not start. Please start it! Log-file says:\n%s" % open(name).read())
582-
raise Exception("OMC Server did not start (timeout=%f). Could not open file %s" % (timeout, self._port_file))
583+
raise Exception(f"OMC Server did not start (timeout={timeout}). Could not open file {self._port_file}")
583584
time.sleep(timeout / 80.0)
584585

585586
self._port = self._port.replace("0.0.0.0", self._serverIPAddress)
586-
logger.info("OMC Server is up and running at {0} pid={1} cid={2}".format(self._omc_zeromq_uri, self._omc_process.pid, self._dockerCid))
587+
logger.info(f"OMC Server is up and running at {self._omc_zeromq_uri} pid={self._omc_process.pid} cid={self._dockerCid}")
587588

588589
# Create the ZeroMQ socket and connect to OMC server
589590
context = zmq.Context.instance()
@@ -593,36 +594,36 @@ def _connect_to_omc(self, timeout):
593594
self._omc.connect(self._port)
594595

595596
def sendExpression(self, command, parsed=True):
596-
# check for process is running
597-
p = self._omc_process.poll()
598-
if p is None:
599-
attempts = 0
600-
while True:
601-
try:
602-
self._omc.send_string(str(command), flags=zmq.NOBLOCK)
603-
break
604-
except zmq.error.Again:
605-
pass
606-
attempts += 1
607-
if attempts == 50.0:
608-
name = self._omc_log_file.name
609-
self._omc_log_file.close()
610-
raise Exception("No connection with OMC (timeout=%f). Log-file says: \n%s" % (self._timeout, open(name).read()))
611-
time.sleep(self._timeout / 50.0)
612-
if command == "quit()":
613-
self._omc.close()
614-
self._omc = None
615-
return None
616-
else:
617-
result = self._omc.recv_string()
618-
if parsed is True:
619-
answer = OMTypedParser.parseString(result)
620-
return answer
621-
else:
622-
return result
623-
else:
597+
p = self._omc_process.poll() # check if process is running
598+
if p is not None:
624599
raise Exception("Process Exited, No connection with OMC. Create a new instance of OMCSessionZMQ")
625600

601+
attempts = 0
602+
while True:
603+
try:
604+
self._omc.send_string(str(command), flags=zmq.NOBLOCK)
605+
break
606+
except zmq.error.Again:
607+
pass
608+
attempts += 1
609+
if attempts >= 50:
610+
self._omc_log_file.seek(0)
611+
log = self._omc_log_file.read()
612+
self._omc_log_file.close()
613+
raise Exception(f"No connection with OMC (timeout={self._timeout}). Log-file says: \n{log}")
614+
time.sleep(self._timeout / 50.0)
615+
if command == "quit()":
616+
self._omc.close()
617+
self._omc = None
618+
return None
619+
else:
620+
result = self._omc.recv_string()
621+
if parsed is True:
622+
answer = OMTypedParser.parseString(result)
623+
return answer
624+
else:
625+
return result
626+
626627

627628
class ModelicaSystemError(Exception):
628629
pass

0 commit comments

Comments
 (0)