From 8a183ab774823c203aa9ccb23dd16ff2a0e2e816 Mon Sep 17 00:00:00 2001
From: Henning Glawe <glaweh@debian.org>
Date: Fri, 15 Jul 2016 22:20:44 +0200
Subject: [PATCH] setup CompiledMatcher.transformers from
 parserBuilder.strValueTransform

transformers assign custom strValueTransform functions to named groups
in SimpleMatcher regular expressions.
Implemented analogously to the existing 'converters' mechanism
for unit conversion.
---
 common/python/nomadcore/simple_parser.py | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/common/python/nomadcore/simple_parser.py b/common/python/nomadcore/simple_parser.py
index f23d1bc..8ab4885 100644
--- a/common/python/nomadcore/simple_parser.py
+++ b/common/python/nomadcore/simple_parser.py
@@ -510,12 +510,20 @@ class CompiledMatcher(object):
         self.possibleNextsEnd = possibleNextsEnd
         self.possibleNextsRe = self.matchersToRe(possibleNexts)
         self.possibleNextsEndRe = self.matchersToRe(possibleNextsEnd)
+        strValueTransform = parserBuilder.strValueTransform
+        if strValueTransform is None:
+            strValueTransform = {}
         converters = {}
+        transformers = {}
         groups = extractGroupNames(matcher.startReStr)
         if matcher.endReStr:
             groups.extend(extractGroupNames(matcher.endReStr))
         for gName, units in groups:
             metaInfo = parserBuilder.metaInfoEnv.infoKinds[gName]
+            if units in strValueTransform:
+                # override units by output units of string transform function
+                transformers[gName] = strValueTransform[units][0]
+                units = strValueTransform[units][1]
             if units:
                 # By default use the unit given in metainfo
                 target_unit = metaInfo.units
@@ -535,6 +543,7 @@ class CompiledMatcher(object):
 
                 converters[gName] = unit_conversion.convert_unit_function(units, target_unit)
         self.converters = converters
+        self.transformers = transformers
 
     def addStrValue(self, backend, metaNameWithUnits, strValue):
         """adds a string value with unit conversions (only for the groups in start and endRe)"""
-- 
GitLab