[diffoscope] 01/01: diffoscope.presenters: Split non-utils out of utils presenter.
Chris Lamb
chris at chris-lamb.co.uk
Tue Feb 7 03:32:42 UTC 2017
This is an automated email from the git hooks/post-receive script.
lamby pushed a commit to branch master
in repository diffoscope.
commit dfd4866f0a06bbc754894d48dd18a392e5ae1e64
Author: Chris Lamb <lamby at debian.org>
Date: Tue Feb 7 16:27:17 2017 +1300
diffoscope.presenters: Split non-utils out of utils presenter.
Signed-off-by: Chris Lamb <lamby at debian.org>
---
diffoscope/main.py | 2 +-
diffoscope/presenters/{utils.py => formats.py} | 20 +----
diffoscope/presenters/utils.py | 102 -------------------------
3 files changed, 2 insertions(+), 122 deletions(-)
diff --git a/diffoscope/main.py b/diffoscope/main.py
index eaccd6b..8f03808 100644
--- a/diffoscope/main.py
+++ b/diffoscope/main.py
@@ -37,7 +37,7 @@ from .tempfiles import clean_all_temp_files
from .difference import Difference
from .external_tools import EXTERNAL_TOOLS
from .presenters.html import JQUERY_SYSTEM_LOCATIONS
-from .presenters.utils import output_all
+from .presenters.formats import output_all
from .comparators.utils.compare import compare_root_paths
logger = logging.getLogger(__name__)
diff --git a/diffoscope/presenters/utils.py b/diffoscope/presenters/formats.py
similarity index 91%
copy from diffoscope/presenters/utils.py
copy to diffoscope/presenters/formats.py
index 4da172a..f5fa396 100644
--- a/diffoscope/presenters/utils.py
+++ b/diffoscope/presenters/formats.py
@@ -18,15 +18,14 @@
# along with diffoscope. If not, see <https://www.gnu.org/licenses/>.
import sys
-import codecs
import logging
-import contextlib
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 .markdown import MarkdownTextPresenter
from .restructuredtext import RestructuredTextPresenter
@@ -123,20 +122,3 @@ def html_directory(difference, parsed_args, has_differences):
css_url=parsed_args.css_url,
jquery_url=parsed_args.jquery_url,
)
-
- at contextlib.contextmanager
-def make_printer(path):
- output = sys.stdout
-
- if path != '-':
- output = codecs.open(path, 'w', encoding='utf-8')
-
- def fn(*args, **kwargs):
- kwargs['file'] = output
- print(*args, **kwargs)
- fn.output = output
-
- yield fn
-
- if path != '-':
- output.close()
diff --git a/diffoscope/presenters/utils.py b/diffoscope/presenters/utils.py
index 4da172a..8e084c8 100644
--- a/diffoscope/presenters/utils.py
+++ b/diffoscope/presenters/utils.py
@@ -19,110 +19,8 @@
import sys
import codecs
-import logging
import contextlib
-from ..profiling import profile
-
-from .text import TextPresenter
-from .json import JSONPresenter
-from .html import output_html, output_html_directory
-from .markdown import MarkdownTextPresenter
-from .restructuredtext import RestructuredTextPresenter
-
-logger = logging.getLogger(__name__)
-
-
-def output_all(difference, parsed_args, has_differences):
- """
- Generate all known output formats.
- """
-
- if difference is None:
- return
-
- FORMATS = {
- 'text': {
- 'fn': text,
- 'target': parsed_args.text_output,
- },
- 'html': {
- 'fn': html,
- 'target': parsed_args.html_output,
- },
- 'json': {
- 'klass': JSONPresenter,
- 'target': parsed_args.json_output,
- },
- 'markdown': {
- 'klass': MarkdownTextPresenter,
- 'target': parsed_args.markdown_output,
- },
- 'restructuredtext': {
- 'klass': RestructuredTextPresenter,
- 'target': parsed_args.restructuredtext_output,
- },
- 'html_directory': {
- 'fn': html_directory,
- 'target': parsed_args.html_output_directory,
- },
- }
-
- # If no output specified, default to printing --text output to stdout
- if not any(x['target'] for x in FORMATS.values()):
- parsed_args.text_output = FORMATS['text']['target'] = '-'
-
- for name, data in FORMATS.items():
- if data['target'] is None:
- continue
-
- 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).visit(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.visit(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,
- )
@contextlib.contextmanager
def make_printer(path):
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/diffoscope.git
More information about the Reproducible-commits
mailing list