Xqt has uploaded a new change for review.


Change subject: [IMPR] Provide a new generator wich yields a subclass of Page

[IMPR] Provide a new generator wich yields a subclass of Page

- PageClassGenerator yields objects of Page, FilePage, Category or User
  which depends on the namespace found for the page.
- redirect the FileGenerator and CategoryGenerator to this new generator
  and deprecate them by using redirect_func.
- Now you haven't care about the page class coming from generator as before.
  You always get the right type. It is up to the developer to use them

Change-Id: Ib9107277474c603b2e687f5573afa57f2071af99
M pywikibot/pagegenerators.py
1 file changed, 15 insertions(+), 19 deletions(-)

  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 

diff --git a/pywikibot/pagegenerators.py b/pywikibot/pagegenerators.py
index e60c98f..022d289 100644
--- a/pywikibot/pagegenerators.py
+++ b/pywikibot/pagegenerators.py
@@ -1783,27 +1783,20 @@
     """Yield from each iterable until exhausted, then proceed with the next."""
     return itertools.chain(*generators)
+def PageClassGenerator(generator):
+    """
+    Yield pages from another generator as Page subclass objects.
-def CategoryGenerator(generator):
-    """Yield pages from another generator as Category objects.
-    Makes sense only if it is ascertained that only categories are being
-    retrieved.
+    The page class type depends on the page namespace.
+    Objects may be Category, FilePage, Userpage or Page.
     for page in generator:
-        yield pywikibot.Category(page)
-def FileGenerator(generator):
-    """
-    Yield pages from another generator as FilePage objects.
-    Makes sense only if it is ascertained
-    that only images are being retrieved.
-    """
-    for page in generator:
-        yield pywikibot.FilePage(page)
+        if page.namespace() == p.site.namespaces.USER:
+            yield pywikibot.User(page)
+        elif page.namespace() == p.site.namespaces.FILE:
+            yield pywikibot.FilePage(page)
+        elif page.namespace() == p.site.namespaces.CATEGORY:
+            yield pywikibot.Category(page)
 def PageWithTalkPageGenerator(generator, return_talk_only=False):
@@ -2780,7 +2773,10 @@
 # Deprecated old names available for compatibility with compat.
-ImageGenerator = redirect_func(FileGenerator, old_name='ImageGenerator')
+ImageGenerator = redirect_func(PageClassGenerator, old_name='ImageGenerator')
+FileGenerator = redirect_func(PageClassGenerator, old_name='FileGenerator')
+CategoryGenerator = redirect_func(PageClassGenerator,
+                                  old_name='CategoryGenerator')
 UnCategorizedTemplatesGenerator = redirect_func(
     UnCategorizedTemplateGenerator, old_name='UnCategorizedTemplatesGenerator')
 RecentchangesPageGenerator = redirect_func(

To view, visit https://gerrit.wikimedia.org/r/316321
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib9107277474c603b2e687f5573afa57f2071af99
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Xqt <i...@gno.de>

MediaWiki-commits mailing list

Reply via email to