Commit 16719945 authored by Chris Lamb's avatar Chris Lamb 👀
Browse files

Refactor html and text presenters so they fit the same Presenter interface.



Signed-off-by: Chris Lamb's avatarChris Lamb <lamby@debian.org>
parent e479a8e4
Loading
Loading
Loading
Loading
+5 −49
Original line number Diff line number Diff line
@@ -24,8 +24,7 @@ from ..profiling import profile

from .text import TextPresenter
from .json import JSONPresenter
from .html import output_html, output_html_directory
from .utils import make_printer
from .html import HTMLPresenter, HTMLDirectoryPresenter
from .markdown import MarkdownTextPresenter
from .restructuredtext import RestructuredTextPresenter

@@ -42,11 +41,11 @@ def output_all(difference, parsed_args, has_differences):

    FORMATS = {
        'text': {
            'fn': text,
            'klass': TextPresenter,
            'target': parsed_args.text_output,
        },
        'html': {
            'fn': html,
            'klass': HTMLPresenter,
            'target': parsed_args.html_output,
        },
        'json': {
@@ -62,7 +61,7 @@ def output_all(difference, parsed_args, has_differences):
            'target': parsed_args.restructuredtext_output,
        },
        'html_directory': {
            'fn': html_directory,
            'klass': HTMLDirectoryPresenter,
            'target': parsed_args.html_output_directory,
        },
    }
@@ -78,47 +77,4 @@ def output_all(difference, parsed_args, has_differences):
        logger.debug("Generating %r output at %r", name, data['target'])

        with profile('output', name):
            if 'fn' in data:
                data['fn'](difference, parsed_args, has_differences)
                continue

            with make_printer(data['target']) as fn:
                data['klass'](fn).start(difference)

def text(difference, parsed_args, has_differences):
    # As a special 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]

        presenter = TextPresenter(fn, color)

        try:
            presenter.start(difference)
        except UnicodeEncodeError:
            logger.critical("Console is unable to print Unicode characters. "
                "Set e.g. PYTHONIOENCODING=utf-8")
            sys.exit(2)

def html(difference, parsed_args, has_differences):
    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):
    output_html_directory(
        parsed_args.html_output_directory,
        difference,
        css_url=parsed_args.css_url,
        jquery_url=parsed_args.jquery_url,
    )
            data['klass'].run(data, difference, parsed_args, has_differences)
+2 −1
Original line number Diff line number Diff line
@@ -17,4 +17,5 @@
# You should have received a copy of the GNU General Public License
# along with diffoscope.  If not, see <https://www.gnu.org/licenses/>.

from .html import output_html, output_html_directory, JQUERY_SYSTEM_LOCATIONS
from .html import HTMLPresenter, HTMLDirectoryPresenter, \
    JQUERY_SYSTEM_LOCATIONS
+23 −1
Original line number Diff line number Diff line
@@ -46,7 +46,7 @@ from diffoscope.config import Config

from ..icon import FAVICON_BASE64
from ..utils import PrintLimitReached, DiffBlockLimitReached, \
    create_limited_print_func
    create_limited_print_func, Presenter, make_printer

from . import templates
from .linediff import linediff
@@ -78,6 +78,28 @@ spl_rows, spl_current_page = 0, 0
spl_print_func, spl_print_ctrl = None, None


class HTMLPresenter(Presenter):
    @classmethod
    def run(cls, data, difference, parsed_args, has_differences):
        with make_printer(parsed_args.html_output) as fn:
            output_html(
                difference,
                css_url=parsed_args.css_url,
                print_func=fn,
            )


class HTMLDirectoryPresenter(Presenter):
    @classmethod
    def run(cls, data, difference, parsed_args, has_differences):
        output_html_directory(
            parsed_args.html_output_directory,
            difference,
            css_url=parsed_args.css_url,
            jquery_url=parsed_args.jquery_url,
        )


def new_unified_diff():
    global buf, add_cpt, del_cpt
    global line1, line2, has_internal_linenos
+31 −1
Original line number Diff line number Diff line
@@ -18,11 +18,16 @@
# along with diffoscope.  If not, see <https://www.gnu.org/licenses/>.

import re
import sys
import logging

from diffoscope.diff import color_unified_diff
from diffoscope.config import Config

from .utils import Presenter, create_limited_print_func, PrintLimitReached
from .utils import Presenter, create_limited_print_func, PrintLimitReached, \
    make_printer

logger = logging.getLogger(__name__)


class TextPresenter(Presenter):
@@ -38,6 +43,31 @@ class TextPresenter(Presenter):

        super().__init__()

    @classmethod
    def run(cls, data, difference, parsed_args, has_differences):
        # As a special 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]

            presenter = cls(fn, color)

            try:
                presenter.start(difference)
            except UnicodeEncodeError:
                logger.critical(
                    "Console is unable to print Unicode characters. Set e.g. "
                    "PYTHONIOENCODING=utf-8"
                )
                sys.exit(2)

    def start(self, difference):
        try:
            super().start(difference)
+5 −0
Original line number Diff line number Diff line
@@ -26,6 +26,11 @@ class Presenter(object):
    def __init__(self):
        self.depth = 0

    @classmethod
    def run(cls, data, difference, parsed_args, has_differences):
        with make_printer(data['target']) as fn:
            cls(fn).start(difference)

    def start(self, difference):
        self.visit(difference)