http://www.mediawiki.org/wiki/Special:Code/pywikipedia/11700
Revision: 11700 Author: legoktm Date: 2013-06-29 09:14:29 +0000 (Sat, 29 Jun 2013) Log Message: ----------- Write a constructor to create Site objects from their dbname, only for some WMF sites.
Modified Paths: -------------- branches/rewrite/pywikibot/site.py branches/rewrite/tests/site_tests.py
Modified: branches/rewrite/pywikibot/site.py =================================================================== --- branches/rewrite/pywikibot/site.py 2013-06-28 08:24:28 UTC (rev 11699) +++ branches/rewrite/pywikibot/site.py 2013-06-29 09:14:29 UTC (rev 11700) @@ -729,6 +729,24 @@ self._loginstatus = LoginStatus.NOT_ATTEMPTED return
+ @staticmethod + def fromDBName(dbname): + # TODO this only works for some WMF sites + req = api.CachedRequest(datetime.timedelta(days=10), + site=pywikibot.Site('meta', 'meta'), + action='sitematrix') + data = req.submit() + for num in data['sitematrix']: + if num in ['specials', 'count']: + continue + lang = data['sitematrix'][num]['code'] + for site in data['sitematrix'][num]['site']: + if site['dbname'] == dbname: + if site['code'] == 'wiki': + site['code'] = 'wikipedia' + return APISite(lang, site['code']) + raise ValueError("Cannot parse a site out of %s." % dbname) + def _generator(self, gen_class, type_arg=None, namespaces=None, step=None, total=None, **args): """Convenience method that returns an API generator.
Modified: branches/rewrite/tests/site_tests.py =================================================================== --- branches/rewrite/tests/site_tests.py 2013-06-28 08:24:28 UTC (rev 11699) +++ branches/rewrite/tests/site_tests.py 2013-06-29 09:14:29 UTC (rev 11700) @@ -66,6 +66,15 @@ "nosuchright"): self.assertType(mysite.has_right(rgt), bool)
+ def testConstructors(self): + """Test cases for site constructors""" + self.assertEqual(pywikibot.site.APISite.fromDBName('enwiki'), pywikibot.Site('en', 'wikipedia')) + self.assertEqual(pywikibot.site.APISite.fromDBName('eswikisource'), pywikibot.Site('es', 'wikisource')) + self.assertEqual(pywikibot.site.APISite.fromDBName('dewikinews'), pywikibot.Site('de', 'wikinews')) + self.assertEqual(pywikibot.site.APISite.fromDBName('ukwikivoyage'), pywikibot.Site('uk', 'wikivoyage')) + + self.assertRaises(ValueError, pywikibot.site.APISite.fromDBName, 'metawiki') + def testLanguageMethods(self): """Test cases for languages() and related methods"""
pywikipedia-svn@lists.wikimedia.org