[diffoscope] 04/05: Refactor Presenter to a singleton manager
Chris Lamb
chris at chris-lamb.co.uk
Sun Apr 30 11:02:26 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 1e8c1f637c53ee041d305f4a6e256e43e202173d
Author: Chris Lamb <lamby at debian.org>
Date: Sun Apr 30 09:25:39 2017 +0100
Refactor Presenter to a singleton manager
Signed-off-by: Chris Lamb <lamby at debian.org>
---
diffoscope/main.py | 11 ++--
diffoscope/presenters/formats.py | 123 +++++++++++++++++++++++----------------
2 files changed, 78 insertions(+), 56 deletions(-)
diff --git a/diffoscope/main.py b/diffoscope/main.py
index caa824a..231b3e0 100644
--- a/diffoscope/main.py
+++ b/diffoscope/main.py
@@ -40,7 +40,7 @@ from .difference import Difference
from .comparators import ComparatorManager
from .external_tools import EXTERNAL_TOOLS
from .presenters.html import JQUERY_SYSTEM_LOCATIONS
-from .presenters.formats import configure_presenters, output_all
+from .presenters.formats import PresenterManager
from .comparators.utils.compare import compare_root_paths
logger = logging.getLogger(__name__)
@@ -271,6 +271,7 @@ def maybe_set_limit(config, parsed_args, key):
def run_diffoscope(parsed_args):
setup_logging(parsed_args.debug)
ProfileManager().setup(parsed_args)
+ PresenterManager().configure(parsed_args)
logger.debug("Starting diffoscope %s", VERSION)
if not tlsh and Config().fuzzy_threshold != parsed_args.fuzzy_threshold:
logger.warning('Fuzzy-matching is currently disabled as the "tlsh" module is unavailable.')
@@ -285,11 +286,7 @@ def run_diffoscope(parsed_args):
Config().fuzzy_threshold = parsed_args.fuzzy_threshold
Config().new_file = parsed_args.new_file
Config().excludes = parsed_args.excludes
- presenter_config = configure_presenters(parsed_args)
- # Don't waste time computing visual differences if we won't use them.
- Config().compute_visual_diffs = any(
- x['klass'].supports_visual_diffs for x in presenter_config.values(),
- )
+ Config().compute_visual_diffs = PresenterManager().compute_visual_diffs()
set_path()
set_locale()
logger.debug('Starting comparison')
@@ -304,7 +301,7 @@ def run_diffoscope(parsed_args):
if difference is None and parsed_args.output_empty:
difference = Difference(None, parsed_args.path1, parsed_args.path2)
with profile('main', 'outputs'):
- output_all(presenter_config, difference, parsed_args, has_differences)
+ PresenterManager().output(difference, parsed_args, has_differences)
return 1 if has_differences else 0
diff --git a/diffoscope/presenters/formats.py b/diffoscope/presenters/formats.py
index e30ac8e..94b723f 100644
--- a/diffoscope/presenters/formats.py
+++ b/diffoscope/presenters/formats.py
@@ -17,7 +17,6 @@
# 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 logging
from ..profiling import profile
@@ -31,51 +30,77 @@ from .restructuredtext import RestructuredTextPresenter
logger = logging.getLogger(__name__)
-def configure_presenters(parsed_args):
- FORMATS = {
- 'text': {
- 'klass': TextPresenter,
- 'target': parsed_args.text_output,
- },
- 'html': {
- 'klass': HTMLPresenter,
- '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': {
- 'klass': HTMLDirectoryPresenter,
- 'target': parsed_args.html_output_directory,
- },
- }
-
- result = {k: v for k, v in FORMATS.items() if v['target'] is not None}
-
- # If no output specified, default to printing --text output to stdout
- if not result:
- parsed_args.text_output = FORMATS['text']['target'] = '-'
- result['text'] = FORMATS['text']
-
- logger.debug(
- "Will generate the following formats: %s", ", ".join(result.keys()),
- )
-
- return result
-
-
-def output_all(config, difference, parsed_args, has_differences):
- for name, data in config.items():
- logger.debug("Generating %r output at %r", name, data['target'])
-
- with profile('output', name):
- data['klass'].run(data, difference, parsed_args, has_differences)
+class PresenterManager(object):
+ _singleton = {}
+
+ def __init__(self):
+ self.__dict__ = self._singleton
+
+ if not self._singleton:
+ self.reset()
+
+ def reset(self):
+ self.config = {}
+
+ def configure(self, parsed_args):
+ FORMATS = {
+ 'text': {
+ 'klass': TextPresenter,
+ 'target': parsed_args.text_output,
+ },
+ 'html': {
+ 'klass': HTMLPresenter,
+ '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': {
+ 'klass': HTMLDirectoryPresenter,
+ 'target': parsed_args.html_output_directory,
+ },
+ }
+
+ self.config = {
+ k: v for k, v in FORMATS.items() if v['target'] is not None
+ }
+
+ # If no output specified, default to printing --text output to stdout
+ if not self.config:
+ parsed_args.text_output = FORMATS['text']['target'] = '-'
+ self.config['text'] = FORMATS['text']
+
+ logger.debug(
+ "Will generate the following formats: %s",
+ ", ".join(self.config.keys()),
+ )
+
+ def output(self, difference, parsed_args, has_differences):
+ for name, data in self.config.items():
+ logger.debug("Generating %r output at %r", name, data['target'])
+
+ with profile('output', name):
+ data['klass'].run(
+ data,
+ difference,
+ parsed_args,
+ has_differences,
+ )
+
+ def compute_visual_diffs(self):
+ """
+ Don't waste time computing visual differences if we won't use them.
+ """
+
+ return any(
+ x['klass'].supports_visual_diffs for x in self.config.values(),
+ )
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/diffoscope.git
More information about the diffoscope
mailing list