Skip to content

Commit 4af7826

Browse files
syntronadeas31
andauthored
Parser (#270)
* [OMParser] use a single entry point for OMParser * [OMTypedParser] use a single entry point for OMTypedParser * [OMCSessionBase] fix exception handling for pyparsing.ParseException * fix ex.message => ex.msg * [OMCSessionBase] simplify the two use cases of OMParser.om_parse_basic() * Return unchanged result from `_ask_with_fallback` Fix typo * [OMParser] use a single entry point for OMParser * [OMTypedParser] use a single entry point for OMTypedParser * [OMCSessionBase] fix exception handling for pyparsing.ParseException * fix ex.message => ex.msg * [OMCSessionBase] simplify the two use cases of OMParser.om_parse_basic() * 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` --------- Co-authored-by: Adeel Asghar <adeel.asghar@liu.se>
1 parent 5b21a06 commit 4af7826

File tree

2 files changed

+27
-35
lines changed

2 files changed

+27
-35
lines changed

OMPython/OMCSession.py

Lines changed: 15 additions & 35 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
@@ -81,9 +81,6 @@ def __init__(self, readonly=False):
8181
self._readonly = readonly
8282
self._omc_cache = {}
8383

84-
def clearOMParserResult(self):
85-
OMParser.result = {}
86-
8784
def execute(self, command):
8885
warnings.warn("This function is depreciated and will be removed in future versions; "
8986
"please use sendExpression() instead", DeprecationWarning, stacklevel=1)
@@ -197,7 +194,7 @@ def getClassComment(self, className):
197194
return self.ask('getClassComment', className)
198195
except pyparsing.ParseException as ex:
199196
logger.warning("Method 'getClassComment' failed for %s", className)
200-
logger.warning('OMTypedParser error: %s', ex.message)
197+
logger.warning('OMTypedParser error: %s', ex.msg)
201198
return 'No description available'
202199

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

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

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

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

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

274247
def getNthComponentModification(self, className, comp_id):
275248
# FIXME: OMPython exception Results KeyError exception
@@ -572,7 +545,14 @@ def sendExpression(self, command, parsed=True):
572545
else:
573546
result = self._omc.recv_string()
574547
if parsed is True:
575-
answer = OMTypedParser.parseString(result)
576-
return answer
548+
try:
549+
return om_parser_typed(result)
550+
except pyparsing.ParseException as ex:
551+
logger.warning('OMTypedParser error: %s. Returning the basic parser result.', ex.msg)
552+
try:
553+
return om_parser_basic(result)
554+
except (TypeError, UnboundLocalError) as ex:
555+
logger.warning('OMParser error: %s. Returning the unparsed result.', ex)
556+
return result
577557
else:
578558
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)