jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/862279 )
Change subject: [IMPR] use pathlib instead of codecs for replace.py ......................................................................
[IMPR] use pathlib instead of codecs for replace.py
- use pathlib.Path instead of codecs to open a file - use readlines() instead of splitlines to read the file - use f-strings instead of format()
Change-Id: I3095f2bac704344ea1fa850649e99b2fa0e7cdc0 --- M scripts/replace.py 1 file changed, 18 insertions(+), 7 deletions(-)
Approvals: D3r1ck01: Looks good to me, approved Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/scripts/replace.py b/scripts/replace.py index d44ff14..9a3c511 100755 --- a/scripts/replace.py +++ b/scripts/replace.py @@ -146,10 +146,10 @@ # # Distributed under the terms of the MIT license. # -import codecs import re from collections.abc import Sequence from contextlib import suppress +from pathlib import Path from typing import Any, Optional
import pywikibot @@ -807,7 +807,7 @@ return local_args, exceptions
-def handle_pairsfile(filename: str) -> List[str]: +def handle_pairsfile(filename: str) -> Optional[List[str]]: """Handle -pairsfile argument.
.. versionadded:: 7.0 @@ -817,9 +817,8 @@ 'Please enter the filename to read replacements from:')
try: - with codecs.open(filename, 'r', 'utf-8') as f: - # strip newlines, but not other characters - replacements = f.read().splitlines() + with Path(filename).open(encoding='utf-8') as f: + replacements = f.readlines() if not replacements: raise OSError(f'{filename} is empty.') except OSError as e: @@ -828,8 +827,7 @@
if len(replacements) % 2: pywikibot.error( - '{} contains an incomplete pattern replacement pair.'.format( - filename)) + f'{filename} contains an incomplete pattern replacement pair.') return None
# Strip BOM from first line
pywikibot-commits@lists.wikimedia.org