From 4ee42ae6f5d5fc822e4fd6a9a3fa783d6902f0db Mon Sep 17 00:00:00 2001
From: Berk Onat <b.onat@warwick.ac.uk>
Date: Fri, 30 Mar 2018 20:02:38 +0100
Subject: [PATCH] Added new replay settings to SmartParserCommon

---
 .../smart_parser/SmartParserCommon.py         | 71 ++++++++++++++-----
 1 file changed, 55 insertions(+), 16 deletions(-)

diff --git a/common/python/nomadcore/smart_parser/SmartParserCommon.py b/common/python/nomadcore/smart_parser/SmartParserCommon.py
index ebfe39e..346dc8e 100644
--- a/common/python/nomadcore/smart_parser/SmartParserCommon.py
+++ b/common/python/nomadcore/smart_parser/SmartParserCommon.py
@@ -988,16 +988,22 @@ class ParserBase(object):
         if "tableendsat" in parserOptions:
             tableEndRe = re.compile(parserOptions["tableendsat"])
         stopOnMatch = False
-        if stopOnMatchRe.findall(lastLine):
-            stopOnMatch = True
-            if parserDict["firstLine"] == 0:
-                if stopOnFirstLine: 
-                    stopOnMatch = True
-                else:
-                    stopOnMatch = False
-        if quitOnMatchRe is not None:
-            if quitOnMatchRe.findall(lastLine):
+        if isinstance(stopOnMatchRe, str):
+            pass
+        else:
+            if stopOnMatchRe.findall(lastLine):
                 stopOnMatch = True
+                if parserDict["firstLine"] == 0:
+                    if stopOnFirstLine: 
+                        stopOnMatch = True
+                    else:
+                        stopOnMatch = False
+        if quitOnMatchRe is not None:
+            if isinstance(stopOnMatchRe, str):
+                pass
+            else:
+                if quitOnMatchRe.findall(lastLine):
+                    stopOnMatch = True
         if tableEndRe is not None:
             if tableEndRe.findall(lastLine):
                 stopOnMatch = True
@@ -1116,14 +1122,16 @@ class ParserBase(object):
                             cName=cName.upper()
                             hlist=[h.upper() for h in hlist]
                         if cName in hlist:
-                            v=vlist[hlist.index(cName)]
                             #print("PRINTING table_parser save:",cName,v)
-                            if key in list(parser.lastMatch.keys()):
-                                parser.lastMatch[key]=v
-                            else:
-                                mNameDict[key].value=v
-                                mNameDict[key].activeInfo=True
-                            anythingmatched = True
+                            hindex = hlist.index(cName)
+                            if hindex < len(vlist):
+                                v=vlist[hlist.index(cName)]
+                                if key in list(parser.lastMatch.keys()):
+                                    parser.lastMatch[key]=v
+                                else:
+                                    mNameDict[key].value=v
+                                    mNameDict[key].activeInfo=True
+                                anythingmatched = True
                 else:
                     if(header is not None and headersave is not None):
                         hlistc=0
@@ -1227,6 +1235,7 @@ class ParserBase(object):
         parsercntrlin = None
         lookupdict = None
         lookupvals = None
+        maxLines = None
         if "movetostopline" in parserOptions:
             updateLastLine = parserOptions["movetostopline"]
         if "lookupdict" in parserOptions:
@@ -1243,6 +1252,9 @@ class ParserBase(object):
                     parsercntrlin = getattr(self,parserOptions["parsercntrlin"])
                 else:
                     parsercntrlin = parserOptions["parsercntrlin"]
+        if "maxtablelines" in parserOptions:
+            maxLines = parserOptions["maxtablelines"] if parserOptions[
+                    "maxtablelines"]>0 else None
 
         if(parsercntrlattr is not None and
             parsercntrlin is not None):
@@ -1264,6 +1276,9 @@ class ParserBase(object):
                     matchNameDict, updateMatchDict, 
                     onlyCaseSensitive, stopOnFirstLine, 
                     parserDict, parserOptions, globalDict, localDict) 
+            if maxLines is not None:
+                if maxLines >= parserDict["numStoredLines"]:
+                    rtn = True
             if rtn is not True:
                 while True:
                     if record and replayCount>0:
@@ -1284,6 +1299,9 @@ class ParserBase(object):
                                 matchNameDict, updateMatchDict, 
                                 onlyCaseSensitive, stopOnFirstLine, 
                                 parserDict, parserOptions, globalDict, localDict)
+                        if maxLines is not None:
+                            if maxLines >= parserDict["numStoredLines"]:
+                                rtn = True
                         if rtn:
                             if updateLastLine:
                                 # Playing from record?
@@ -2000,12 +2018,24 @@ class ParserBase(object):
     def check_subparsers(self, parser, lastLine, stopOnMatchRe, quitOnMatchRe, subParsers, 
             ordered, parserDict, secDict, onStartRunFunction, onQuitRunFunction, 
             onlySubParsersReadLine, maxcycle=None, parent=None):
+        record = None
+        replay = None
+        replayCount = None
+        if parserDict is not None:
+            record = parserDict["record"]
+            replay = parserDict["replay"]
+            replayCount = parserDict["replayCount"]
+        if record is None:
+            record = False
+        if replayCount is None:
+            replayCount = 0
         if maxcycle is None:
             maxcycle=1000000
         cyclenum = 0
         if parent is None:
             parent = 0
         rank = parent
+        stopOnEOF = True
         waitFirstCycle = False
         if "waitFirstCycle" in parserDict:
             waitFirstCycle = parserDict["waitFirstCycle"]
@@ -2151,6 +2181,15 @@ class ParserBase(object):
                 # reached than quit loop
                 if cyclenum > maxcycle:
                     break
+            if stopOnEOF:
+                eofLine = None
+                if record and replayCount>0:
+                    eofLine = self.peekrecord()
+                else:
+                    eofLine = self.peekline(parser)
+                if not eofLine:
+                    stopOnMatch = True
+                    break
 
         if stopOnMatch:
             return True, lastLine, parserDict, secDict
-- 
GitLab