Skip to content
Snippets Groups Projects
Commit 45eba3c2 authored by Massimo Riello's avatar Massimo Riello
Browse files

Fawzi devised a nice way to compile and execute via trigger (in the...

Fawzi devised a nice way to compile and execute via trigger (in the ParserContext) a subparser script within the main one
(this is useful for collecting info from multiple output files).
parent 7dfa27d4
No related branches found
No related tags found
No related merge requests found
...@@ -174,15 +174,16 @@ class CachingDataManager(object): ...@@ -174,15 +174,16 @@ class CachingDataManager(object):
self.cachingLevel = cachingLevel self.cachingLevel = cachingLevel
class ActiveBackend(object): class ActiveBackend(object):
def __init__(self, metaInfoEnv, sectionManagers, dataManagers, superBackend): def __init__(self, metaInfoEnv, sectionManagers, dataManagers, superBackend, propagateStartFinishParsing = True):
self.__metaInfoEnv = metaInfoEnv self.__metaInfoEnv = metaInfoEnv
self.sectionManagers = sectionManagers self.sectionManagers = sectionManagers
self.dataManagers = dataManagers self.dataManagers = dataManagers
self.superBackend = superBackend self.superBackend = superBackend
self.propagateStartFinishParsing = propagateStartFinishParsing
@classmethod @classmethod
def activeBackend(cls, metaInfoEnv, cachingLevelForMetaName, defaultDataCachingLevel, defaultSectionCachingLevel = CachingLevel.Forward, superBackend = None, def activeBackend(cls, metaInfoEnv, cachingLevelForMetaName = {}, defaultDataCachingLevel = CachingLevel.ForwardAndCache, defaultSectionCachingLevel = CachingLevel.Forward, superBackend = None,
onClose = {}): onClose = {}, propagateStartFinishParsing = True):
for sectionName in onClose.keys(): for sectionName in onClose.keys():
if not sectionName in metaInfoEnv: if not sectionName in metaInfoEnv:
raise Exception("Found trigger for non existing section %s" % sectionName) raise Exception("Found trigger for non existing section %s" % sectionName)
...@@ -212,7 +213,7 @@ class ActiveBackend(object): ...@@ -212,7 +213,7 @@ class ActiveBackend(object):
(ik.name, superSectionNames)) (ik.name, superSectionNames))
dataManagers[ik.name] = CachingDataManager(ik, sectionManagers[superSectionNames[0]], dataManagers[ik.name] = CachingDataManager(ik, sectionManagers[superSectionNames[0]],
cachingLevelForMetaName.get(ik.name, defaultDataCachingLevel)) cachingLevelForMetaName.get(ik.name, defaultDataCachingLevel))
return ActiveBackend(metaInfoEnv, sectionManagers, dataManagers, superBackend) return ActiveBackend(metaInfoEnv, sectionManagers, dataManagers, superBackend, propagateStartFinishParsing)
def appendOnClose(self, sectionName, onClose): def appendOnClose(self, sectionName, onClose):
self.sectionManagers.onClose.append(onClose) self.sectionManagers.onClose.append(onClose)
...@@ -227,12 +228,12 @@ class ActiveBackend(object): ...@@ -227,12 +228,12 @@ class ActiveBackend(object):
def startedParsingSession(self, mainFileUri, parserInfo, parsingStatus = None, parsingErrors = None): def startedParsingSession(self, mainFileUri, parserInfo, parsingStatus = None, parsingErrors = None):
"""should be called when the parsing starts, parserInfo should be a valid json dictionary""" """should be called when the parsing starts, parserInfo should be a valid json dictionary"""
if self.superBackend: if self.propagateStartFinishParsing and self.superBackend:
self.superBackend.startedParsingSession(mainFileUri, parserInfo, parsingStatus, parsingErrors) self.superBackend.startedParsingSession(mainFileUri, parserInfo, parsingStatus, parsingErrors)
def finishedParsingSession(self, parsingStatus, parsingErrors, mainFileUri = None, parserInfo = None): def finishedParsingSession(self, parsingStatus, parsingErrors, mainFileUri = None, parserInfo = None):
"""should be called when the parsing finishes""" """should be called when the parsing finishes"""
if self.superBackend: if self.propagateStartFinishParsing and self.superBackend:
self.superBackend.finishedParsingSession(parsingStatus, parsingErrors, mainFileUri, parserInfo) self.superBackend.finishedParsingSession(parsingStatus, parsingErrors, mainFileUri, parserInfo)
def metaInfoEnv(self): def metaInfoEnv(self):
......
...@@ -674,6 +674,9 @@ class CompiledMatcher(object): ...@@ -674,6 +674,9 @@ class CompiledMatcher(object):
def findNextWithRe(self, regex, possible, parser): def findNextWithRe(self, regex, possible, parser):
# searching for match would be more efficient than doing line by line as here... # searching for match would be more efficient than doing line by line as here...
# catch empty regular expression
if regex.pattern == '':
return -3
while True: while True:
line = parser.fIn.readline() line = parser.fIn.readline()
if not line: if not line:
...@@ -1026,7 +1029,7 @@ class SimpleParser(object): ...@@ -1026,7 +1029,7 @@ class SimpleParser(object):
cNames = self.contextDesc() cNames = self.contextDesc()
while self.context: while self.context:
self.contextPop() self.contextPop()
if nextI != -1: if nextI != -1 and nextI != -3:
raise Exception("finished with error with parsing context %s" % (cNames)) raise Exception("finished with error with parsing context %s" % (cNames))
else: else:
index = nextI / 2 index = nextI / 2
...@@ -1070,16 +1073,17 @@ def compileParser(simpleParser, metaInfo, metaInfoToKeep): ...@@ -1070,16 +1073,17 @@ def compileParser(simpleParser, metaInfo, metaInfoToKeep):
def runParser(compiledParser, backend, superContext, fIn): def runParser(compiledParser, backend, superContext, fIn):
"""parses the open file fIn with the given compiledParser into the backend using superContext as parser SuperContext""" """parses the open file fIn with the given compiledParser into the backend using superContext as parser SuperContext"""
parser = compiledParser.buildParser(PushbackLineFile(fIn), backend, superContext = superContext) parser = compiledParser.buildParser(PushbackLineFile(fIn), backend, superContext = superContext)
try:
superContext.startedParsing(fIn.name, parser)
except AttributeError:
logging.exception("problem calling superContext.startedParsing")
pass
parser.parse() parser.parse()
def defaultParseFile(parserInfo): def defaultParseFile(parserInfo):
def parseF(parserBuilder, uri, path, backend, superContext): def parseF(parserBuilder, uri, path, backend, superContext):
with open(path, "r") as fIn: with open(path, "r") as fIn:
backend.startedParsingSession(uri, parserInfo) backend.startedParsingSession(uri, parserInfo)
try:
superContext.startedParsing(path)
except AttributeError:
pass
runParser(parserBuilder, backend, superContext, fIn) runParser(parserBuilder, backend, superContext, fIn)
backend.finishedParsingSession("ParseSuccess", None) backend.finishedParsingSession("ParseSuccess", None)
return parseF return parseF
...@@ -1092,12 +1096,13 @@ def mainFunction(mainFileDescription, metaInfoEnv, parserInfo, ...@@ -1092,12 +1096,13 @@ def mainFunction(mainFileDescription, metaInfoEnv, parserInfo,
defaultSectionCachingLevel = CachingLevel.Forward, defaultSectionCachingLevel = CachingLevel.Forward,
superContext = None, superContext = None,
onClose = {}): onClose = {}):
usage = """{exeName} [--meta-info] [--help] [--specialize] [--stream] [--uri uri] [path/toFile] usage = """{exeName} [--meta-info] [--help] [--specialize] [--stream] [--uri uri] [--verbose] [path/toFile]
--meta-info outputs the meta info supported by this parser --meta-info outputs the meta info supported by this parser
--help prints this message --help prints this message
--specialize expects inclusion and exclusion of meta info to parse via a json dictionary on stdin --specialize expects inclusion and exclusion of meta info to parse via a json dictionary on stdin
--stream expects the files to parse via dictionary on stdin --stream expects the files to parse via dictionary on stdin
--verbose writes metainfo to stderr and detailed debug info of parsing process to file detailed.log
If a path to a file is given this is parsed If a path to a file is given this is parsed
""".format(exeName = os.path.basename(sys.argv[0] if len(sys.argv) > 0 else "simple_parser")) """.format(exeName = os.path.basename(sys.argv[0] if len(sys.argv) > 0 else "simple_parser"))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment