jenkins-bot has submitted this change and it was merged.
Change subject: A BaseSite subclass for non MW sites. ......................................................................
A BaseSite subclass for non MW sites.
Currently the class only contains a constructor and __getattribute__. __getattribute__ method returns an attribute if present. This is done using a whitelist and not hasattr as hasattr uses getattribute of the super which goes into an infinite recursion. No attributes have been implemented yet and NotImplementedError is raised if the attribute is not present in the whitelist. A corresponsding unittest has been written for this class.
Change-Id: I5d182c6dc4b96b6f06b87ef320bdaec44c8945b8 --- M pywikibot/site.py M tests/site_tests.py 2 files changed, 41 insertions(+), 1 deletion(-)
Approvals: XZise: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/site.py b/pywikibot/site.py index 4edb41d..a6ed308 100644 --- a/pywikibot/site.py +++ b/pywikibot/site.py @@ -64,12 +64,13 @@ from pywikibot.echo import Notification
if sys.version_info[0] > 2: - from urllib.parse import urlencode + from urllib.parse import urlencode, urlparse basestring = (str,) unicode = str from itertools import zip_longest else: from urllib import urlencode + from urlparse import urlparse from itertools import izip_longest as zip_longest
@@ -1445,6 +1446,25 @@ return self._tokens.__repr__()
+class NonMWAPISite(BaseSite): + + """API interface to non MediaWiki sites.""" + + def __init__(self, url): + """Constructor.""" + self.netloc = urlparse(url).netloc + + def __getattribute__(self, attr): + """Return attribute if present else raise NotImplementedError.""" + whitelist = ['__getattribute__', 'netloc'] + if attr in whitelist: + return super(NonMWAPISite, self).__getattribute__(attr) + else: + raise NotImplementedError('The attribute %s has not been on ' + 'site %s implemented yet.' + % (attr, self.netloc)) + + class APISite(BaseSite):
"""API interface to MediaWiki site. diff --git a/tests/site_tests.py b/tests/site_tests.py index db78da7..3d116e7 100644 --- a/tests/site_tests.py +++ b/tests/site_tests.py @@ -2455,6 +2455,26 @@ pywikibot.Site, 'en', 'wikidata')
+class TestNonMWAPISite(TestCase): + + """Test the BaseSite subclass, site.NonMWAPISite.""" + + net = False + + def testNonMWsites(self): + """Test NonMWAPISite for sites not using MediaWiki.""" + self._run_test("http://moinmo.in/$1") + self._run_test("http://twiki.org/cgi-bin/view/$1") + self._run_test("http://www.usemod.com/cgi-bin/wiki.pl?$1") + self._run_test("https://developer.mozilla.org/en/docs/$1") + self._run_test("http://www.tvtropes.org/pmwiki/pmwiki.php/Main/$1") + + def _run_test(self, url): + site = pywikibot.site.NonMWAPISite(url) + with self.assertRaises(NotImplementedError): + site.attr + + if __name__ == '__main__': try: unittest.main()
pywikibot-commits@lists.wikimedia.org