jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/316321 )
Change subject: [IMPR] Provide a new generator which yields a subclass of Page ......................................................................
[IMPR] Provide a new generator which 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 accordingly.
Change-Id: Ib9107277474c603b2e687f5573afa57f2071af99 --- M pywikibot/pagegenerators.py 1 file changed, 19 insertions(+), 20 deletions(-)
Approvals: Mpaa: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/pagegenerators.py b/pywikibot/pagegenerators.py index e60c98f..95a9384 100644 --- a/pywikibot/pagegenerators.py +++ b/pywikibot/pagegenerators.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- +# -*- coding: utf-8 -*- """ This module offers a wide variety of page generators.
@@ -14,7 +14,7 @@ ¶ms; """ # -# (C) Pywikibot team, 2008-2016 +# (C) Pywikibot team, 2008-2017 # # Distributed under the terms of the MIT license. # @@ -1784,26 +1784,22 @@ return itertools.chain(*generators)
-def CategoryGenerator(generator): - """Yield pages from another generator as Category objects. +def PageClassGenerator(generator): + """ + Yield pages from another generator as Page subclass 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() == page.site.namespaces.USER: + yield pywikibot.User(page) + elif page.namespace() == page.site.namespaces.FILE: + yield pywikibot.FilePage(page) + elif page.namespace() == page.site.namespaces.CATEGORY: + yield pywikibot.Category(page) + else: + yield page
def PageWithTalkPageGenerator(generator, return_talk_only=False): @@ -2780,7 +2776,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(
pywikibot-commits@lists.wikimedia.org