From nobody Mon Dec 15 01:37:29 2025 Delivered-To: importer@patchew.org Received-SPF: none (zoho.com: 206.53.239.180 is neither permitted nor denied by domain of freelists.org) client-ip=206.53.239.180; envelope-from=patchew-devel-bounce@freelists.org; helo=turing.freelists.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=none (zoho.com: 206.53.239.180 is neither permitted nor denied by domain of freelists.org) smtp.mailfrom=patchew-devel-bounce@freelists.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from turing.freelists.org (turing.freelists.org [206.53.239.180]) by mx.zohomail.com with SMTPS id 1520241400476626.1045934352479; Mon, 5 Mar 2018 01:16:40 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id E3D4C27251; Mon, 5 Mar 2018 04:16:39 -0500 (EST) Received: from turing.freelists.org ([127.0.0.1]) by localhost (turing.freelists.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id D5F6zmvWkI7R; Mon, 5 Mar 2018 04:16:39 -0500 (EST) Received: from turing.freelists.org (localhost [127.0.0.1]) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id 98F5326402; Mon, 5 Mar 2018 04:16:39 -0500 (EST) Received: with ECARTIS (v1.0.0; list patchew-devel); Mon, 05 Mar 2018 04:16:39 -0500 (EST) Received: from localhost (localhost [127.0.0.1]) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id 645E427251 for ; Mon, 5 Mar 2018 04:16:39 -0500 (EST) Received: from turing.freelists.org ([127.0.0.1]) by localhost (turing.freelists.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ez5ODg8m322M for ; Mon, 5 Mar 2018 04:16:39 -0500 (EST) Received: from mail-wr0-f175.google.com (mail-wr0-f175.google.com [209.85.128.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTPS id F405126402 for ; Mon, 5 Mar 2018 04:16:38 -0500 (EST) Received: by mail-wr0-f175.google.com with SMTP id f14so16399264wre.8 for ; Mon, 05 Mar 2018 01:16:38 -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 b68sm7018546wmi.30.2018.03.05.01.16.36 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 05 Mar 2018 01:16:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=freelists.org; s=turing; t=1520241399; bh=TjbRQ8qKSyUOqFRnomiXCpQuAcF5JlYJPV++1G5xOZM=; h=From:To:Subject:Date:In-Reply-To:References:Reply-To:List-help: List-unsubscribe:List-Id:List-subscribe:List-owner:List-post: List-archive; b=fxvDPsCyb7pJ0DWKgqZPh/xSOUxY9ED+uFv0LFAFvN2GxuKZ+XlnkYl/DiFp9kwo4 Pa4m2NUvu/wRrX/dS3hjODSncKy5jg0ulBg8r/ClgGeGv4cN3ybOfB60SxNbyD07vR qiX0IGCQsjvnYIoKZvjiEa4Un1G8YVQe+el7wYCc= X-Virus-Scanned: Debian amavisd-new at turing.freelists.org DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=freelists.org; s=turing; t=1520241399; bh=TjbRQ8qKSyUOqFRnomiXCpQuAcF5JlYJPV++1G5xOZM=; h=From:To:Subject:Date:In-Reply-To:References:Reply-To:List-help: List-unsubscribe:List-Id:List-subscribe:List-owner:List-post: List-archive; b=fxvDPsCyb7pJ0DWKgqZPh/xSOUxY9ED+uFv0LFAFvN2GxuKZ+XlnkYl/DiFp9kwo4 Pa4m2NUvu/wRrX/dS3hjODSncKy5jg0ulBg8r/ClgGeGv4cN3ybOfB60SxNbyD07vR qiX0IGCQsjvnYIoKZvjiEa4Un1G8YVQe+el7wYCc= X-Original-To: patchew-devel@freelists.org X-Virus-Scanned: Debian amavisd-new at turing.freelists.org 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=IfqI7qxse1PNe+y8kZzVVrioZfRIsfVTsmty1IdkvvE=; b=czPpSd5hvAx0MFYe7uWlU8m1/DBBZDI04Bf45aNPEd2qs6kCEA3VPMXLO4kKXKZ8qf JSjIXKTejIZBl/SFzGRRXnm2+3aUu91JobXGuZwxPq74dcIjLZFadLE2z+VPkvo+l8gK K7ptDCfwqgNBkVDeB4rMv70/mF/Aw0rs+P/uroWals5xM+mWK9r2IqRtkxSp5n3U4R/M N0Vahzl76LzgsCxkWr2bp+Fl98VAolwijimK0l6HcvZaavSaiwqxqW1EAbD2ZWF72l8u atIUSmS8DILWn47Ca8XNjHPpfPVwtz/NB9IyDukoecQJUR2YPaQFI/VRvmGAO6/fbZb/ rLpw== X-Gm-Message-State: APf1xPAaV2KqG8XGSUJbucZCLf8gDqT+R7duDOdlOiLqADL1ZB3d/El3 28zVQFKMOB4Da0SjXJOKi6yhqoiW X-Google-Smtp-Source: AG47ELsoS8MqcgN+uPvxuNdJz19CQHdZMeXme9QEqHG8ceLhORSdqH0WMJ6FJJF3n7YWRm5VgR2LiQ== X-Received: by 10.223.160.243 with SMTP id n48mr12437603wrn.92.1520241397302; Mon, 05 Mar 2018 01:16:37 -0800 (PST) From: Paolo Bonzini To: patchew-devel@freelists.org Subject: [patchew-devel] [PATCH 3/6] ansi2html: create ANSI to text converter Date: Mon, 5 Mar 2018 10:16:31 +0100 Message-Id: <20180305091634.7391-4-pbonzini@redhat.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180305091634.7391-1-pbonzini@redhat.com> References: <20180305091634.7391-1-pbonzini@redhat.com> X-archive-position: 75 X-ecartis-version: Ecartis v1.0.0 Sender: patchew-devel-bounce@freelists.org Errors-to: patchew-devel-bounce@freelists.org X-original-sender: pbonzini@redhat.com Precedence: normal Reply-To: patchew-devel@freelists.org List-help: List-unsubscribe: List-software: Ecartis version 1.0.0 List-Id: patchew-devel X-List-ID: patchew-devel List-subscribe: List-owner: List-post: List-archive: X-list: patchew-devel X-ZohoMail-DKIM: pass (identity @freelists.org) X-ZohoMail: RDKM_0 RSF_4 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This converter compresses sequences involving carriage returns or backspace= s, 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..b84d487 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 '\u2015' * 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