Skip to content

Commit 89c3548

Browse files
authored
Merge branch 'master' into OMCSessionException
2 parents 7a61ae7 + 5a666a0 commit 89c3548

2 files changed

Lines changed: 30 additions & 38 deletions

File tree

OMPython/OMCSession.py

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@
5151
import warnings
5252

5353
# TODO: replace this with the new parser
54-
from OMPython import OMTypedParser
55-
from OMPython import OMParser
54+
from OMPython.OMTypedParser import parseString as om_parser_typed
55+
from OMPython.OMParser import om_parser_basic
5656

5757

5858
# define logger using the current module name as ID
@@ -85,9 +85,6 @@ def __init__(self, readonly=False):
8585
self._readonly = readonly
8686
self._omc_cache = {}
8787

88-
def clearOMParserResult(self):
89-
OMParser.result = {}
90-
9188
def execute(self, command):
9289
warnings.warn("This function is depreciated and will be removed in future versions; "
9390
"please use sendExpression() instead", DeprecationWarning, stacklevel=1)
@@ -201,7 +198,7 @@ def getClassComment(self, className):
201198
return self.ask('getClassComment', className)
202199
except pyparsing.ParseException as ex:
203200
logger.warning("Method 'getClassComment' failed for %s", className)
204-
logger.warning('OMTypedParser error: %s', ex.message)
201+
logger.warning('OMTypedParser error: %s', ex.msg)
205202
return 'No description available'
206203

207204
def getNthComponent(self, className, comp_id):
@@ -236,44 +233,20 @@ def getParameterValue(self, className, parameterName):
236233
try:
237234
return self.ask('getParameterValue', f'{className}, {parameterName}')
238235
except pyparsing.ParseException as ex:
239-
logger.warning('OMTypedParser error: %s', ex.message)
236+
logger.warning('OMTypedParser error: %s', ex.msg)
240237
return ""
241238

242239
def getComponentModifierNames(self, className, componentName):
243240
return self.ask('getComponentModifierNames', f'{className}, {componentName}')
244241

245242
def getComponentModifierValue(self, className, componentName):
246-
try:
247-
# FIXME: OMPython exception UnboundLocalError exception for 'Modelica.Fluid.Machines.ControlledPump'
248-
return self.ask('getComponentModifierValue', f'{className}, {componentName}')
249-
except pyparsing.ParseException as ex:
250-
logger.warning('OMTypedParser error: %s', ex.message)
251-
result = self.ask('getComponentModifierValue', f'{className}, {componentName}', parsed=False)
252-
try:
253-
answer = OMParser.check_for_values(result)
254-
OMParser.result = {}
255-
return answer[2:]
256-
except (TypeError, UnboundLocalError) as ex:
257-
logger.warning('OMParser error: %s', ex)
258-
return result
243+
return self.ask(question='getComponentModifierValue', opt=f'{className}, {componentName}')
259244

260245
def getExtendsModifierNames(self, className, componentName):
261246
return self.ask('getExtendsModifierNames', f'{className}, {componentName}')
262247

263248
def getExtendsModifierValue(self, className, extendsName, modifierName):
264-
try:
265-
# FIXME: OMPython exception UnboundLocalError exception for 'Modelica.Fluid.Machines.ControlledPump'
266-
return self.ask('getExtendsModifierValue', f'{className}, {extendsName}, {modifierName}')
267-
except pyparsing.ParseException as ex:
268-
logger.warning('OMTypedParser error: %s', ex.message)
269-
result = self.ask('getExtendsModifierValue', f'{className}, {extendsName}, {modifierName}', parsed=False)
270-
try:
271-
answer = OMParser.check_for_values(result)
272-
OMParser.result = {}
273-
return answer[2:]
274-
except (TypeError, UnboundLocalError) as ex:
275-
logger.warning('OMParser error: %s', ex)
276-
return result
249+
return self.ask(question='getExtendsModifierValue', opt=f'{className}, {extendsName}, {modifierName}')
277250

278251
def getNthComponentModification(self, className, comp_id):
279252
# FIXME: OMPython exception Results KeyError exception
@@ -327,7 +300,7 @@ def __init__(self, readonly=False, timeout=10.00,
327300
self._serverIPAddress = "127.0.0.1"
328301
self._interactivePort = None
329302
# FIXME: this code is not well written... need to be refactored
330-
self._temp_dir = tempfile.gettempdir()
303+
self._temp_dir = pathlib.Path(tempfile.gettempdir())
331304
# generate a random string for this session
332305
self._random_string = uuid.uuid4().hex
333306
# omc log file
@@ -352,7 +325,7 @@ def __init__(self, readonly=False, timeout=10.00,
352325
self._dockerNetwork = dockerNetwork
353326
self._create_omc_log_file("port")
354327
self._timeout = timeout
355-
self._port_file = os.path.join("/tmp" if docker else self._temp_dir, self._port_file).replace("\\", "/")
328+
self._port_file = ((pathlib.Path("/tmp") if docker else self._temp_dir) / self._port_file).as_posix()
356329
self._interactivePort = port
357330
# set omc executable path and args
358331
self._set_omc_command([
@@ -386,7 +359,7 @@ def _create_omc_log_file(self, suffix):
386359
else:
387360
log_filename = f"openmodelica.{self._currentUser}.{suffix}.{self._random_string}.log"
388361
# this file must be closed in the destructor
389-
self._omc_log_file = open(pathlib.Path(self._temp_dir) / log_filename, "w+")
362+
self._omc_log_file = open(self._temp_dir / log_filename, "w+")
390363

391364
def _start_omc_process(self, timeout):
392365
if sys.platform == 'win32':
@@ -581,7 +554,14 @@ def sendExpression(self, command, parsed=True):
581554
else:
582555
result = self._omc.recv_string()
583556
if parsed is True:
584-
answer = OMTypedParser.parseString(result)
585-
return answer
557+
try:
558+
return om_parser_typed(result)
559+
except pyparsing.ParseException as ex:
560+
logger.warning('OMTypedParser error: %s. Returning the basic parser result.', ex.msg)
561+
try:
562+
return om_parser_basic(result)
563+
except (TypeError, UnboundLocalError) as ex:
564+
logger.warning('OMParser error: %s. Returning the unparsed result.', ex)
565+
return result
586566
else:
587567
return result

OMPython/OMParser.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,3 +892,15 @@ def check_for_values(string):
892892
check_for_values(next_set)
893893

894894
return result
895+
896+
897+
# TODO: hack to be able to use one entry point which also resets the (global) variable results
898+
# this should be checked such that the content of this file can be used as class with correct handling of
899+
# variable usage
900+
def om_parser_basic(string: str):
901+
result_return = check_for_values(string=string)
902+
903+
global result
904+
result = {}
905+
906+
return result_return

0 commit comments

Comments
 (0)