diff --git a/common/python/nomadcore/simple_parser.py b/common/python/nomadcore/simple_parser.py index 91175134038c5f23109fa0b9deef35435f34d50a..7762fa42294f714036b6b62f0e30e47a1b17d6ad 100644 --- a/common/python/nomadcore/simple_parser.py +++ b/common/python/nomadcore/simple_parser.py @@ -553,15 +553,15 @@ class CompiledMatcher(object): value = transformer(strValue) else: value = backend.convertScalarStringValue(metaName, strValue) - self.addValue(backend, metaName, value) + return metaName, self.addValue(backend, metaName, value) def addValue(self, backend, metaName, value): """adds a value with unit conversions (only for the groups in start and endRe)""" converter = self.converters.get(metaName, None) if converter: - backend.addValue(metaName, converter(value)) - else: - backend.addValue(metaName, value) + value = converter(value) + backend.addValue(metaName, value) + return value def toDict(self): res = { @@ -581,30 +581,38 @@ class CompiledMatcher(object): if not m: raise Exception("Expected to match %s on %r" % (self.startRe.pattern, line)) self.annotate(m,line,parser,self.matcher,0) + result_dict = {} for k,v in m.groupdict().items(): if v is None: # a group may be optional (subexpression of ? or | in regex) continue - self.addStrValue(parser.backend, k, v) + k_converted, v_converted = self.addStrValue(parser.backend, k, v) + result_dict[k_converted] = v_converted if self.matcher.fixedStartValues: for k,v in self.matcher.fixedStartValues.items(): - self.addStrValue(parser.backend, k, v) + k_converted, v_converted = self.addStrValue(parser.backend, k, v) + result_dict[k_converted] = v_converted if self.matcher.forwardMatch: logger.debug("handleStartMatch of %s on (%s) pushing back line", self.matcher.desc(),line) parser.fIn.pushbackLine(line) + return result_dict def handleEndMatch(self, parser): line = parser.fIn.readline() m = self.endRe.match(line) #, re.MULTILINE) self.annotate(m,line,parser,self.matcher,1) + result_dict = {} for k,v in m.groupdict().items(): if v is None: # a group may be optional (subexpression of ? or | in regex) continue - self.addStrValue(parser.backend, k, v) + k_converted, v_converted = self.addStrValue(parser.backend, k, v) + result_dict[k_converted] = v_converted if self.matcher.fixedEndValues: for k,v in self.matcher.fixedEndValues.items(): - self.addStrValue(parser.backend, k, v) + k_converted, v_converted = self.addStrValue(parser.backend, k, v) + result_dict[k_converted] = v_converted + return result_dict def annotate(self,match,line,parser,matcher,targetStartEnd): if not parser.annotateFile: @@ -981,6 +989,7 @@ class SimpleParser(object): self.superContext = superContext self.annotateFile = None self.matchHighlighter = MatchHighlighter(highlightmode) + self.lastMatch = {} if annotate: annofilename=fIn.fIn.name + ".annotate" logger.info("writing annotated input to " + annofilename) @@ -1072,10 +1081,10 @@ class SimpleParser(object): logger.debug("new context: %s\n", self.contextDesc()) currentCtx = self.context[len(self.context) - 1] if startEnd == ParsingContext.End: - currentCtx.compiledMatcher.handleEndMatch(self) + self.lastMatch = currentCtx.compiledMatcher.handleEndMatch(self) self.contextClose(currentCtx) else: - currentCtx.compiledMatcher.handleStartMatch(self) + self.lastMatch = currentCtx.compiledMatcher.handleStartMatch(self) except Exception as e: origin = traceback.format_exc() raise_from(Exception("Failure, context %s, line %d coming from %s"%(self.contextDesc(), self.fIn.lineNr, origin)), e)