How to use the diffoscope.difference.Difference function in diffoscope

To help you get started, we’ve selected a few diffoscope examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github anthraxx / diffoscope / tests / test_difference.py View on Github external
def test_too_long_diff_block_lines(monkeypatch):
    monkeypatch.setattr(Config(), 'enforce_constraints', False)
    monkeypatch.setattr(Config(), 'max_diff_block_lines_saved', 10)
    too_long_text_a = io.StringIO("a\n" * 21)
    too_long_text_b = io.StringIO("b\n" * 21)
    difference = Difference.from_text_readers(too_long_text_a, too_long_text_b, 'a', 'b')
    assert '[ 11 lines removed ]' in difference.unified_diff
github anthraxx / diffoscope / diffoscope / main.py View on Github external
maybe_set_limit(Config(), parsed_args, "max_diff_input_lines")
    Config().fuzzy_threshold = parsed_args.fuzzy_threshold
    Config().new_file = parsed_args.new_file
    Config().excludes = parsed_args.excludes
    set_locale()
    logger.debug('Starting comparison')
    ProgressManager().setup(parsed_args)
    with Progress(1, parsed_args.path1):
        with profile('main', 'outputs'):
            difference = compare_root_paths(
                parsed_args.path1, parsed_args.path2)
    ProgressManager().finish()
    # Generate an empty, dummy diff to write, saving the exit code first.
    has_differences = bool(difference is not None)
    if difference is None and parsed_args.output_empty:
        difference = Difference(None, parsed_args.path1, parsed_args.path2)
    with profile('main', 'outputs'):
        output_all(difference, parsed_args, has_differences)
    return 1 if has_differences else 0
github anthraxx / diffoscope / diffoscope / comparators / utils / compare.py View on Github external
def compare_binary_files(file1, file2, source=None):
    try:
        return Difference.from_command(
            Xxd, file1.path, file2.path,
            source=[file1.name, file2.name], has_internal_linenos=True)
    except RequiredToolNotFound:
        hexdump1 = hexdump_fallback(file1.path)
        hexdump2 = hexdump_fallback(file2.path)
        comment = 'xxd not available in path. Falling back to Python hexlify.\n'
        return Difference.from_text(hexdump1, hexdump2, file1.name, file2.name, source, comment)
github anthraxx / diffoscope / diffoscope / comparators / java.py View on Github external
def compare_details(self, other, source=None):
        return [Difference.from_command(Javap, self.path, other.path)]
github anthraxx / diffoscope / diffoscope / comparators / text.py View on Github external
def compare(self, other, source=None):
        my_encoding = self.encoding or 'utf-8'
        other_encoding = other.encoding or 'utf-8'
        try:
            with codecs.open(self.path, 'r', encoding=my_encoding) as my_content, \
                 codecs.open(other.path, 'r', encoding=other_encoding) as other_content:
                difference = Difference.from_text_readers(my_content, other_content, self.name, other.name, source)
                # Check if difference is only in line order.
                if difference and order_only_difference(difference.unified_diff):
                    difference.add_comment("ordering differences only")
                if my_encoding != other_encoding:
                    if difference is None:
                        difference = Difference(None, self.path, other.path, source)
                    difference.add_details([Difference.from_text(my_encoding, other_encoding, None, None, source='encoding')])
                return difference
        except (LookupError, UnicodeDecodeError):
            # unknown or misdetected encoding
            return self.compare_bytes(other, source)