Skip to content

Commit f16e486

Browse files
committed
[OMCSessionBase] simplify the two use cases of OMParser.om_parse_basic()
1 parent 8d642d5 commit f16e486

File tree

1 file changed

+22
-24
lines changed

1 file changed

+22
-24
lines changed

OMPython/OMCSession.py

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,24 @@ def ask(self, question, opt=None, parsed=True):
128128

129129
return res
130130

131+
def _ask_with_fallback(self, question: str, opt: str = None):
132+
"""
133+
Version of ask() with OMTypedParser as fallback for OMParser which is used in ask() => sendExpression() if
134+
parsed is set to True.
135+
"""
136+
try:
137+
# FIXME: OMPython exception UnboundLocalError exception for 'Modelica.Fluid.Machines.ControlledPump'
138+
return self.ask(question=question, opt=opt)
139+
except pyparsing.ParseException as ex:
140+
logger.warning('OMTypedParser error: %s', ex.msg)
141+
result = self.ask(question=question, opt=opt, parsed=False)
142+
try:
143+
answer = om_parser_basic(result)
144+
return answer[2:]
145+
except (TypeError, UnboundLocalError) as ex:
146+
logger.warning('OMParser error: %s', ex)
147+
return result
148+
131149
# TODO: Open Modelica Compiler API functions. Would be nice to generate these.
132150
def loadFile(self, filename):
133151
return self.ask('loadFile', f'"{filename}"')
@@ -236,35 +254,15 @@ def getComponentModifierNames(self, className, componentName):
236254
return self.ask('getComponentModifierNames', f'{className}, {componentName}')
237255

238256
def getComponentModifierValue(self, className, componentName):
239-
try:
240-
# FIXME: OMPython exception UnboundLocalError exception for 'Modelica.Fluid.Machines.ControlledPump'
241-
return self.ask('getComponentModifierValue', f'{className}, {componentName}')
242-
except pyparsing.ParseException as ex:
243-
logger.warning('OMTypedParser error: %s', ex.msg)
244-
result = self.ask('getComponentModifierValue', f'{className}, {componentName}', parsed=False)
245-
try:
246-
answer = om_parser_basic(result)
247-
return answer[2:]
248-
except (TypeError, UnboundLocalError) as ex:
249-
logger.warning('OMParser error: %s', ex)
250-
return result
257+
return self._ask_with_fallback(question='getComponentModifierValue',
258+
opt=f'{className}, {componentName}')
251259

252260
def getExtendsModifierNames(self, className, componentName):
253261
return self.ask('getExtendsModifierNames', f'{className}, {componentName}')
254262

255263
def getExtendsModifierValue(self, className, extendsName, modifierName):
256-
try:
257-
# FIXME: OMPython exception UnboundLocalError exception for 'Modelica.Fluid.Machines.ControlledPump'
258-
return self.ask('getExtendsModifierValue', f'{className}, {extendsName}, {modifierName}')
259-
except pyparsing.ParseException as ex:
260-
logger.warning('OMTypedParser error: %s', ex.msg)
261-
result = self.ask('getExtendsModifierValue', f'{className}, {extendsName}, {modifierName}', parsed=False)
262-
try:
263-
answer = om_parser_basic(result)
264-
return answer[2:]
265-
except (TypeError, UnboundLocalError) as ex:
266-
logger.warning('OMParser error: %s', ex)
267-
return result
264+
return self._ask_with_fallback(question='getExtendsModifierValue',
265+
opt=f'{className}, {extendsName}, {modifierName}')
268266

269267
def getNthComponentModification(self, className, comp_id):
270268
# FIXME: OMPython exception Results KeyError exception

0 commit comments

Comments
 (0)