Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Do not strip the result
The output of `getComponentModifierValue` is changed so we don't need to strip the result.
And the output of `getExtendsModifierValue` is changed in OpenModelica/OpenModelica#13533

Removed `_ask_with_fallback` and moved the fallback code to `sendExpression`
  • Loading branch information
adeas31 committed Apr 30, 2025
commit 6a6b50194ccfcc98a21b24df2455948a343e59ef
34 changes: 11 additions & 23 deletions OMPython/OMCSession.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,23 +128,6 @@ def ask(self, question, opt=None, parsed=True):

return res

def _ask_with_fallback(self, question: str, opt: str = None):
"""
Version of ask() with OMTypedParser as fallback for OMParser which is used in ask() => sendExpression() if
parsed is set to True.
"""
try:
# FIXME: OMPython exception UnboundLocalError exception for 'Modelica.Fluid.Machines.ControlledPump'
return self.ask(question=question, opt=opt)
except pyparsing.ParseException as ex:
logger.warning('OMTypedParser error: %s', ex.msg)
result = self.ask(question=question, opt=opt, parsed=False)
try:
return om_parser_basic(result)
except (TypeError, UnboundLocalError) as ex:
logger.warning('OMParser error: %s', ex)
return result

# TODO: Open Modelica Compiler API functions. Would be nice to generate these.
def loadFile(self, filename):
return self.ask('loadFile', f'"{filename}"')
Expand Down Expand Up @@ -253,15 +236,13 @@ def getComponentModifierNames(self, className, componentName):
return self.ask('getComponentModifierNames', f'{className}, {componentName}')

def getComponentModifierValue(self, className, componentName):
return self._ask_with_fallback(question='getComponentModifierValue',
opt=f'{className}, {componentName}')
return self.ask(question='getComponentModifierValue', opt=f'{className}, {componentName}')

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

def getExtendsModifierValue(self, className, extendsName, modifierName):
return self._ask_with_fallback(question='getExtendsModifierValue',
opt=f'{className}, {extendsName}, {modifierName}')
return self.ask(question='getExtendsModifierValue', opt=f'{className}, {extendsName}, {modifierName}')

def getNthComponentModification(self, className, comp_id):
# FIXME: OMPython exception Results KeyError exception
Expand Down Expand Up @@ -564,7 +545,14 @@ def sendExpression(self, command, parsed=True):
else:
result = self._omc.recv_string()
if parsed is True:
answer = om_parser_typed(result)
return answer
try:
return om_parser_typed(result)
except pyparsing.ParseException as ex:
logger.warning('OMTypedParser error: %s. Returning the basic parser result.', ex.msg)
try:
return om_parser_basic(result)
except (TypeError, UnboundLocalError) as ex:
logger.warning('OMParser error: %s. Returning the unparsed result.', ex)
return result
else:
return result