Commit 1e8c1f63 authored by Chris Lamb's avatar Chris Lamb 👀
Browse files

Refactor Presenter to a singleton manager



Signed-off-by: Chris Lamb's avatarChris Lamb <lamby@debian.org>
parent 7b683097
Loading
Loading
Loading
Loading
+4 −7
Original line number Diff line number Diff line
@@ -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


+74 −49
Original line number Diff line number Diff line
@@ -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,7 +30,19 @@ from .restructuredtext import RestructuredTextPresenter
logger = logging.getLogger(__name__)


def configure_presenters(parsed_args):
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,
@@ -59,23 +70,37 @@ def configure_presenters(parsed_args):
            },
        }

    result = {k: v for k, v in FORMATS.items() if v['target'] is not None}
        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 result:
        if not self.config:
            parsed_args.text_output = FORMATS['text']['target'] = '-'
        result['text'] = FORMATS['text']
            self.config['text'] = FORMATS['text']

        logger.debug(
        "Will generate the following formats: %s", ", ".join(result.keys()),
            "Will generate the following formats: %s",
            ", ".join(self.config.keys()),
        )

    return result


def output_all(config, difference, parsed_args, has_differences):
    for name, data in config.items():
    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)
                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(),
        )