jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1101838?usp=email )
Change subject: IMPR: simplify diff.Hunk.create_diff ......................................................................
IMPR: simplify diff.Hunk.create_diff
- remove check_line and replace it with a single f-string - create a prefix dict for tags and prefixes - combine processing for equal and delete tags
Change-Id: Ia1e0e1aff06ee440f37ffed92605d62d7e0c60ba --- M pywikibot/diff.py 1 file changed, 8 insertions(+), 13 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/diff.py b/pywikibot/diff.py index 005da21..983707e 100644 --- a/pywikibot/diff.py +++ b/pywikibot/diff.py @@ -93,25 +93,20 @@ Check each line ends with line feed to prevent behaviour like :issue:`46395` """ - def check_line(line: str) -> str: - r"""Make sure each line ends with '\n'.""" - return line if line.endswith('\n') else line + '\n' - + lf = '\n' # required for Python < 3.12 + prefix = {'insert': '+ ', 'delete': '- ', 'replace': '', 'equal': ' '} for tag, i1, i2, j1, j2 in self.group: # equal/delete/insert add additional space after the sign as it's # what difflib.ndiff does do too. - if tag == 'equal': - for line in self.a[i1:i2]: - yield ' ' + check_line(line) - elif tag == 'delete': - for line in self.a[i1:i2]: - yield '- ' + check_line(line) - elif tag == 'insert': + if tag == 'insert': for line in self.b[j1:j2]: - yield '+ ' + check_line(line) + yield f'{prefix[tag]}{line.strip(lf)}{lf}' elif tag == 'replace': for line in difflib.ndiff(self.a[i1:i2], self.b[j1:j2]): - yield check_line(line) + yield f'{prefix[tag]}{line.strip(lf)}{lf}' + else: # equal, delete + for line in self.a[i1:i2]: + yield f'{prefix[tag]}{line.strip(lf)}{lf}'
def format_diff(self) -> Iterable[str]: """Color diff lines."""
pywikibot-commits@lists.wikimedia.org