[Patchew-devel] [PATCH] custom_tags: add lines_between tag

Paolo Bonzini posted 1 patch 6 years, 3 months ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/patchew-ci tags/patchew/20180404151608.22886-1-pbonzini@redhat.com
patchew/tags.py           | 23 +++++++++++++++++++++++
tests/test_custom_tags.py | 10 +++++++++-
2 files changed, 32 insertions(+), 1 deletion(-)
[Patchew-devel] [PATCH] custom_tags: add lines_between tag
Posted by Paolo Bonzini 6 years, 3 months ago
This tag can be used to extract sections of the tester's log, for example
the test script or the output.

Fixes: https://github.com/patchew-project/patchew/issues/31
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 patchew/tags.py           | 23 +++++++++++++++++++++++
 tests/test_custom_tags.py | 10 +++++++++-
 2 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/patchew/tags.py b/patchew/tags.py
index 4847cb4..93a6f7e 100644
--- a/patchew/tags.py
+++ b/patchew/tags.py
@@ -62,6 +62,25 @@ def grep_iter(value, regex, n_before, n_after, sep):
     for i in range(max(lineno - n, 0), min(stop, lineno)):
         yield lines[i % n]
 
+# Similar to sed "/abc/,/def/p" except that the last line can
+# be excluded.
+def lines_between_iter(value, start, stop, include_last=True):
+    inside = False
+    for line in lines_iter(value):
+        if inside and re.search(stop, line):
+            inside = False
+            if include_last:
+                yield line
+                continue
+            # If include_last is false, immediately look
+            # for the next range.
+
+        if not inside and re.search(start, line):
+            inside = True
+
+        if inside:
+            yield line
+
 register = template.Library()
 
 @register.simple_tag
@@ -94,3 +113,7 @@ def grep_B(value, regex, n=3, sep='---'):
 @register.filter
 def grep_C(value, regex, n=3, sep='---'):
     return '\n'.join(grep_iter(value, regex, n, n, sep))
+
+@register.simple_tag
+def lines_between(value, start, stop, include_last=True):
+    return '\n'.join(lines_between_iter(value, start, stop, include_last))
diff --git a/tests/test_custom_tags.py b/tests/test_custom_tags.py
index 9ed2117..c401d0e 100755
--- a/tests/test_custom_tags.py
+++ b/tests/test_custom_tags.py
@@ -9,7 +9,7 @@
 # http://opensource.org/licenses/MIT.
 
 from django.template import Context, Template
-from patchew.tags import tail_lines, grep_A, grep_B, grep_C, grep
+from patchew.tags import tail_lines, grep_A, grep_B, grep_C, grep, lines_between
 import unittest
 
 class CustomTagsTest(unittest.TestCase):
@@ -38,6 +38,7 @@ class CustomTagsTest(unittest.TestCase):
         self.assertTemplate('{% grep_B s regex="[bc]" n=1 %}', 'a\nb\nc', s='a\nb\nc\nd')
         self.assertTemplate('{% grep_C s "b" n=1 %}', 'a\nb\nc', s='a\nb\nc\nd')
         self.assertTemplate('{% tail_lines s n=3 %}', 'b\nc\nd', s='a\nb\nc\nd')
+        self.assertTemplate('{% lines_between s start="^b$" stop="c" %}', 'b\nc', s='a\nb\nc\nd')
 
     def test_grep(self):
         self.assertEqual(grep('0\na\n9', '[0-9]'), '0\n9')
@@ -124,5 +125,12 @@ class CustomTagsTest(unittest.TestCase):
         self.assertEqual(tail_lines('\n\nbc', 3), '\n\nbc')
         self.assertEqual(tail_lines('\n\n\n\nbc', 3), '\n\nbc')
 
+    def test_lines_between(self):
+        self.assertEqual(lines_between('a\nb\nc\nd', 'b', 'c'), 'b\nc')
+        self.assertEqual(lines_between('a\nb\nc\nd', 'b', 'c', False), 'b')
+        self.assertEqual(lines_between('a\nb\ncb\nd', 'b', 'c'), 'b\ncb')
+        self.assertEqual(lines_between('a\nb\ncb\nd', 'b', 'c', False), 'b\ncb\nd')
+        self.assertEqual(lines_between('a\nb\n\n\na\nb', '.', '^$'), 'a\nb\n\na\nb')
+
 if __name__ == '__main__':
     unittest.main()
-- 
2.16.2

_______________________________________________
Patchew-devel mailing list
Patchew-devel@redhat.com
https://www.redhat.com/mailman/listinfo/patchew-devel