Code - Update/modernise diff.py (#2471)

pull/2476/head
Nectariferous 6 months ago committed by GitHub
parent e9c4037178
commit 1c46914992
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -1,62 +1,97 @@
# used for the notifications, the front-end is using a JS library
import difflib import difflib
from typing import List, Iterator, Union
def same_slicer(lst: List[str], start: int, end: int) -> List[str]:
"""Return a slice of the list, or a single element if start == end."""
return lst[start:end] if start != end else [lst[start]]
def same_slicer(l, a, b): def customSequenceMatcher(
if a == b: before: List[str],
return [l[a]] after: List[str],
else: include_equal: bool = False,
return l[a:b] include_removed: bool = True,
include_added: bool = True,
# like .compare but a little different output include_replaced: bool = True,
def customSequenceMatcher(before, after, include_equal=False, include_removed=True, include_added=True, include_replaced=True, include_change_type_prefix=True): include_change_type_prefix: bool = True
cruncher = difflib.SequenceMatcher(isjunk=lambda x: x in " \\t", a=before, b=after) ) -> Iterator[List[str]]:
"""
# @todo Line-by-line mode instead of buncghed, including `after` that is not in `before` (maybe unset?) Compare two sequences and yield differences based on specified parameters.
Args:
before (List[str]): Original sequence
after (List[str]): Modified sequence
include_equal (bool): Include unchanged parts
include_removed (bool): Include removed parts
include_added (bool): Include added parts
include_replaced (bool): Include replaced parts
include_change_type_prefix (bool): Add prefixes to indicate change types
Yields:
List[str]: Differences between sequences
"""
cruncher = difflib.SequenceMatcher(isjunk=lambda x: x in " \t", a=before, b=after)
for tag, alo, ahi, blo, bhi in cruncher.get_opcodes(): for tag, alo, ahi, blo, bhi in cruncher.get_opcodes():
if include_equal and tag == 'equal': if include_equal and tag == 'equal':
g = before[alo:ahi] yield before[alo:ahi]
yield g
elif include_removed and tag == 'delete': elif include_removed and tag == 'delete':
row_prefix = "(removed) " if include_change_type_prefix else '' prefix = "(removed) " if include_change_type_prefix else ''
g = [ row_prefix + i for i in same_slicer(before, alo, ahi)] yield [f"{prefix}{line}" for line in same_slicer(before, alo, ahi)]
yield g
elif include_replaced and tag == 'replace': elif include_replaced and tag == 'replace':
row_prefix = "(changed) " if include_change_type_prefix else '' prefix_changed = "(changed) " if include_change_type_prefix else ''
g = [row_prefix + i for i in same_slicer(before, alo, ahi)] prefix_into = "(into) " if include_change_type_prefix else ''
row_prefix = "(into) " if include_change_type_prefix else '' yield [f"{prefix_changed}{line}" for line in same_slicer(before, alo, ahi)] + \
g += [row_prefix + i for i in same_slicer(after, blo, bhi)] [f"{prefix_into}{line}" for line in same_slicer(after, blo, bhi)]
yield g
elif include_added and tag == 'insert': elif include_added and tag == 'insert':
row_prefix = "(added) " if include_change_type_prefix else '' prefix = "(added) " if include_change_type_prefix else ''
g = [row_prefix + i for i in same_slicer(after, blo, bhi)] yield [f"{prefix}{line}" for line in same_slicer(after, blo, bhi)]
yield g
# only_differences - only return info about the differences, no context def render_diff(
# line_feed_sep could be "<br>" or "<li>" or "\n" etc previous_version_file_contents: str,
def render_diff(previous_version_file_contents, newest_version_file_contents, include_equal=False, include_removed=True, include_added=True, include_replaced=True, line_feed_sep="\n", include_change_type_prefix=True, patch_format=False): newest_version_file_contents: str,
include_equal: bool = False,
newest_version_file_contents = [line.rstrip() for line in newest_version_file_contents.splitlines()] include_removed: bool = True,
include_added: bool = True,
if previous_version_file_contents: include_replaced: bool = True,
previous_version_file_contents = [line.rstrip() for line in previous_version_file_contents.splitlines()] line_feed_sep: str = "\n",
else: include_change_type_prefix: bool = True,
previous_version_file_contents = "" patch_format: bool = False
) -> str:
"""
Render the difference between two file contents.
Args:
previous_version_file_contents (str): Original file contents
newest_version_file_contents (str): Modified file contents
include_equal (bool): Include unchanged parts
include_removed (bool): Include removed parts
include_added (bool): Include added parts
include_replaced (bool): Include replaced parts
line_feed_sep (str): Separator for lines in output
include_change_type_prefix (bool): Add prefixes to indicate change types
patch_format (bool): Use patch format for output
Returns:
str: Rendered difference
"""
newest_lines = [line.rstrip() for line in newest_version_file_contents.splitlines()]
previous_lines = [line.rstrip() for line in previous_version_file_contents.splitlines()] if previous_version_file_contents else []
if patch_format: if patch_format:
patch = difflib.unified_diff(previous_version_file_contents, newest_version_file_contents) patch = difflib.unified_diff(previous_lines, newest_lines)
return line_feed_sep.join(patch) return line_feed_sep.join(patch)
rendered_diff = customSequenceMatcher(before=previous_version_file_contents, rendered_diff = customSequenceMatcher(
after=newest_version_file_contents, before=previous_lines,
include_equal=include_equal, after=newest_lines,
include_removed=include_removed, include_equal=include_equal,
include_added=include_added, include_removed=include_removed,
include_replaced=include_replaced, include_added=include_added,
include_change_type_prefix=include_change_type_prefix) include_replaced=include_replaced,
include_change_type_prefix=include_change_type_prefix
)
def flatten(lst: List[Union[str, List[str]]]) -> str:
return line_feed_sep.join(flatten(x) if isinstance(x, list) else x for x in lst)
# Recursively join lists return flatten(rendered_diff)
f = lambda L: line_feed_sep.join([f(x) if type(x) is list else x for x in L])
p= f(rendered_diff)
return p

Loading…
Cancel
Save