Skip to content

Parser #270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
May 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
50 changes: 15 additions & 35 deletions OMPython/OMCSession.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
import warnings

# TODO: replace this with the new parser
from OMPython import OMTypedParser
from OMPython import OMParser
from OMPython.OMTypedParser import parseString as om_parser_typed
from OMPython.OMParser import om_parser_basic


# define logger using the current module name as ID
Expand Down Expand Up @@ -81,9 +81,6 @@ def __init__(self, readonly=False):
self._readonly = readonly
self._omc_cache = {}

def clearOMParserResult(self):
OMParser.result = {}

def execute(self, command):
warnings.warn("This function is depreciated and will be removed in future versions; "
"please use sendExpression() instead", DeprecationWarning, stacklevel=1)
Expand Down Expand Up @@ -197,7 +194,7 @@ def getClassComment(self, className):
return self.ask('getClassComment', className)
except pyparsing.ParseException as ex:
logger.warning("Method 'getClassComment' failed for %s", className)
logger.warning('OMTypedParser error: %s', ex.message)
logger.warning('OMTypedParser error: %s', ex.msg)
return 'No description available'

def getNthComponent(self, className, comp_id):
Expand Down Expand Up @@ -232,44 +229,20 @@ def getParameterValue(self, className, parameterName):
try:
return self.ask('getParameterValue', f'{className}, {parameterName}')
except pyparsing.ParseException as ex:
logger.warning('OMTypedParser error: %s', ex.message)
logger.warning('OMTypedParser error: %s', ex.msg)
return ""

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

def getComponentModifierValue(self, className, componentName):
try:
# FIXME: OMPython exception UnboundLocalError exception for 'Modelica.Fluid.Machines.ControlledPump'
return self.ask('getComponentModifierValue', f'{className}, {componentName}')
except pyparsing.ParseException as ex:
logger.warning('OMTypedParser error: %s', ex.message)
result = self.ask('getComponentModifierValue', f'{className}, {componentName}', parsed=False)
try:
answer = OMParser.check_for_values(result)
OMParser.result = {}
return answer[2:]
except (TypeError, UnboundLocalError) as ex:
logger.warning('OMParser error: %s', ex)
return result
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):
try:
# FIXME: OMPython exception UnboundLocalError exception for 'Modelica.Fluid.Machines.ControlledPump'
return self.ask('getExtendsModifierValue', f'{className}, {extendsName}, {modifierName}')
except pyparsing.ParseException as ex:
logger.warning('OMTypedParser error: %s', ex.message)
result = self.ask('getExtendsModifierValue', f'{className}, {extendsName}, {modifierName}', parsed=False)
try:
answer = OMParser.check_for_values(result)
OMParser.result = {}
return answer[2:]
except (TypeError, UnboundLocalError) as ex:
logger.warning('OMParser error: %s', ex)
return result
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 @@ -572,7 +545,14 @@ def sendExpression(self, command, parsed=True):
else:
result = self._omc.recv_string()
if parsed is True:
answer = OMTypedParser.parseString(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
12 changes: 12 additions & 0 deletions OMPython/OMParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -892,3 +892,15 @@ def check_for_values(string):
check_for_values(next_set)

return result


# TODO: hack to be able to use one entry point which also resets the (global) variable results
# this should be checked such that the content of this file can be used as class with correct handling of
# variable usage
def om_parser_basic(string: str):
result_return = check_for_values(string=string)

global result
result = {}

return result_return
Loading