[diffoscope] 03/04: diffoscope.main: Split output-related methods into presenters.utils.

Chris Lamb chris at chris-lamb.co.uk
Mon Jan 2 11:14:28 CET 2017


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

lamby pushed a commit to branch master
in repository diffoscope.

commit 52d2b59843dd3c680205310f19cc26ef640fdf26
Author: Chris Lamb <lamby at debian.org>
Date:   Mon Jan 2 10:11:37 2017 +0000

    diffoscope.main: Split output-related methods into presenters.utils.
    
    Signed-off-by: Chris Lamb <lamby at debian.org>
---
 diffoscope/main.py             |  81 +------------------------------
 diffoscope/presenters/utils.py | 106 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 108 insertions(+), 79 deletions(-)

diff --git a/diffoscope/main.py b/diffoscope/main.py
index 5a8a49a..6073615 100644
--- a/diffoscope/main.py
+++ b/diffoscope/main.py
@@ -21,12 +21,10 @@
 
 import os
 import sys
-import codecs
 import signal
 import logging
 import argparse
 import traceback
-import contextlib
 
 from . import VERSION
 from .exc import RequiredToolNotFound
@@ -36,11 +34,9 @@ from .locale import set_locale
 from .logging import logger
 from .progress import ProgressManager, Progress
 from .tempfiles import clean_all_temp_files
-from .profiling import ProfileManager, profile
 from .difference import Difference
-from .presenters.html import output_html, output_html_directory, \
-    JQUERY_SYSTEM_LOCATIONS
-from .presenters.text import output_text
+from .presenters.html import JQUERY_SYSTEM_LOCATIONS
+from .presenters.utils import output_all
 from .comparators.utils.compare import compare_root_paths
 
 try:
@@ -172,20 +168,6 @@ def create_parser():
     return parser
 
 
- at contextlib.contextmanager
-def make_printer(path):
-    if path == '-':
-        output = sys.stdout
-    else:
-        output = codecs.open(path, 'w', encoding='utf-8')
-    def print_func(*args, **kwargs):
-        kwargs['file'] = output
-        print(*args, **kwargs)
-    print_func.output = output
-    yield print_func
-    if path != '-':
-        output.close()
-
 class RangeCompleter(object):
     def __init__(self, start, end, step):
         self.choices = range(start, end + 1, step)
@@ -258,65 +240,6 @@ def run_diffoscope(parsed_args):
     output_all(difference, parsed_args, has_differences)
     return 1 if has_differences else 0
 
-def output_all(difference, parsed_args, has_differences):
-    # Generate outputs
-    for name, fn, target in (
-        ('text', text, parsed_args.text_output),
-        ('html', html, parsed_args.html_output),
-        ('html_directory', html_directory, parsed_args.html_output_directory),
-        ('profile', profiling, parsed_args.html_output_directory),
-    ):
-        if target is None:
-            continue
-
-        logger.debug("Generating %r output at %r", name, target)
-
-        with profile('output', name):
-            fn(difference, parsed_args, has_differences)
-
-def text(difference, parsed_args, has_differences):
-    if difference is None:
-        return
-
-    # As a sppecial case, write an empty file instead of an empty diff.
-    if not has_differences:
-        open(parsed_args.text_output, 'w').close()
-        return
-
-    color = {
-        'auto': fn.output.isatty(),
-        'never': False,
-        'always': True,
-    }[parsed_args.text_color]
-
-    with make_printer(parsed_args.text_output or '-') as fn:
-        output_text(difference, print_func=fn, color=color)
-
-def html(difference, parsed_args, has_differences):
-    if difference is None:
-        return
-
-    with make_printer(parsed_args.html_output) as fn:
-        output_html(
-            difference,
-            css_url=parsed_args.css_url,
-            print_func=fn,
-        )
-
-def html_directory(difference, parsed_args, has_differences):
-    if difference is None:
-        return
-
-    output_html_directory(
-        parsed_args.html_output_directory,
-        difference,
-        css_url=parsed_args.css_url,
-        jquery_url=parsed_args.jquery_url,
-    )
-
-def profiling(difference, parsed_args, has_differences):
-    with make_printer(parsed_args.profile_output or '-') as fn:
-        ProfileManager().output(fn)
 
 def sigterm_handler(signo, stack_frame):
     sys.exit(2)
diff --git a/diffoscope/presenters/utils.py b/diffoscope/presenters/utils.py
new file mode 100644
index 0000000..71a5746
--- /dev/null
+++ b/diffoscope/presenters/utils.py
@@ -0,0 +1,106 @@
+# -*- coding: utf-8 -*-
+#
+# diffoscope: in-depth comparison of files, archives, and directories
+#
+# Copyright © 2016 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 sys
+import codecs
+import contextlib
+
+from ..logging import logger
+from ..profiling import ProfileManager, profile
+
+from .text import output_text
+from .html import output_html, output_html_directory
+
+
+def output_all(difference, parsed_args, has_differences):
+    """
+    Generate all known output formats.
+    """
+
+    for name, fn, target in (
+        ('text', text, parsed_args.text_output),
+        ('html', html, parsed_args.html_output),
+        ('html_directory', html_directory, parsed_args.html_output_directory),
+        ('profile', profiling, parsed_args.html_output_directory),
+    ):
+        if target is None:
+            continue
+
+        logger.debug("Generating %r output at %r", name, target)
+
+        with profile('output', name):
+            fn(difference, parsed_args, has_differences)
+
+def text(difference, parsed_args, has_differences):
+    if difference is None:
+        return
+
+    # As a sppecial case, write an empty file instead of an empty diff.
+    if not has_differences:
+        open(parsed_args.text_output, 'w').close()
+        return
+
+    with make_printer(parsed_args.text_output or '-') as fn:
+        color = {
+            'auto': fn.output.isatty(),
+            'never': False,
+            'always': True,
+        }[parsed_args.text_color]
+
+        output_text(difference, print_func=fn, color=color)
+
+def html(difference, parsed_args, has_differences):
+    if difference is None:
+        return
+
+    with make_printer(parsed_args.html_output) as fn:
+        output_html(
+            difference,
+            css_url=parsed_args.css_url,
+            print_func=fn,
+        )
+
+def html_directory(difference, parsed_args, has_differences):
+    if difference is None:
+        return
+
+    output_html_directory(
+        parsed_args.html_output_directory,
+        difference,
+        css_url=parsed_args.css_url,
+        jquery_url=parsed_args.jquery_url,
+    )
+
+def profiling(difference, parsed_args, has_differences):
+    with make_printer(parsed_args.profile_output or '-') as fn:
+        ProfileManager().output(fn)
+
+ at contextlib.contextmanager
+def make_printer(path):
+    if path == '-':
+        output = sys.stdout
+    else:
+        output = codecs.open(path, 'w', encoding='utf-8')
+    def print_func(*args, **kwargs):
+        kwargs['file'] = output
+        print(*args, **kwargs)
+    print_func.output = output
+    yield print_func
+    if path != '-':
+        output.close()

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


More information about the diffoscope mailing list