Xqt has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1288511?usp=email )

Change subject: redirect: simplify get_redirect_pages_via_api method by using 
until parameter
......................................................................

redirect: simplify get_redirect_pages_via_api method by using until parameter

- use opt.limit and opt.until in get_redirect_pages_via_api method
- use islice to limit the generated  pages in get_redirect_pages_via_api
- fix membership test for source; the set has only one element here
  because multiple elements leads to a warning and leaves the script.
- fix spelling mistakes

Change-Id: I5bb6d75422342bf7b00b2f9e4ecaa7e1523dec84
---
M scripts/redirect.py
1 file changed, 17 insertions(+), 16 deletions(-)

Approvals:
  Xqt: Verified; Looks good to me, approved




diff --git a/scripts/redirect.py b/scripts/redirect.py
index 1318302..b27e3ef 100755
--- a/scripts/redirect.py
+++ b/scripts/redirect.py
@@ -82,6 +82,7 @@
 import datetime
 from collections.abc import Generator
 from contextlib import suppress
+from itertools import islice
 from textwrap import fill
 from typing import Any

@@ -209,18 +210,18 @@

     def get_redirect_pages_via_api(self) -> Generator[pywikibot.Page]:
         """Yield Pages that are redirects."""
-        for ns in self.opt.namespaces:
-            gen = self.site.allpages(start=self.opt.start,
-                                     namespace=ns,
-                                     filterredir=True)
-            if self.opt.limit:
-                gen.set_maximum_items(self.opt.limit)
-            for p in gen:
-                done = (self.opt.until
-                        and p.title(with_ns=False) >= self.opt.until)
-                if done:
-                    return
-                yield p
+        pages = (
+            page
+            for ns in self.opt.namespaces
+            for page in self.site.allpages(
+                start=self.opt.start,
+                until=self.opt.until,
+                namespace=ns,
+                filterredir=True,
+                total=self.opt.limit
+            )
+        )
+        yield from islice(pages, self.opt.limit)

     def _next_redirect_group(self) -> Generator[list[pywikibot.Page]]:
         """Generator that yields batches of redirects as a list."""
@@ -505,7 +506,7 @@
                 return

             if redir_page.namespace() != movedTarget.namespace():
-                pywikibot.info(f'Namespace of {redir_page} is different'
+                pywikibot.info(f'Namespace of {redir_page} is different '
                                f'from target page {movedTarget}')
             elif redir_page == movedTarget:
                 pywikibot.info('Redirect to target page forms a redirect loop')
@@ -576,7 +577,7 @@
                 return False  # do nothing
 
             pywikibot.info(
-                f'Skipping: Redirect target {new_redir} is not a  redirect.')
+                f'Skipping: Redirect target {new_redir} is not a redirect.')

         elif isinstance(error, SectionError):
             pywikibot.warning(
@@ -601,7 +602,7 @@
             pywikibot.info('Skipping due to server error: No textarea found')

         else:
-            # all uncatched exceptions
+            # all uncaught exceptions
             # Note: elif statements are necessary above because all Errors
             # above derive from Exception class
             raise error
@@ -768,7 +769,7 @@
         gen = RedirectGenerator(action, **gen_options)

     if gen_factory.gens \
-       or action != 'both' and source not in ('-fullscan', '-xml'):
+       or action != 'both' and next(iter(source)) not in ('-fullscan', '-xml'):
         gen = gen_factory.getCombinedGenerator(gen=gen)

     bot = RedirectRobot(action, generator=gen, **options)

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1288511?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: I5bb6d75422342bf7b00b2f9e4ecaa7e1523dec84
Gerrit-Change-Number: 1288511
Gerrit-PatchSet: 3
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]

Reply via email to