jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1025406?usp=email )
Change subject: [IMPR] Avoid empty blocks in textlib
......................................................................
[IMPR] Avoid empty blocks in textlib
Instead iterating over all elements to find the last match (and cnt),
create a list and extract the needed values from there.
Change-Id: Ia8fd6432771f7e721523212baa8e7672b3f450d3
---
M pywikibot/textlib.py
1 file changed, 14 insertions(+), 13 deletions(-)
Approvals:
jenkins-bot: Verified
Xqt: Looks good to me, approved
diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py
index 788df3a..b54d4f5 100644
--- a/pywikibot/textlib.py
+++ b/pywikibot/textlib.py
@@ -1671,8 +1671,7 @@
if under_categories:
category = get_regexes('category', site)[0]
- for last_category in category.finditer(newtext):
- pass
+ last_category = list(category.finditer(newtext))[-1]
for reg in under_categories:
special = reg.search(newtext)
if special and not isDisabled(newtext, special.start()):
@@ -2112,29 +2111,31 @@
"""
return to_latin_digits(line)
- def _last_match_and_replace(self, txt: str, pat):
+ def _last_match_and_replace(self,
+ txt: str,
+ pat) -> tuple[str, Match[str] | None]:
"""Take the rightmost match and replace with marker.
It does so to prevent spurious earlier matches.
"""
- m = None
- cnt = 0
- for cnt, m in enumerate(pat.finditer(txt), start=1):
- pass
+ all_matches = list(pat.finditer(txt))
+ cnt = len(all_matches)
- def marker(m):
+ if not cnt:
+ return (txt, None)
+
+ m = all_matches[-1]
+
+ def marker(m: Match[str]):
"""
Replace exactly the same number of matched characters.
- Same number of chars shall be replaced, in order to be able to
- compare pos for matches reliably (absolute pos of a match
+ Same number of chars shall be replaced, in order to be able
+ to compare pos for matches reliably (absolute pos of a match
is not altered by replacement).
"""
return '@' * (m.end() - m.start())
- if not m:
- return (txt, None)
-
# month and day format might be identical (e.g. see bug T71315),
# avoid to wipe out day, after month is matched. Replace all matches
# but the last two (i.e. allow to search for dd. mm.)
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1025406?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Ia8fd6432771f7e721523212baa8e7672b3f450d3
Gerrit-Change-Number: 1025406
Gerrit-PatchSet: 6
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: Matěj Suchánek <matejsuchanek97(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged
jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1025444?usp=email )
Change subject: [IMPR] Remove empty body
......................................................................
[IMPR] Remove empty body
- remove needless return statement in DryRequest._write_cache
- drop context manager functionality for textlib._GetDataHTML
and use contextlib.closing instead in removeHTMLParts()
- update documentation
Change-Id: I38287c83a8e8d1cac45b0a0deb8064e153434b80
---
M pywikibot/textlib.py
M tests/utils.py
2 files changed, 44 insertions(+), 22 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py
index 788df3a..60968c7 100644
--- a/pywikibot/textlib.py
+++ b/pywikibot/textlib.py
@@ -10,7 +10,7 @@
import re
from collections import OrderedDict
from collections.abc import Sequence
-from contextlib import suppress
+from contextlib import closing, suppress
from html.parser import HTMLParser
from typing import NamedTuple
@@ -543,22 +543,26 @@
def removeHTMLParts(text: str, keeptags: list[str] | None = None) -> str:
- """
- Return text without portions where HTML markup is disabled.
+ """Return text without portions where HTML markup is disabled.
- Parts that can/will be removed are --
- * HTML and all wiki tags
+ Parts that can/will be removed are HTML tags and all wiki tags. The
+ exact set of parts which should NOT be removed can be passed as the
+ *keeptags* parameter, which defaults to
+ ``['tt', 'nowiki', 'small', 'sup']``.
- The exact set of parts which should NOT be removed can be passed as the
- 'keeptags' parameter, which defaults to ['tt', 'nowiki', 'small', 'sup'].
+ **Example:**
+
+ >>> removeHTMLParts('<div><b><ref><tt>Hi all!</tt></ref></b></div>')
+ '<tt>Hi all!</tt>'
+
+ .. seealso:: :class:`_GetDataHTML`
"""
- # try to merge with 'removeDisabledParts()' above into one generic function
- # thanks to:
- # https://www.hellboundhackers.org/articles/read-article.php?article_id=841
+ # TODO: try to merge with 'removeDisabledParts()' above into one generic
+ # function
parser = _GetDataHTML()
if keeptags is None:
keeptags = ['tt', 'nowiki', 'small', 'sup']
- with parser:
+ with closing(parser):
parser.keeptags = keeptags
parser.feed(text)
return parser.textdata
@@ -568,20 +572,39 @@
"""HTML parser which removes html tags except they are listed in keeptags.
- This class is also a context manager which closes itself at exit time.
+ The parser is used by :func:`removeHTMLParts` similar to this:
- .. seealso:: :pylib:`html.parser`
+ .. code:: python
+
+ from contextlib import closing
+ from pywikibot.textlib import _GetDataHTML
+ with closing(_GetDataHTML()) as parser:
+ parser.keeptags = ['html']
+ parser.feed('<html><head><title>Test</title></head>'
+ '<body><h1><!-- Parse --> me!</h1></body></html>')
+ print(parser.textdata)
+
+ The result is:
+
+ .. code:: text
+
+ <html>Test me!</html>
+
+ .. versionchanged:: 9.2
+ This class is no longer a context manager;
+ :pylib:`contextlib.closing()<contextlib#contextlib.closing>`
+ should be used instead.
+
+ .. seealso::
+ - :pylib:`html.parser`
+ - :pylib:`contextlib#contextlib.closing`
+
+ :meta public:
"""
textdata = ''
keeptags: list[str] = []
- def __enter__(self) -> None:
- pass
-
- def __exit__(self, *exc_info) -> None:
- self.close()
-
def handle_data(self, data) -> None:
"""Add data to text."""
self.textdata += data
diff --git a/tests/utils.py b/tests/utils.py
index 445075a..7ea74d1 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -322,9 +322,8 @@
"""Never invalidate cached data."""
return False
- def _write_cache(self, data):
- """Never write data."""
- return
+ def _write_cache(self, data) -> None:
+ """Never write data but just do nothing."""
def submit(self):
"""Prevented method."""
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1025444?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I38287c83a8e8d1cac45b0a0deb8064e153434b80
Gerrit-Change-Number: 1025444
Gerrit-PatchSet: 3
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged
jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1027554?usp=email )
Change subject: [tests] update coverage for version.py
......................................................................
[tests] update coverage for version.py
Change-Id: Ie1dcfb1e0731428505e4ce67e63038b380fdaa00
---
M pywikibot/version.py
M tests/version_tests.py
2 files changed, 7 insertions(+), 7 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/version.py b/pywikibot/version.py
index 0e3f35a..722a377 100644
--- a/pywikibot/version.py
+++ b/pywikibot/version.py
@@ -103,7 +103,7 @@
exceptions[vcs_func] = vcs_func.__name__, e
else:
break
- else:
+ else: # pragma: no cover
# nothing worked; version unknown (but suppress exceptions)
# the value is most likely '$Id' + '$', it means that
# pywikibot was imported without using version control at all.
@@ -114,14 +114,14 @@
exceptions = None
# Git and SVN can silently fail, as it may be a nightly.
- if exceptions:
+ if exceptions: # pragma: no cover
pywikibot.debug(f'version algorithm exceptions:\n{exceptions!r}')
if isinstance(date, str):
datestring = date
elif isinstance(date, time.struct_time):
datestring = time.strftime('%Y/%m/%d, %H:%M:%S', date)
- else:
+ else: # pragma: no cover
warn('Unable to detect package date', UserWarning)
datestring = '-2 (unknown)'
@@ -258,7 +258,7 @@
"""
file = Path(path or _get_program_dir())
if not path:
- file /= 'pywikibot'
+ file /= 'pywikibot' # pragma: no cover
file /= 'version'
with file.open() as data:
@@ -267,7 +267,7 @@
date = time.strptime(date[:19], '%Y-%m-%dT%H:%M:%S')
if not date or not tag or not rev:
- raise VersionParseError
+ raise VersionParseError # pragma: no cover
return (tag, rev, date, hsh)
@@ -299,7 +299,7 @@
headers={'user-agent': '{pwb}'}).text[4:]
try:
return json.loads(buf)['revision']
- except Exception as e:
+ except Exception as e: # pragma: no cover
raise VersionParseError(f'{e!r} while parsing {buf!r}')
diff --git a/tests/version_tests.py b/tests/version_tests.py
index 217bbd2..96a6de1 100644
--- a/tests/version_tests.py
+++ b/tests/version_tests.py
@@ -58,7 +58,7 @@
hsh = version.getversion_onlinerepo('branches/' + branch)
try:
int(hsh, 16)
- except ValueError:
+ except ValueError: # pragma: no cover
self.fail(
f'{hsh!r} is not a valid hash of {branch} branch')
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1027554?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Ie1dcfb1e0731428505e4ce67e63038b380fdaa00
Gerrit-Change-Number: 1027554
Gerrit-PatchSet: 1
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged
jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1026714?usp=email )
Change subject: [doc] fix description for version script
......................................................................
[doc] fix description for version script
Change-Id: I0b52ac21517d547638036f2338a492f3e2f0ef12
---
M pywikibot/scripts/version.py
1 file changed, 2 insertions(+), 1 deletion(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/scripts/version.py b/pywikibot/scripts/version.py
index 3d02378..0905135 100755
--- a/pywikibot/scripts/version.py
+++ b/pywikibot/scripts/version.py
@@ -3,7 +3,8 @@
The following option is supported:
--usernames print usernames for each registered family
+-nouser do not print usernames; otherwise they are printed for each
+ registered family
.. versionchanged:: 7.0
version script was moved to the framework scripts folder
--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1026714?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I0b52ac21517d547638036f2338a492f3e2f0ef12
Gerrit-Change-Number: 1026714
Gerrit-PatchSet: 2
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged