diff --git a/OMPython/OMCSession.py b/OMPython/OMCSession.py index 63fbba00..7b3e647f 100644 --- a/OMPython/OMCSession.py +++ b/OMPython/OMCSession.py @@ -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 @@ -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) @@ -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): @@ -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 @@ -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 diff --git a/OMPython/OMParser.py b/OMPython/OMParser.py index f1708947..1377fc6a 100644 --- a/OMPython/OMParser.py +++ b/OMPython/OMParser.py @@ -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