support multi-line search in grep parser 53/29053/8
authorwu.zhihui <wu.zhihui1@zte.com.cn>
Tue, 21 Feb 2017 08:35:41 +0000 (16:35 +0800)
committerwu.zhihui <wu.zhihui1@zte.com.cn>
Tue, 21 Feb 2017 08:35:41 +0000 (16:35 +0800)
use finditer with multiline mode

JIRA: QTIP-211

Change-Id: Ib8854f749a0258f6b6775be9b80573f6ac8e47db
Signed-off-by: wu.zhihui <wu.zhihui1@zte.com.cn>
qtip/collector/parser/grep.py
tests/unit/collector/grep_test.py

index d7ada48..c3274bc 100644 (file)
@@ -29,5 +29,6 @@ class GrepParser(BaseActor):
 
 
 def grep_in_file(filename, regex):
-    with open(filename, 'r') as f:
-        return filter(lambda x: x is not None, [re.search(regex, line) for line in f])
+    with open(filename, "r") as outfile:
+        return filter(lambda x: x is not None,
+                      list(re.finditer(regex, outfile.read(), re.MULTILINE)))
index e5d5f8c..2d4079a 100644 (file)
@@ -21,7 +21,9 @@ def logfile(data_root):
 @pytest.mark.parametrize("regex,expected", [
     ('not exist', []),
     ('Lorem (\S+)', [{'groups': ('ipsum',), 'groupdict': {}}]),
-    ('nisi ut (?P<name>\S+)', [{'groups': ('aliquip',), 'groupdict': {'name': 'aliquip'}}])
+    ('nisi ut (?P<name>\S+)', [{'groups': ('aliquip',), 'groupdict': {'name': 'aliquip'}}]),
+    ('Lorem\s(\w+)\s.+\nconsectetur\s(\w+)\s.+\n',
+     [{'groups': ('ipsum', 'adipiscing',), 'groupdict': {}}])
 ])
 def test_grep_in_file(logfile, regex, expected):
     matches = grep_in_file(logfile, regex)