From nobody Sat Apr 20 11:01:57 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=patchew-devel-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=patchew-devel-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1522854976350311.45338353131274; Wed, 4 Apr 2018 08:16:16 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 5D69E7E9C0; Wed, 4 Apr 2018 15:16:15 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 4795282AC4; Wed, 4 Apr 2018 15:16:13 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id B60F8ADABE; Wed, 4 Apr 2018 15:16:13 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w34FGCWq028982 for ; Wed, 4 Apr 2018 11:16:12 -0400 Received: by smtp.corp.redhat.com (Postfix) id 23D5210B2B5D; Wed, 4 Apr 2018 15:16:12 +0000 (UTC) Received: from donizetti.redhat.com (ovpn-117-172.ams2.redhat.com [10.36.117.172]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7CBD610B2B51 for ; Wed, 4 Apr 2018 15:16:08 +0000 (UTC) From: Paolo Bonzini To: patchew-devel@redhat.com Date: Wed, 4 Apr 2018 17:16:08 +0200 Message-Id: <20180404151608.22886-1-pbonzini@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.3 X-loop: patchew-devel@redhat.com Subject: [Patchew-devel] [PATCH] custom_tags: add lines_between tag X-BeenThere: patchew-devel@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Patchew development and discussion list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: patchew-devel-bounces@redhat.com Errors-To: patchew-devel-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Wed, 04 Apr 2018 15:16:15 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" 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 --- 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] =20 +# Similar to sed "/abc/,/def/p" except that the last line can +# be excluded. +def lines_between_iter(value, start, stop, include_last=3DTrue): + inside =3D False + for line in lines_iter(value): + if inside and re.search(stop, line): + inside =3D 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 =3D True + + if inside: + yield line + register =3D template.Library() =20 @register.simple_tag @@ -94,3 +113,7 @@ def grep_B(value, regex, n=3D3, sep=3D'---'): @register.filter def grep_C(value, regex, n=3D3, sep=3D'---'): return '\n'.join(grep_iter(value, regex, n, n, sep)) + +@register.simple_tag +def lines_between(value, start, stop, include_last=3DTrue): + 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. =20 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_b= etween import unittest =20 class CustomTagsTest(unittest.TestCase): @@ -38,6 +38,7 @@ class CustomTagsTest(unittest.TestCase): self.assertTemplate('{% grep_B s regex=3D"[bc]" n=3D1 %}', 'a\nb\n= c', s=3D'a\nb\nc\nd') self.assertTemplate('{% grep_C s "b" n=3D1 %}', 'a\nb\nc', s=3D'a\= nb\nc\nd') self.assertTemplate('{% tail_lines s n=3D3 %}', 'b\nc\nd', s=3D'a\= nb\nc\nd') + self.assertTemplate('{% lines_between s start=3D"^b$" stop=3D"c" %= }', 'b\nc', s=3D'a\nb\nc\nd') =20 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') =20 + 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__ =3D=3D '__main__': unittest.main() --=20 2.16.2 _______________________________________________ Patchew-devel mailing list Patchew-devel@redhat.com https://www.redhat.com/mailman/listinfo/patchew-devel