jenkins-bot has submitted this change and it was merged.
Change subject: category.py: select doc subpage based on family and site code ......................................................................
category.py: select doc subpage based on family and site code
List convention for Template documentation subpage in family file and retrieve it from there when needed.
Implement doc_subpage as Site property. Implemented tests in site_tests.py. Fixed some pep257 in site_tests.py.
Bug: 55017
Change-Id: I0b9b44484e2ac390ab23d1d0242adad519ff0e22 --- M pywikibot/families/wikipedia_family.py M pywikibot/families/wikisource_family.py M pywikibot/site.py M scripts/category.py M tests/site_tests.py 5 files changed, 166 insertions(+), 27 deletions(-)
Approvals: John Vandenberg: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/families/wikipedia_family.py b/pywikibot/families/wikipedia_family.py index 106203a..dc01e91 100644 --- a/pywikibot/families/wikipedia_family.py +++ b/pywikibot/families/wikipedia_family.py @@ -558,6 +558,34 @@ return 'utf-8', 'iso-8859-1' return self.code2encoding(code),
+ # Subpages for documentation. + # TODO: List is incomplete, to be completed for missing languages. + # TODO: Remove comments for appropriate pages + self.doc_subpages = { + '_default': ((u'/doc', ), + ['ar', 'bn', 'cs', 'da', 'en', 'es', 'fa', + 'hu', 'id', 'ilo', 'ja', 'ms', + 'ms', 'pt', 'ro', 'ru', 'simple', 'vi', 'zh'] + ), + 'ca': (u'/ús', ), + 'de': (u'Doku', u'/Meta'), + 'dsb': (u'/Dokumentacija', ), + 'eu': (u'txantiloi dokumentazioa', u'/dok'), + # fi: no idea how to handle this type of subpage at :Metasivu: + 'fi': ((), ), + 'fr': (u'/documentation', ), + 'hsb': (u'/Dokumentacija', ), + 'it': (u'/Man', ), + 'ka': (u'/ინფო', ), + 'ko': (u'/설명문서', ), + 'no': (u'/dok', ), + 'nn': (u'/dok', ), + 'pl': (u'/opis', ), + 'sk': (u'/Dokumentácia', ), + 'sv': (u'/dok', ), + 'uk': (u'/Документація', ), + } + def shared_data_repository(self, code, transcluded=False): """Return the shared data repository for this site.""" if code in ['test', 'test2']: diff --git a/pywikibot/families/wikisource_family.py b/pywikibot/families/wikisource_family.py index f439795..94cc43f 100644 --- a/pywikibot/families/wikisource_family.py +++ b/pywikibot/families/wikisource_family.py @@ -96,6 +96,39 @@ for item in values: self.crossnamespace[item].update({key: self.authornamespaces})
+ # Subpages for documentation. + # TODO: List is incomplete, to be completed for missing languages. + # TODO: Remove comments for appropriate pages + self.doc_subpages = { + '_default': ((u'/doc', ), + ['ar', 'as', 'az', 'bn', 'en', 'es', + 'et', 'gu', 'hu', 'it', 'ja', 'kn', 'ml', + 'mk', 'mr', 'pt', 'ro', 'sa', 'sah', 'ta', + 'te', 'th', 'vi'] + ), + 'be': (u'/Дакументацыя', ), + 'bn': (u'/নথি', ), + 'br': (u'/diellerezh', ), + 'de': (u'/Doku', u'/Meta'), + 'el': (u'/τεκμηρίωση', ), + 'eo': ('u/dokumentado', ), + #'fa': (u'/صفحه الگو', ), + #'fa': (u'/فضاینام توضیحات', ), + #'fa': (u'/آغاز جعبه', ), + #'fa': (u'/پایان جعبه۲', ), + #'fa': (u'/آغاز جعبه۲', ), + #'fa': (u'/پایان جعبه', ), + #'fa': (u'/توضیحات', ), + 'fr': (u'/documentation', ), + 'id': (u'/dok', ), + 'ko': (u'/설명문서', ), + 'no': (u'/dok', ), + 'ru': (u'/Документация', ), + 'sl': (u'/dok', ), + 'sv': (u'/dok', ), + 'uk': (u'/документація', ), + } + def shared_data_repository(self, code, transcluded=False): """Return the shared data repository for this site.""" return ('wikidata', 'wikidata') diff --git a/pywikibot/site.py b/pywikibot/site.py index ac78b07..f227434 100644 --- a/pywikibot/site.py +++ b/pywikibot/site.py @@ -482,6 +482,37 @@ """ return self.__code
+ @property + def doc_subpage(self): + """Return the documentation subpage for this Site. + + @return: tuple + + """ + if not hasattr(self, '_doc_subpage'): + try: + doc, codes = self.family.doc_subpages.get('_default', ((), [])) + if self.code not in codes: + try: + doc = self.family.doc_subpages[self.code] + # Language not defined in doc_subpages in x_family.py file + # It will use default for the family. + # should it just raise an Exception and fail? + # this will help to check the dictionary ... + except KeyError: + pywikibot.warning( + u"Site {0} has no language defined in doc_subpages dict in {1}_family.py file" + .format(self, self.family.name)) + # doc_subpages not defined in x_family.py file + except AttributeError: + doc = () # default + pywikibot.warning( + u"Site {0} has no doc_subpages dict in {1}_family.py file" + .format(self, self.family.name)) + self._doc_subpage = doc + + return self._doc_subpage + def _cmpkey(self): """Perform equality and inequality tests on Site objects.""" return (self.family.name, self.code) diff --git a/scripts/category.py b/scripts/category.py index 4bb32bb..5fc81b0 100755 --- a/scripts/category.py +++ b/scripts/category.py @@ -573,6 +573,8 @@
@param gen: Generator containing pages or categories. """ + template_docs = set() # buffer for template doc pages preloading + for page in pagegenerators.PreloadingGenerator(gen): if not self.title_regex or re.search(self.title_regex, page.title()): @@ -582,14 +584,24 @@ inPlace=self.inplace)
# Categories for templates can be included in <includeonly> section - # of Template:X/doc subpage. + # of Template:Page/doc subpage. + # TODO: doc page for a template can be Anypage/doc, as specified in + # {{Template:Documentation}} -> not managed here + # TODO: decide if/how to enable/disable this feature if page.namespace() == 10: - doc_page = pywikibot.Page(page.site, page.title() + '/doc') - if doc_page.exists(): - doc_page.change_category(self.oldcat, self.newcat, - comment=self.comment, - inPlace=self.inplace, - include=['includeonly']) + docs = page.site.doc_subpage # return tuple + for doc in docs: + doc_page = pywikibot.Page(page.site, page.title() + doc) + template_docs.add(doc_page) + + for doc_page in pagegenerators.PreloadingGenerator(template_docs): + if (doc_page.exists() and + (not self.title_regex or + re.search(self.title_regex, doc_page.title()))): + doc_page.change_category(self.oldcat, self.newcat, + comment=self.comment, + inPlace=self.inplace, + include=['includeonly'])
@staticmethod def check_move(name, old_page, new_page): diff --git a/tests/site_tests.py b/tests/site_tests.py index 248d03d..5718e65 100644 --- a/tests/site_tests.py +++ b/tests/site_tests.py @@ -59,6 +59,41 @@ self.assertEqual(token, mysite.token(mainpage, ttype))
+class TestBaseSiteProperties(TestCase): + + """Test properties for BaseSite.""" + + sites = { + 'enwk': { + 'family': 'wiktionary', + 'code': 'en', + 'result': (), # To be changed when wiktionary will have doc_subpage. + }, + 'enws': { + 'family': 'wikisource', + 'code': 'en', + 'result': ('/doc',), + }, + 'dews': { + 'family': 'wikisource', + 'code': 'de', + 'result': ('/Doku', '/Meta'), + }, + } + + cached = True + + def testProperties(self): + """Test cases for BaseSite properties.""" + # Test doc_subpage property. + mysite = self.get_site('enwk') + self.assertEqual(mysite.doc_subpage, self.sites['enwk']['result']) + mysite = self.get_site('enws') + self.assertEqual(mysite.doc_subpage, self.sites['enws']['result']) + mysite = self.get_site('dews') + self.assertEqual(mysite.doc_subpage, self.sites['dews']['result']) + + class TestSiteObject(DefaultSiteTestCase):
"""Test cases for Site methods.""" @@ -118,7 +153,7 @@ self.assertEqual(pywikibot.site.APISite.fromDBName('wikidatawiki'), pywikibot.Site('wikidata', 'wikidata'))
def testLanguageMethods(self): - """Test cases for languages() and related methods""" + """Test cases for languages() and related methods.""" mysite = self.get_site() langs = mysite.languages() self.assertIsInstance(langs, list) @@ -348,7 +383,7 @@ for el in mysite.page_extlinks(mainpage)))
def testAllPages(self): - """Test the site.allpages() method""" + """Test the site.allpages() method.""" mysite = self.get_site() fwd = list(mysite.allpages(total=10)) self.assertLessEqual(len(fwd), 10) @@ -419,7 +454,7 @@ self.assertIn("sysop", page._protection["edit"])
def testAllLinks(self): - """Test the site.alllinks() method""" + """Test the site.alllinks() method.""" mysite = self.get_site() fwd = list(mysite.alllinks(total=10)) self.assertLessEqual(len(fwd), 10) @@ -446,7 +481,7 @@ self.assertRaises(pywikibot.Error, next, errgen)
def testAllCategories(self): - """Test the site.allcategories() method""" + """Test the site.allcategories() method.""" mysite = self.get_site() ac = list(mysite.allcategories(total=10)) self.assertLessEqual(len(ac), 10) @@ -464,7 +499,7 @@ # self.assertLessEqual(cat.title(withNamespace=False), "Hij")
def testAllUsers(self): - """Test the site.allusers() method""" + """Test the site.allusers() method.""" mysite = self.get_site() au = list(mysite.allusers(total=10)) self.assertLessEqual(len(au), 10) @@ -494,7 +529,7 @@ self.assertTrue("groups" in user and "sysop" in user["groups"])
def testAllImages(self): - """Test the site.allimages() method""" + """Test the site.allimages() method.""" mysite = self.get_site() ai = list(mysite.allimages(total=10)) self.assertLessEqual(len(ai), 10) @@ -523,7 +558,7 @@ self.assertLessEqual(impage._imageinfo["size"], 2000)
def testBlocks(self): - """Test the site.blocks() method""" + """Test the site.blocks() method.""" mysite = self.get_site() props = ("id", "by", "timestamp", "expiry", "reason") bl = list(mysite.blocks(total=10)) @@ -581,7 +616,7 @@ self.assertEqual(block['user'], mysite.user())
def testExturlusage(self): - """Test the site.exturlusage() method""" + """Test the site.exturlusage() method.""" mysite = self.get_site() url = "www.google.com" eu = list(mysite.exturlusage(url, total=10)) @@ -670,7 +705,7 @@ user = True
def testLogEvents(self): - """Test the site.logevents() method""" + """Test the site.logevents() method.""" mysite = self.get_site() mainpage = self.get_mainpage() le = list(mysite.logevents(total=10)) @@ -713,7 +748,7 @@ end="2008-02-03T00:00:01Z", reverse=True, total=5)
def testRecentchanges(self): - """Test the site.recentchanges() method""" + """Test the site.recentchanges() method.""" mysite = self.get_site() mainpage = self.get_mainpage() try: @@ -817,7 +852,7 @@ self.assertNotIn("patrolled", change)
def testSearch(self): - """Test the site.search() method""" + """Test the site.search() method.""" mysite = self.get_site() try: se = list(mysite.search("wiki", total=100)) @@ -844,7 +879,7 @@ raise
def testUsercontribs(self): - """Test the site.usercontribs() method""" + """Test the site.usercontribs() method.""" mysite = self.get_site() uc = list(mysite.usercontribs(user=mysite.user(), total=10)) self.assertLessEqual(len(uc), 10) @@ -917,7 +952,7 @@ self.assertNotIn("minor", contrib)
def testWatchlistrevs(self): - """Test the site.watchlist_revs() method""" + """Test the site.watchlist_revs() method.""" mysite = self.get_site() wl = list(mysite.watchlist_revs(total=10)) self.assertLessEqual(len(wl), 10) @@ -985,7 +1020,7 @@ self.assertIsInstance(rev, dict)
def testDeletedrevs(self): - """Test the site.deletedrevs() method""" + """Test the site.deletedrevs() method.""" mysite = self.get_site() mainpage = self.get_mainpage() if not mysite.logged_in(True): @@ -1053,7 +1088,7 @@ total=5)
def testUsers(self): - """Test the site.users() method""" + """Test the site.users() method.""" mysite = self.get_site() us = list(mysite.users(mysite.user())) self.assertEqual(len(us), 1) @@ -1102,7 +1137,7 @@ """Test random methods of a site."""
def testRandompages(self): - """Test the site.randompages() method""" + """Test the site.randompages() method.""" mysite = self.get_site() rn = list(mysite.randompages(total=10)) self.assertLessEqual(len(rn), 10) @@ -1285,7 +1320,7 @@ self.mainpage = pywikibot.Page(pywikibot.Link("Main Page", self.mysite))
def testLoadRevisions_basic(self): - """Test the site.loadrevisions() method""" + """Test the site.loadrevisions() method.""" self.mysite.loadrevisions(self.mainpage, total=15) self.assertTrue(hasattr(self.mainpage, "_revid")) self.assertTrue(hasattr(self.mainpage, "_revisions")) @@ -1294,7 +1329,7 @@ self.assertEqual(self.mainpage._text, None)
def testLoadRevisions_getText(self): - """Test the site.loadrevisions() method with getText=True""" + """Test the site.loadrevisions() method with getText=True.""" self.mysite.loadrevisions(self.mainpage, getText=True, total=5) self.assertGreater(len(self.mainpage._text), 0)
@@ -1317,12 +1352,12 @@ self.mainpage, revids=130000)
def testLoadRevisions_querycontinue(self): - """Test the site.loadrevisions() method with query-continue""" + """Test the site.loadrevisions() method with query-continue.""" self.mysite.loadrevisions(self.mainpage, step=5, total=12) self.assertEqual(len(self.mainpage._revisions), 12)
def testLoadRevisions_revdir(self): - """Test the site.loadrevisions() method with rvdir=True""" + """Test the site.loadrevisions() method with rvdir=True.""" self.mysite.loadrevisions(self.mainpage, rvdir=True, total=15) self.assertEqual(len(self.mainpage._revisions), 15)
pywikibot-commits@lists.wikimedia.org