Skip to content

Commit 3f78a8b

Browse files
committed
[OMCSessionZMQ] use 'getMessagesStringInternal()' to check for OMC errors for each command using sendExpression()
1 parent 17423e0 commit 3f78a8b

File tree

1 file changed

+58
-9
lines changed

1 file changed

+58
-9
lines changed

OMPython/OMCSession.py

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import os
4242
import pathlib
4343
import psutil
44+
import re
4445
import signal
4546
import subprocess
4647
import sys
@@ -323,6 +324,9 @@ def __init__(self, timeout=10.00,
323324
# connect to the running omc instance using ZMQ
324325
self._connect_to_omc(timeout)
325326

327+
self._re_log_entries = None
328+
self._re_log_raw = None
329+
326330
def __del__(self):
327331
try:
328332
self.sendExpression("quit()")
@@ -547,15 +551,60 @@ def sendExpression(self, command, parsed=True):
547551
else:
548552
result = self._omc.recv_string()
549553

550-
# allways check for error
551-
self._omc.send_string("getErrorString()", flags=zmq.NOBLOCK)
552-
error_raw = self._omc.recv_string()
553-
error_str = om_parser_typed(error_raw)
554-
if error_str:
555-
if "Error" in error_str:
556-
raise OMCSessionException(f"OM Error for 'sendExpression({command}, {parsed})': {error_str}")
557-
else:
558-
logger.warning(f"[OM]: {error_str}")
554+
if command == "getErrorString()":
555+
# no error handling if 'getErrorString()' is called
556+
pass
557+
elif command == "getMessagesStringInternal()":
558+
# no error handling if 'getMessagesStringInternal()' is called; parsig NOT possible!
559+
if parsed:
560+
logger.warning("Result of 'getMessagesStringInternal()' cannot be parsed - set parsed to False!")
561+
parsed = False
562+
else:
563+
# allways check for error
564+
self._omc.send_string('getMessagesStringInternal()', flags=zmq.NOBLOCK)
565+
error_raw = self._omc.recv_string()
566+
# run error handling only if there is something to check
567+
if error_raw != "{}\n":
568+
if not self._re_log_entries:
569+
self._re_log_entries = re.compile(pattern=r'record OpenModelica\.Scripting\.ErrorMessage'
570+
'(.*?)'
571+
r'end OpenModelica\.Scripting\.ErrorMessage;',
572+
flags=re.MULTILINE | re.DOTALL)
573+
if not self._re_log_raw:
574+
self._re_log_raw = re.compile(
575+
pattern=r"\s+message = \"(.*?)\",\n" # message
576+
r"\s+kind = .OpenModelica.Scripting.ErrorKind.(.*?),\n" # kind
577+
r"\s+level = .OpenModelica.Scripting.ErrorLevel.(.*?),\n" # level
578+
r"\s+id = (.*?)" # id
579+
"(,\n|\n)", # end marker
580+
flags=re.MULTILINE | re.DOTALL)
581+
582+
# extract all ErrorMessage records
583+
log_entries = self._re_log_entries.findall(string=error_raw)
584+
for log_entry in reversed(log_entries):
585+
log_raw = self._re_log_raw.findall(string=log_entry)
586+
if len(log_raw) != 1 or len(log_raw[0]) != 5:
587+
logger.warning("Invalid ErrorMessage record returned by 'getMessagesStringInternal()':"
588+
f" {repr(log_entry)}!")
589+
590+
log_message = log_raw[0][0].encode().decode('unicode_escape')
591+
log_kind = log_raw[0][1]
592+
log_level = log_raw[0][2]
593+
log_id = log_raw[0][3]
594+
595+
msg = (f"[OMC log for 'sendExpression({command}, {parsed})']: "
596+
f"[{log_kind}:{log_level}:{log_id}] {log_message}")
597+
598+
# response according to the used log level
599+
# see: https://build.openmodelica.org/Documentation/OpenModelica.Scripting.ErrorLevel.html
600+
if log_level == 'error':
601+
raise OMCSessionException(msg)
602+
elif log_level == 'warning':
603+
logger.warning(msg)
604+
elif log_level == 'notification':
605+
logger.info(msg)
606+
else: # internal
607+
logger.debug(msg)
559608

560609
if parsed is True:
561610
try:

0 commit comments

Comments
 (0)