[diffoscope] 01/05: Refactor html and text presenters so they fit the same Presenter interface.
Chris Lamb
chris at chris-lamb.co.uk
Sun Apr 30 11:02:21 CEST 2017
This is an automated email from the git hooks/post-receive script.
lamby pushed a commit to branch experimental
in repository diffoscope.
commit 16719945db80cf9222a1a51eb1b40d496f632d56
Author: Chris Lamb <lamby at debian.org>
Date: Sat Apr 29 23:01:24 2017 +0100
Refactor html and text presenters so they fit the same Presenter interface.
Signed-off-by: Chris Lamb <lamby at debian.org>
---
diffoscope/presenters/formats.py | 54 ++++------------------------------
diffoscope/presenters/html/__init__.py | 3 +-
diffoscope/presenters/html/html.py | 24 ++++++++++++++-
diffoscope/presenters/text.py | 32 +++++++++++++++++++-
diffoscope/presenters/utils.py | 5 ++++
5 files changed, 66 insertions(+), 52 deletions(-)
diff --git a/diffoscope/presenters/formats.py b/diffoscope/presenters/formats.py
index 92f81fd..963d8d0 100644
--- a/diffoscope/presenters/formats.py
+++ b/diffoscope/presenters/formats.py
@@ -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)
diff --git a/diffoscope/presenters/html/__init__.py b/diffoscope/presenters/html/__init__.py
index 1951994..b927da6 100644
--- a/diffoscope/presenters/html/__init__.py
+++ b/diffoscope/presenters/html/__init__.py
@@ -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
diff --git a/diffoscope/presenters/html/html.py b/diffoscope/presenters/html/html.py
index f066081..160d3fb 100644
--- a/diffoscope/presenters/html/html.py
+++ b/diffoscope/presenters/html/html.py
@@ -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
diff --git a/diffoscope/presenters/text.py b/diffoscope/presenters/text.py
index 0e2558b..842d0ff 100644
--- a/diffoscope/presenters/text.py
+++ b/diffoscope/presenters/text.py
@@ -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)
diff --git a/diffoscope/presenters/utils.py b/diffoscope/presenters/utils.py
index 53b7b07..c174f5f 100644
--- a/diffoscope/presenters/utils.py
+++ b/diffoscope/presenters/utils.py
@@ -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)
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/diffoscope.git
More information about the diffoscope
mailing list