jenkins-bot has submitted this change. (
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1285524?usp=email )
Change subject: allpages: enable alto parameter for API:allpages
......................................................................
allpages: enable alto parameter for API:allpages
- add until parameter to Site.allpages to enable the
API:Allpages 'gapto' parameter
- add -until to the pagegenerators option
- implement allpages in pagegenerators with -until option. The
generator is created when GeneratorFactory.getCombinedGenerator
is called so that-start and -until are both taken into account
- add some tests
- update documentation
- ignore SIG305 test because the test fails for annotation within
parameter description
Bug: T425882
Change-Id: If6c84dd6b9d19fc3298c29ad67868597a6a82d50
---
M pyproject.toml
M pywikibot/pagegenerators/__init__.py
M pywikibot/pagegenerators/_factory.py
M pywikibot/site/_generators.py
M tests/pagegenerators_tests.py
M tests/site_generators_tests.py
6 files changed, 79 insertions(+), 9 deletions(-)
Approvals:
jenkins-bot: Verified
Xqt: Looks good to me, approved
diff --git a/pyproject.toml b/pyproject.toml
index 288f7cc..7a75d97 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -165,6 +165,7 @@
"SIG202",
"SIG203",
"SIG301",
+ "SIG305", # fails with annotation within param docstrings
"SIG306",
"SIG501",
"SIG503",
diff --git a/pywikibot/pagegenerators/__init__.py
b/pywikibot/pagegenerators/__init__.py
index 1983fb6..c46ea06 100644
--- a/pywikibot/pagegenerators/__init__.py
+++ b/pywikibot/pagegenerators/__init__.py
@@ -15,6 +15,9 @@
These parameters are supported to specify which pages titles to be used:
¶ms;
+
+.. version-changed:: 11.3
+ The :kbd:`-until` option was added.
"""
from __future__ import annotations
@@ -313,7 +316,12 @@
"-start:Template:!" will make the bot work on all pages
in the template namespace.
- default value is start:!
+ Default value is start:!
+
+-until Specifies the page title at which the robot should
+ stop alphabetically through all pages on the home wiki.
+
+ Can only be used together with -start.
-prefixindex Work on pages commencing with a common prefix.
diff --git a/pywikibot/pagegenerators/_factory.py
b/pywikibot/pagegenerators/_factory.py
index a592282..1b2c076 100644
--- a/pywikibot/pagegenerators/_factory.py
+++ b/pywikibot/pagegenerators/_factory.py
@@ -119,7 +119,7 @@
self._sparql: str | None = None
self.nopreload = False
self._validate_options(enabled_options, disabled_options)
-
+ self._allpages_args = None
self.is_preloading: bool | None = None
"""Return whether Page objects are preloaded. You may use this
instance variable after :meth:`getCombinedGenerator` is called
@@ -227,6 +227,10 @@
if gen:
self.gens.insert(0, gen)
+ # Handle allpages where args are given by -start and -until
+ if self._allpages_args is not None and 'start' in self._allpages_args:
+ self.gens.append(self.site.allpages(**self._allpages_args))
+
for i, gen_item in enumerate(self.gens):
if self.namespaces:
if (isinstance(gen_item, api.QueryGenerator)
@@ -766,14 +770,27 @@
source=self.site))
return page.getReferences(only_template_inclusion=True)
- def _handle_start(self, value: str) -> HANDLER_GEN_TYPE:
+ def _handle_start(self, value: str) -> Literal[True]:
"""Handle `-start` argument."""
if not value:
value = '!'
firstpagelink = pywikibot.Link(value, self.site)
- return self.site.allpages(
- start=firstpagelink.title, namespace=firstpagelink.namespace,
- filterredir=False)
+ self._allpages_args = self._allpages_args or {}
+ self._allpages_args.update(
+ start=firstpagelink.title,
+ namespace=firstpagelink.namespace,
+ filterredir=False,
+ )
+ return True
+
+ def _handle_until(self, value: str) -> Literal[True]:
+ """Handle `-until` argument."""
+ if not value:
+ value = '!'
+ lastpagelink = pywikibot.Link(value, self.site)
+ self._allpages_args = self._allpages_args or {}
+ self._allpages_args.update(until=lastpagelink.title)
+ return True
def _handle_prefixindex(self, value: str) -> HANDLER_GEN_TYPE:
"""Handle `-prefixindex` argument."""
diff --git a/pywikibot/site/_generators.py b/pywikibot/site/_generators.py
index d25366a..6b83a91 100644
--- a/pywikibot/site/_generators.py
+++ b/pywikibot/site/_generators.py
@@ -950,12 +950,20 @@
reverse: bool = False,
total: int | None = None,
content: bool = False,
+ until: str = '',
) -> Iterable[pywikibot.Page]:
"""Iterate pages in a single namespace.
.. seealso:: :api:`Allpages`
- :param start: Start at this title (page need not exist).
+ .. version-changed:: 10.4
+ All parameters except of *start* are keyword-only. Enable
+ *maxsize* filtering even if misermode is enabled.
+ .. version-changed:: 11.3
+ The *until* parameter was added.
+
+ :param start: Title to start enumerating from (the page does not
+ need to exist). Can be used with *until* to define a range.
:param prefix: Only yield pages starting with this string.
:param namespace: Iterate pages from this (single) namespace.
:param filterredir: If True, only yield redirects; if False (and
@@ -975,6 +983,13 @@
order (default: iterate in forward order).
:param content: If True, load the current content of each
iterated page (default False).
+ :param until: Optional page title to stop enumerating at; only
+ meaningful when used together with *start*.
+
+ .. note:: The given argument must be lexically higher than
+ the *start* argument if *reverse* is False; otherwise it
+ must be lower.
+
:raises KeyError: The namespace identifier was not resolved
:raises TypeError: The namespace identifier has an inappropriate
type such as bool, or an iterable with more than one
@@ -993,6 +1008,8 @@
namespaces=namespace,
gapfrom=start, total=total,
g_content=content or misermode)
+ if until:
+ apgen.request['gapto'] = until
if prefix:
apgen.request['gapprefix'] = prefix
if filterredir is not None:
@@ -1324,7 +1341,7 @@
namespaces=namespaces,
total=total, g_content=content, **iuargs)
- def logevents( # docsig: disable=SIG305
+ def logevents(
self,
logtype: str | None = None,
user: str | None = None,
@@ -1714,7 +1731,7 @@
or self.has_right('undelete'))):
raise UserRightsError(err + 'deleted content.')
- def deletedrevs( # docsig: disable=SIG305
+ def deletedrevs(
self,
titles: str
| pywikibot.Page
diff --git a/tests/pagegenerators_tests.py b/tests/pagegenerators_tests.py
index 19d51b9..84bb07f 100755
--- a/tests/pagegenerators_tests.py
+++ b/tests/pagegenerators_tests.py
@@ -962,6 +962,18 @@
self.assertIsInstance(page, pywikibot.Page)
self.assertEqual(page.namespace(), 0)
+ def test_allpages_until(self) -> None:
+ """Test allpages generator."""
+ gf = pagegenerators.GeneratorFactory()
+ self.assertTrue(gf.handle_arg('-start:Python'))
+ self.assertTrue(gf.handle_arg('-until:Pywikibot'))
+ gen = gf.getCombinedGenerator()
+ self.assertIsNotNone(gen)
+ for page in gen:
+ self.assertIsInstance(page, pywikibot.Page)
+ self.assertEqual(page.namespace(), 0)
+ self.assertLessEqual(page.title(), 'Pywikibot')
+
def test_allpages_ns(self) -> None:
"""Test allpages generator with namespace argument."""
gf = pagegenerators.GeneratorFactory()
diff --git a/tests/site_generators_tests.py b/tests/site_generators_tests.py
index 4b48f49..f9a1bae 100755
--- a/tests/site_generators_tests.py
+++ b/tests/site_generators_tests.py
@@ -286,6 +286,21 @@
if self.validate_page(page):
self.assertFalse(page.isRedirectPage())
+ def test_allpages_until(self) -> None:
+ """Test the site.allpages() method with until option."""
+ start, until = 'Python', 'Pywiki'
+ fwd = list(self.site.allpages(start=start, until=until))
+ for page in fwd:
+ if self.validate_page(page):
+ self.assertLessEqual(page.title(), until)
+
+ rev = list(self.site.allpages(start=until, until=start, reverse=True))
+ for page in rev:
+ if self.validate_page(page):
+ self.assertLessEqual(page.title(), until)
+
+ self.assertLength(fwd, rev)
+
def test_allpages_langlinks_enabled(self) -> None:
"""Test allpages with langlinks enabled."""
mysite = self.get_site()
--
To view, visit
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1285524?usp=email
To unsubscribe, or for help writing mail filters, visit
https://gerrit.wikimedia.org/r/settings?usp=email
Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: If6c84dd6b9d19fc3298c29ad67868597a6a82d50
Gerrit-Change-Number: 1285524
Gerrit-PatchSet: 5
Gerrit-Owner: Xqt <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]