jenkins-bot has submitted this change and it was merged.
Change subject: Access any mediawiki site without a family file ......................................................................
Access any mediawiki site without a family file
AutoFamily dynamically provides Family capabilities without a special class file, using only the wiki URL. The URL may be only the protocol and hostname, or a URL to the API php script.
pywikibot.site.Family uses AutoFamily if it finds a URL has been provided instead of a file path.
To use a mediawiki site without a family file, add the following to user-config.py
family_files['mhwp'] = 'https://mh.wikipedia.org/' usernames['whwp']['*']=u'xyz'
or
family_files['w3c'] = 'https://www.w3.org/wiki/api.php'
Bug: 70795 Change-Id: I9ad83fe996bc240fb9d5f27b866a022fccd313c0 --- M pywikibot/family.py M pywikibot/site.py 2 files changed, 41 insertions(+), 0 deletions(-)
Approvals: XZise: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/family.py b/pywikibot/family.py index d2e6a6d..ed6baf2 100644 --- a/pywikibot/family.py +++ b/pywikibot/family.py @@ -8,9 +8,15 @@ __version__ = '$Id$' #
+import sys import logging import re import collections + +if sys.version_info[0] == 2: + from urlparse import urlparse +else: + from urllib.parse import urlparse
import pywikibot from pywikibot import config2 as config @@ -1091,4 +1097,30 @@ return ('commons', 'commons')
def protocol(self, code): + """Return 'https' as the protocol.""" return 'https' + + +class AutoFamily(Family): + + """Family that automatically loads the site configuration.""" + + def __init__(self, name, url, site=None): + """Constructor.""" + super(AutoFamily, self).__init__() + self.name = name + self.url = urlparse(url) + self.langs = { + name: self.url.netloc + } + + def protocol(self, code): + """Return the protocol of the URL.""" + return self.url.scheme + + def scriptpath(self, code): + """Extract the script path from the URL.""" + if self.url.path.endswith('/api.php'): + return self.url.path[0:-8] + else: + return super(AutoFamily, self).scriptpath(code) diff --git a/pywikibot/site.py b/pywikibot/site.py index a5c1754..630a5aa 100644 --- a/pywikibot/site.py +++ b/pywikibot/site.py @@ -28,6 +28,7 @@
import pywikibot from pywikibot import config +from pywikibot.family import AutoFamily from pywikibot.tools import itergroup, deprecated, deprecate_arg from pywikibot.throttle import Throttle from pywikibot.data import api @@ -118,6 +119,14 @@ if fam in _families: return _families[fam]
+ if fam in config.family_files: + family_file = config.family_files[fam] + + if family_file.startswith('http://') or family_file.startswith('https://'): + myfamily = AutoFamily(fam, family_file) + _families[fam] = myfamily + return _families[fam] + try: # Ignore warnings due to dots in family names. import warnings