Skip to content
Snippets Groups Projects
Commit 797813bd authored by Henning Glawe's avatar Henning Glawe
Browse files

move handling of coverageIgnore to annotator

parent db3a73e8
No related branches found
No related tags found
No related merge requests found
import logging
from nomadcore.match_highlighter import MatchHighlighter
from nomadcore.match_highlighter import MatchHighlighter, ANSI
LOGGER = logging.getLogger(__name__)
......@@ -19,21 +19,23 @@ class Annotator(object):
def annotate(self,match,line,matcher,targetStartEnd):
if not self.annotateFile:
return 1
(matchtype, highlightedLine) = self.matchHighlighter.highlight(
match, line, self.coverageIgnore)
(matched, full, highlightedLine) = self.matchHighlighter.highlight(
match, line)
matchlabel = ''
if matchtype == 0:
matchlabel='ign'
elif matchtype == 1:
matchlabel = 'no'
else:
if matched:
# we matched
if matchtype == 2:
if not full:
matchlabel = 'p_'
if targetStartEnd==0:
if targetStartEnd == 0:
matchlabel += 'start'
else:
matchlabel += 'end'
else:
if self.coverageIgnore.match(line):
matchlabel = 'ign'
highlightedLine = ANSI.FG_BLUE + highlightedLine
else:
matchlabel = 'no'
if matcher.name:
name=matcher.name[-15:]
......
......@@ -16,12 +16,15 @@ RE_finalnewlines=re.compile(r"^(.*?)(\n*)$")
class MatchHighlighter(object):
"""ANSI-Color highlighting for capturing groups in regex matches"""
def highlight(self, match, lineOrig, coverageIgnore, multiline=False):
""":returns: (int, str_including_ANSI_escapes)
0 - RE did not match, and line was part of coverage-ignored
1 - RE did not match, and line was not part of coverage-ignored
2 - RE matched partially
3 - RE matched fully"""
def highlight(self, match, lineOrig, multiline=False):
"""
:returns: (matched, fullmatch, highlighed)
:rtype: (bool, bool, str)
matched: true if matched at all
fullmatch: all characters of lineOrig were covered by match
hightlighted: string containing ANSI escapes (at least ANSI RESET at
end of string)
"""
if multiline:
line = lineOrig
trailing = ''
......@@ -33,10 +36,7 @@ class MatchHighlighter(object):
trailing = m.group(2)
# shortcut: there is nothing to highlight
if not match:
if coverageIgnore and coverageIgnore.match(lineOrig):
return (0, ANSI.FG_BLUE + line + ANSI.RESET + trailing)
else:
return (1, lineOrig)
return (False, False, line + ANSI.RESET + trailing)
else:
match_full = match.start() == 0 and match.end() == len(lineOrig)
......@@ -81,7 +81,4 @@ class MatchHighlighter(object):
highlightedLine+=ansiSwitch[i][2]
# re-append trailing newlines if we stripped them
highlightedLine += trailing
if match_full:
return(3, highlightedLine)
else:
return(2, highlightedLine)
return(True, match_full, highlightedLine)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment