[diffoscope] 03/03: diffoscope.presenters: Add RestructuredText output format.

Chris Lamb chris at chris-lamb.co.uk
Sat Jan 21 01:02:47 CET 2017


This is an automated email from the git hooks/post-receive script.

lamby pushed a commit to branch lamby/output-formats
in repository diffoscope.

commit 6465ea8d31eb791b41d22aaf8eaf7ca1ec02c7e0
Author: Chris Lamb <lamby at debian.org>
Date:   Fri Jan 20 17:01:52 2017 +1100

    diffoscope.presenters: Add RestructuredText output format.
    
    Signed-off-by: Chris Lamb <lamby at debian.org>
---
 diffoscope/main.py                        |  3 ++
 diffoscope/presenters/restructuredtext.py | 62 +++++++++++++++++++++++++++++++
 diffoscope/presenters/utils.py            |  9 +++++
 tests/test_main.py                        |  9 +++++
 4 files changed, 83 insertions(+)

diff --git a/diffoscope/main.py b/diffoscope/main.py
index e26b83a..5f233d4 100644
--- a/diffoscope/main.py
+++ b/diffoscope/main.py
@@ -95,6 +95,9 @@ def create_parser():
                         'installation. Known locations: %s' % ', '.join(JQUERY_SYSTEM_LOCATIONS))
     group1.add_argument('--markdown', metavar='OUTPUT_FILE', dest='markdown_output',
                         help='Write Markdown text output to given file (use - for stdout)')
+    group1.add_argument('--restructured-text', metavar='OUTPUT_FILE',
+                        dest='restructuredtext_output',
+                        help='Write RsT text output to given file (use - for stdout)')
     group1.add_argument('--profile', metavar='OUTPUT_FILE', dest='profile_output',
                         help='Write profiling info to given file (use - for stdout)')
 
diff --git a/diffoscope/presenters/restructuredtext.py b/diffoscope/presenters/restructuredtext.py
new file mode 100644
index 0000000..84bd20d
--- /dev/null
+++ b/diffoscope/presenters/restructuredtext.py
@@ -0,0 +1,62 @@
+# -*- coding: utf-8 -*-
+#
+# diffoscope: in-depth comparison of files, archives, and directories
+#
+# Copyright © 2017 Chris Lamb <lamby at debian.org>
+#
+# diffoscope is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# diffoscope is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with diffoscope.  If not, see <https://www.gnu.org/licenses/>.
+
+import textwrap
+
+from .base import Presenter
+
+
+class RestructuredTextPresenter(Presenter):
+    TITLE_CHARS = '=-`:.\'"~^_*+#'
+
+    def __init__(self, print_func):
+        self.print_func = print_func
+        super().__init__()
+
+    def visit_difference(self, difference):
+        if difference.source1 == difference.source2:
+            self.title(difference.source1)
+        else:
+            self.title("Comparing {} & {}".format(
+                difference.source1,
+                difference.source2,
+            ))
+
+        for x in difference.comments:
+            self.print_func()
+            self.print_func(x)
+
+        if difference.unified_diff:
+            self.print_func('::')
+            self.print_func()
+            self.print_func(textwrap.indent(difference.unified_diff, '    '))
+
+    def title(self, val):
+        char = self.TITLE_CHARS[self.depth % len(self.TITLE_CHARS)]
+
+        if self.depth < len(self.TITLE_CHARS):
+            self.print_func(len(val) * char)
+
+        self.print_func(val)
+        self.print_func(len(val) * char)
+        self.print_func()
+
+def output_restructuredtext(difference, print_func, color=False):
+    presenter = RestructuredTextPresenter(print_func)
+    presenter.visit(difference)
diff --git a/diffoscope/presenters/utils.py b/diffoscope/presenters/utils.py
index df77806..3b3d704 100644
--- a/diffoscope/presenters/utils.py
+++ b/diffoscope/presenters/utils.py
@@ -27,6 +27,7 @@ from ..profiling import profile
 from .text import output_text
 from .html import output_html, output_html_directory
 from .markdown import output_markdown
+from .restructuredtext import output_restructuredtext
 
 logger = logging.getLogger(__name__)
 
@@ -52,6 +53,10 @@ def output_all(difference, parsed_args, has_differences):
             'fn': markdown,
             'target': parsed_args.markdown_output,
         },
+        'restructuredtext': {
+            'fn': restructuredtext,
+            'target': parsed_args.restructuredtext_output,
+        },
         'html_directory': {
             'fn': html_directory,
             'target': parsed_args.html_output_directory,
@@ -98,6 +103,10 @@ def markdown(difference, parsed_args, has_differences):
     with make_printer(parsed_args.markdown_output) as fn:
         output_markdown(difference, print_func=fn)
 
+def restructuredtext(difference, parsed_args, has_differences):
+    with make_printer(parsed_args.restructuredtext_output) as fn:
+        output_restructuredtext(difference, print_func=fn)
+
 def html_directory(difference, parsed_args, has_differences):
     output_html_directory(
         parsed_args.html_output_directory,
diff --git a/tests/test_main.py b/tests/test_main.py
index 160bda4..d0d2575 100644
--- a/tests/test_main.py
+++ b/tests/test_main.py
@@ -125,6 +125,15 @@ def test_markdown(capsys):
     assert err == ''
     assert out.startswith('# Comparing')
 
+    args = ['--restructured-text', '-', *TEST_TARS]
+    with pytest.raises(SystemExit) as excinfo:
+        main(args)
+    assert excinfo.value.code == 1
+    out, err = capsys.readouterr()
+    assert err == ''
+    assert out.startswith('=====')
+    assert "Comparing" in out
+
 def test_no_report_option(capsys):
     args = [*TEST_TARS]
     with pytest.raises(SystemExit) as excinfo:

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/diffoscope.git


More information about the diffoscope mailing list