From nobody Mon Dec 9 17:31:07 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; dkim=fail; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=patchew-devel-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1520496061145156.1656092560952; Thu, 8 Mar 2018 00:01:01 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3901A804F4; Thu, 8 Mar 2018 08:01:00 +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 2157217967; Thu, 8 Mar 2018 08:01:00 +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 1773E4CA99; Thu, 8 Mar 2018 08:01:00 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w2880woE032409 for ; Thu, 8 Mar 2018 03:00:58 -0500 Received: by smtp.corp.redhat.com (Postfix) id 87869608F2; Thu, 8 Mar 2018 08:00:58 +0000 (UTC) Received: from mx1.redhat.com (ext-mx05.extmail.prod.ext.phx2.redhat.com [10.5.110.29]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 7D3A6614D0 for ; Thu, 8 Mar 2018 08:00:58 +0000 (UTC) Received: from mail-wr0-f182.google.com (mail-wr0-f182.google.com [209.85.128.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3440237E7B for ; Thu, 8 Mar 2018 08:00:50 +0000 (UTC) Received: by mail-wr0-f182.google.com with SMTP id z12so4672211wrg.4 for ; Thu, 08 Mar 2018 00:00:50 -0800 (PST) Received: from donizetti.lan (94-36-191-219.adsl-ull.clienti.tiscali.it. [94.36.191.219]) by smtp.gmail.com with ESMTPSA id b9sm21755912wrh.68.2018.03.08.00.00.47 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 08 Mar 2018 00:00:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:subject:date:message-id:in-reply-to:references; bh=eesPaABE/2R7Xuy0SUeJVfJACgFxBGegOWgazei0C7c=; b=SllRXVhKyq/E7PZMzJKCi91SWla6oHkXVcOnHzPPW64RzVmP9TtUk/yuvZVhiVIagl ufYJNtmjeLIlLC4iMt6sGmIrJHNLdzfzopHAoIM2WD2+7Y/4V65bmjZWoy5hXd6O1NIm oq2roDOcwX4IrkagtUpzFehhLlAEiwxXbpUO+VOJ8/E3zCcPi55QavNJ5nV3hauRtQbN 4GTXKr3ZlCZbJHXhIC0UAvh2PZTNafpYEyEFltMiXacSEU2v826LYlQYRZEBVce54hVH zXkmeArY3KqSlpC+G4gDAIy0YCdYfQT6EHZlYKnmwULP8uXnFqakPKcBmQc3506GnzgS mcPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:subject:date:message-id :in-reply-to:references; bh=eesPaABE/2R7Xuy0SUeJVfJACgFxBGegOWgazei0C7c=; b=il1i0e5O/6Qmv9ebYw7deEWIUzk06kGz9pDq5EbB0jm2L/GL2jEW8cET3a/O4uT/3K 5i/qi+Cv4i7I3XmkGJaJZlyhnsfv3wLGQBtMA2RWzG3XTXE62GDpkMmIIrM5+e28MDUZ jv+MTM9SlHNBR9anVKjMKTDQGFtJ/8T1GMECJB4iQUiaoAAGwhJjf/hvJvadzTVQjuVT Z2/pim68h7MQ1kwmXVbC9Xp7V4TE+7gntVVGKafHmo7QTj0dX+QMPD/sevlaGbLvom36 utMfzmHrsAAPqPhlrzrsrIDZL9xS8loSTBe8TCJIDJTDHggKfn9obFVurydYAxARIitn zkRA== X-Gm-Message-State: APf1xPCj+NxtRIvBCR71Y4zTH/u8aL4PHN7abpdogxTbUXbat/e9F4TQ Lu5sXEboAVh94D8+1DVT/KtPf4yk X-Google-Smtp-Source: AG47ELs2P6Tor340jwDGxOc0CL/1CHXx4I4sKsJcd7ZCunTv8+nOsObmalvslpzgiKsFNdTtYzugLw== X-Received: by 10.223.153.230 with SMTP id y93mr20881553wrb.215.1520496048311; Thu, 08 Mar 2018 00:00:48 -0800 (PST) From: Paolo Bonzini To: patchew-devel@redhat.com Date: Thu, 8 Mar 2018 09:00:38 +0100 Message-Id: <20180308080042.19342-4-pbonzini@redhat.com> In-Reply-To: <20180308080042.19342-1-pbonzini@redhat.com> References: <20180308080042.19342-1-pbonzini@redhat.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Thu, 08 Mar 2018 08:00:50 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Thu, 08 Mar 2018 08:00:50 +0000 (UTC) for IP:'209.85.128.182' DOMAIN:'mail-wr0-f182.google.com' HELO:'mail-wr0-f182.google.com' FROM:'paolo.bonzini@gmail.com' RCPT:'' X-RedHat-Spam-Score: 0.488 (DKIM_SIGNED, DKIM_VALID, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_PASS) 209.85.128.182 mail-wr0-f182.google.com 209.85.128.182 mail-wr0-f182.google.com X-RedHat-Possible-Forgery: Paolo Bonzini X-Scanned-By: MIMEDefang 2.78 on 10.5.110.29 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: patchew-devel@redhat.com Subject: [Patchew-devel] [PATCH 3/7] ansi2html: create ANSI to text converter 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.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Thu, 08 Mar 2018 08:01:00 +0000 (UTC) X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" This converter compresses sequences involving carriage returns or backspaces, to make them readable from an editor or mail reader. Signed-off-by: Paolo Bonzini --- patchew/logviewer.py | 29 ++++++++++++++++++ patchew/settings.py | 3 ++ patchew/tags.py | 19 ++++++++++++ tests/test_ansi2html.py | 75 +++++++++++++++++++++++++++++++++++++++++++= +++- tests/test_custom_tags.py | 27 +++++++++++++++++ 5 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 patchew/tags.py create mode 100755 tests/test_custom_tags.py diff --git a/patchew/logviewer.py b/patchew/logviewer.py index aa5d2bb..fe0f3ca 100644 --- a/patchew/logviewer.py +++ b/patchew/logviewer.py @@ -286,6 +286,29 @@ class ANSIProcessor(object): self._reset_attrs() =20 =20 +class ANSI2TextConverter(ANSIProcessor): + FF =3D '\u2500' * 72 + '\n' + SYMBOLS =3D { + '\x00' : '\u2400', '\x01' : '\u2401', '\x02' : '\u2402', + '\x03' : '\u2403', '\x04' : '\u2404', '\x05' : '\u2405', + '\x06' : '\u2406', '\x07' : '\U00001F514', '\x0B' : '\u240B', + '\x0E' : '\u240E', '\x0F' : '\u240F', '\x10' : '\u2410', + '\x11' : '\u2411', '\x12' : '\u2412', '\x13' : '\u2413', + '\x14' : '\u2414', '\x15' : '\u2415', '\x16' : '\u2416', + '\x17' : '\u2417', '\x18' : '\u2418', '\x19' : '\u2419', + '\x1A' : '\u241A', '\x1B' : '\u241B', '\x1C' : '\u241C', + '\x1D' : '\u241D', '\x1E' : '\u241E', '\x1F' : '\u241F', + '\x7F' : '\u2326' + } + RE_SYMBOLS =3D re.compile('[\x00-\x1F\x7F]') + + def _write_span(self, text, class_id): + yield self.RE_SYMBOLS.sub(lambda x: self.SYMBOLS[x.group(0)], text) + + def _write_form_feed(self): + yield self.FF + + class ANSI2HTMLConverter(ANSIProcessor): ENTITIES =3D { '\x00' : '␀', '\x01' : '␁', '\x02' : '␂', @@ -399,6 +422,12 @@ class ANSI2HTMLConverter(ANSIProcessor): self.prefix =3D '
'
=20
=20
+def ansi2text(input):
+    c =3D ANSI2TextConverter()
+    yield from c.convert(input)
+    yield from c.finish()
+
+
 def ansi2html(input, white_bg=3DFalse):
     c =3D ANSI2HTMLConverter(white_bg=3Dwhite_bg)
     yield from c.convert(input)
diff --git a/patchew/settings.py b/patchew/settings.py
index 90a28c3..65a3b29 100644
--- a/patchew/settings.py
+++ b/patchew/settings.py
@@ -84,6 +84,9 @@ TEMPLATES =3D [
                 'django.contrib.auth.context_processors.auth',
                 'django.contrib.messages.context_processors.messages',
             ],
+            'builtins': [
+                'patchew.tags',
+            ],
         },
     },
 ]
diff --git a/patchew/tags.py b/patchew/tags.py
new file mode 100644
index 0000000..22d64b0
--- /dev/null
+++ b/patchew/tags.py
@@ -0,0 +1,19 @@
+#!/usr/bin/env python3
+#
+# Copyright 2018 Red Hat, Inc.
+#
+# Authors:
+#     Paolo Bonzini 
+#
+# This work is licensed under the MIT License.  Please see the LICENSE fil=
e or
+# http://opensource.org/licenses/MIT.
+
+from django import template
+from patchew import logviewer
+
+register =3D template.Library()
+
+@register.simple_tag
+@register.filter
+def ansi2text(value):
+    return ''.join(logviewer.ansi2text(value))
diff --git a/tests/test_ansi2html.py b/tests/test_ansi2html.py
index cb709fd..e146c3e 100644
--- a/tests/test_ansi2html.py
+++ b/tests/test_ansi2html.py
@@ -6,7 +6,7 @@
=20
 import unittest
=20
-from patchew.logviewer import ansi2html
+from patchew.logviewer import ansi2html, ansi2text, ANSI2TextConverter
=20
 class ANSI2HTMLTest(unittest.TestCase):
     def assertAnsi(self, test, expected, **kwargs):
@@ -291,5 +291,78 @@ class ANSI2HTMLTest(unittest.TestCase):
         self.assertWhiteBg('abc\x1b[7m\x1b[1Kabc', '   abc')
=20
=20
+class ANSI2TextTest(unittest.TestCase):
+    def assertAnsi(self, test, expected, **kwargs):
+        self.assertEqual(''.join(ansi2text(test, **kwargs)), expected,
+                         repr(test))
+
+    # basic formatting tests
+    def test_basic(self):
+        self.assertAnsi('\tb', '        b')
+        self.assertAnsi('\t\ta', '                a')
+        self.assertAnsi('a\tb', 'a       b')
+        self.assertAnsi('ab\tc', 'ab      c')
+        self.assertAnsi('a\nbc', 'a\nbc')
+        self.assertAnsi('a\f', 'a\n' + ANSI2TextConverter.FF)
+        self.assertAnsi('a\n\f', 'a\n\n' + ANSI2TextConverter.FF)
+        self.assertAnsi('<', '<')
+        self.assertAnsi('\x07', '\U00001F514')
+
+    # backspace and carriage return
+    def test_set_pos(self):
+        self.assertAnsi('abc\b\bBC', 'aBC')
+        self.assertAnsi('a\b<', '<')
+        self.assertAnsi('<\ba', 'a')
+        self.assertAnsi('a\b\bbc', 'bc')
+        self.assertAnsi('a\rbc', 'bc')
+        self.assertAnsi('a\nb\bc', 'a\nc')
+        self.assertAnsi('a\t\bb', 'a      b')
+        self.assertAnsi('a\tb\b\bc', 'a      cb')
+        self.assertAnsi('01234567\r\tb', '01234567b')
+
+    # Escape sequences
+    def test_esc_parsing(self):
+        self.assertAnsi('{\x1b%}', '{}')
+        self.assertAnsi('{\x1b[0m}', '{}')
+        self.assertAnsi('{\x1b[m}', '{}')
+        self.assertAnsi('{\x1b[0;1;7;0m}', '{}')
+        self.assertAnsi('{\x1b[1;7m\x1b[m}', '{}')
+        self.assertAnsi('{\x1b]test\x1b\\}', '{}')
+        self.assertAnsi('{\x1b]test\x07}', '{}')
+        self.assertAnsi('{\x1b]test\x1b[0m\x07}', '{}')
+        self.assertAnsi('{\x1b]test\x1b[7m\x07}', '{}')
+
+    # ESC [C and ESC [D
+    def test_horiz_movement(self):
+        self.assertAnsi('abc\x1b[2DB', 'aBc')
+        self.assertAnsi('abc\x1b[3CD', 'abc   D')
+        self.assertAnsi('abcd\x1b[3DB\x1b[1CD', 'aBcD')
+        self.assertAnsi('abc\x1b[0CD', 'abc D')
+        self.assertAnsi('abc\x1b[CD', 'abc D')
+
+    # ESC [K
+    def test_clear_line(self):
+        self.assertAnsi('\x1b[Kabcd', 'abcd')
+        self.assertAnsi('abcd\r\x1b[K', '')
+        self.assertAnsi('abcd\b\x1b[K', 'abc')
+        self.assertAnsi('abcd\r\x1b[KDef', 'Def')
+        self.assertAnsi('abcd\b\x1b[KDef', 'abcDef')
+        self.assertAnsi('abcd\r\x1b[0K', '')
+        self.assertAnsi('abcd\b\x1b[0K', 'abc')
+        self.assertAnsi('abcd\r\x1b[1K', 'abcd')
+        self.assertAnsi('abcd\b\x1b[1K', '   d')
+        self.assertAnsi('abcd\r\x1b[2K', '')
+        self.assertAnsi('abcd\b\x1b[2K', '   ')
+        self.assertAnsi('abcd\r\x1b[2KDef', 'Def')
+        self.assertAnsi('abcd\b\x1b[2KDef', '   Def')
+
+    # combining cursor movement and formatting
+    def test_movement_and_formatting(self):
+        self.assertAnsi('\x1b[42m\tabc', '        abc')
+        self.assertAnsi('abc\x1b[42m\x1b[1Kabc', '   abc')
+        self.assertAnsi('\x1b[7m\tabc', '        abc')
+        self.assertAnsi('abc\x1b[7m\x1b[1Kabc', '   abc')
+
+
 if __name__ =3D=3D '__main__':
     unittest.main()
diff --git a/tests/test_custom_tags.py b/tests/test_custom_tags.py
new file mode 100755
index 0000000..ec875c8
--- /dev/null
+++ b/tests/test_custom_tags.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python3
+#
+# Copyright 2018 Red Hat, Inc.
+#
+# Authors:
+#     Paolo Bonzini 
+#
+# This work is licensed under the MIT License.  Please see the LICENSE fil=
e or
+# http://opensource.org/licenses/MIT.
+
+from django.template import Context, Template
+import patchewtest
+import unittest
+
+class CustomTagsTest(unittest.TestCase):
+    def assertTemplate(self, template, expected, **kwargs):
+        context =3D Context(kwargs)
+        self.assertEqual(Template(template).render(context), expected)
+
+    def test_template_filters(self):
+        self.assertTemplate('{{s|ansi2text}}', 'dbc', s=3D'abc\rd')
+
+    def test_template_tags(self):
+        self.assertTemplate('{% ansi2text s %}', 'dbc', s=3D'abc\rd')
+
+if __name__ =3D=3D '__main__':
+    unittest.main()
--=20
2.14.3


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