Skip to content
Snippets Groups Projects
Commit d11d3efe authored by Lauri Himanen's avatar Lauri Himanen
Browse files

Added some convenience functions to CachingBackend/CachingSectionManager,...

Added some convenience functions to CachingBackend/CachingSectionManager, added new 'endAction' argument for SimpleMatchers.
parent 40e42f75
No related branches found
No related tags found
No related merge requests found
...@@ -272,7 +272,7 @@ class RegexService(object): ...@@ -272,7 +272,7 @@ class RegexService(object):
Stores basic regex definitions that can be reused on multiple parsers. Stores basic regex definitions that can be reused on multiple parsers.
""" """
def __init__(self): def __init__(self):
self.float = "-?\d+\.\d+(?:E(?:\+|-)\d+)?" # Regex for a floating point value self.float = "-?\d+\.\d+(?:[ED](?:\+|-)\d+)?" # Regex for a floating point value
self.int = "-?\d+" # Regex for an integer self.int = "-?\d+" # Regex for an integer
self.word = "[\S]+" # Regex for a single word. Can contain anything else but whitespace self.word = "[\S]+" # Regex for a single word. Can contain anything else but whitespace
self.letter = "[^\W\d_]" # Regex for a single alphabetical letter self.letter = "[^\W\d_]" # Regex for a single alphabetical letter
......
...@@ -178,6 +178,12 @@ class CachingSectionManager(object): ...@@ -178,6 +178,12 @@ class CachingSectionManager(object):
def setSectionInfo(self, gIndex, references): def setSectionInfo(self, gIndex, references):
self.openSections[gIndex].references = [reference[x] for x in self.parentSectionNames] self.openSections[gIndex].references = [reference[x] for x in self.parentSectionNames]
def get_latest_section(self):
"""Returns the latest opened section if it is still open.
"""
section = self.openSections.get(self.lastSectionGIndex)
return section
def openSection(self, backend): def openSection(self, backend):
newGIndex = self.lastSectionGIndex + 1 newGIndex = self.lastSectionGIndex + 1
self.openSectionWithGIndex(backend, newGIndex) self.openSectionWithGIndex(backend, newGIndex)
...@@ -317,6 +323,14 @@ class ActiveBackend(object): ...@@ -317,6 +323,14 @@ class ActiveBackend(object):
def appendOnClose(self, sectionName, onClose): def appendOnClose(self, sectionName, onClose):
self.sectionManagers.onClose.append(onClose) self.sectionManagers.onClose.append(onClose)
def get_latest_section(self, metaname):
"""Returns the latest opened section with the given metaname if it has
not been closed.
"""
manager = self.sectionManagers.get(metaname)
if manager:
return manager.get_latest_section()
def appendOnOpen(self, sectionName, onOpen): def appendOnOpen(self, sectionName, onOpen):
self.sectionManagers.onOpen.append(onOpen) self.sectionManagers.onOpen.append(onOpen)
......
...@@ -74,6 +74,7 @@ class SimpleMatcher(object): ...@@ -74,6 +74,7 @@ class SimpleMatcher(object):
defLine=0, defLine=0,
defFile='', defFile='',
coverageIgnore=False, # mark line as ignored in coverage analysis coverageIgnore=False, # mark line as ignored in coverage analysis
endAction = None,
): ):
self.index = -1 self.index = -1
self.startReStr = startReStr self.startReStr = startReStr
...@@ -118,6 +119,7 @@ class SimpleMatcher(object): ...@@ -118,6 +119,7 @@ class SimpleMatcher(object):
"and is marked as coverageIgnore", name) "and is marked as coverageIgnore", name)
self.coverageIgnore = coverageIgnore self.coverageIgnore = coverageIgnore
self.endAction = endAction
caller=inspect.currentframe() caller=inspect.currentframe()
if (defFile == '') and (defLine == 0): if (defFile == '') and (defLine == 0):
if (caller is not None) and (caller.f_back is not None): if (caller is not None) and (caller.f_back is not None):
...@@ -679,10 +681,12 @@ class CompiledMatcher(object): ...@@ -679,10 +681,12 @@ class CompiledMatcher(object):
parser.skipped += 1 parser.skipped += 1
def findNextMatch(self, parser): def findNextMatch(self, parser):
nextI = None
if self.matcher.adHoc: if self.matcher.adHoc:
nextI = self.matcher.adHoc(parser) nextI = self.matcher.adHoc(parser)
if nextI is not None:
return nextI if nextI is not None:
return nextI
return self.findNextWithRe(self.possibleNextsRe, self.possibleNexts, parser) return self.findNextWithRe(self.possibleNextsRe, self.possibleNexts, parser)
def findNextMatchEnd(self, parser): def findNextMatchEnd(self, parser):
...@@ -1088,6 +1092,11 @@ class SimpleParser(object): ...@@ -1088,6 +1092,11 @@ class SimpleParser(object):
self.context.append(ParsingContext(stateIndex, sects, compiledMatcher, ParsingContext.Start)) self.context.append(ParsingContext(stateIndex, sects, compiledMatcher, ParsingContext.Start))
def contextClose(self, cNow): def contextClose(self, cNow):
# Handle end action before closing sections
# Call the end action
if cNow.compiledMatcher.matcher.endAction:
cNow.compiledMatcher.matcher.endAction()
if cNow.startEnd == ParsingContext.Start: if cNow.startEnd == ParsingContext.Start:
for s,gIndex in reversed(list(cNow.sections.items())): for s,gIndex in reversed(list(cNow.sections.items())):
self.backend.closeSection(s, gIndex) self.backend.closeSection(s, gIndex)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment