]> projects.mako.cc - python-simplemediawiki.debian/blobdiff - simplemediawiki.py
merge patched into master
[python-simplemediawiki.debian] / simplemediawiki.py
index 05dd471456e83d6f31b73de2b95342017a5f9dfd..ec12a11754d8efdb157e68435a5335ac9c86e030 100644 (file)
@@ -33,12 +33,18 @@ of namespaces are provided for your convenience.
 
 import cookielib
 import gzip
 
 import cookielib
 import gzip
-from iso8601 import iso8601
+import iso8601
 import json
 from StringIO import StringIO
 import urllib
 import urllib2
 
 import json
 from StringIO import StringIO
 import urllib
 import urllib2
 
+__author__ = 'Ian Weller <ian@ianweller.org>'
+__version__ = '1.0.2'
+DEFAULT_UA = ('python-simplemediawiki/%s '
+              '+https://github.com/ianweller/python-simplemediawiki') \
+        % __version__
+
 
 class MediaWiki():
     """
 
 class MediaWiki():
     """
@@ -61,10 +67,16 @@ class MediaWiki():
                 self._cj.load()
         else:
             self._cj = cookielib.CookieJar()
                 self._cj.load()
         else:
             self._cj = cookielib.CookieJar()
-        self._opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self._cj))
+        self._opener = urllib2.build_opener(
+                urllib2.HTTPCookieProcessor(self._cj)
+        )
         self._opener.addheaders = [('User-agent', user_agent)]
 
     def _fetch_http(self, url, params):
         self._opener.addheaders = [('User-agent', user_agent)]
 
     def _fetch_http(self, url, params):
+        """
+        Standard HTTP request handler for this class with gzip and cookie
+        support.
+        """
         request = urllib2.Request(url, urllib.urlencode(params))
         request.add_header('Accept-encoding', 'gzip')
         response = self._opener.open(request)
         request = urllib2.Request(url, urllib.urlencode(params))
         request.add_header('Accept-encoding', 'gzip')
         response = self._opener.open(request)
@@ -92,7 +104,20 @@ class MediaWiki():
         returns that URL, while also helpfully setting this object's API URL to
         it. If it can't magically conjure an API endpoint, it returns False.
         """
         returns that URL, while also helpfully setting this object's API URL to
         it. If it can't magically conjure an API endpoint, it returns False.
         """
-        data, data_json = self._normalize_api_url_tester(self._api_url)
+        def tester(self, api_url):
+            """
+            Attempts to fetch general information about the MediaWiki instance
+            in order to test whether the given URL will return JSON.
+            """
+            data = self._fetch_http(api_url, {'action': 'query',
+                                              'meta': 'siteinfo'})
+            try:
+                data_json = json.loads(data)
+                return (data, data_json)
+            except ValueError:
+                return (data, None)
+
+        data, data_json = tester(self, self._api_url)
         if data_json:
             return self._api_url
         else:
         if data_json:
             return self._api_url
         else:
@@ -100,30 +125,13 @@ class MediaWiki():
             if 'index.php' in self._api_url:
                 test_api_url = self._api_url.split('index.php')[0] + 'api.php'
                 print test_api_url
             if 'index.php' in self._api_url:
                 test_api_url = self._api_url.split('index.php')[0] + 'api.php'
                 print test_api_url
-                test_data, test_data_json = \
-                        self._normalize_api_url_tester(test_api_url)
+                test_data, test_data_json = tester(self, test_api_url)
                 print (test_data, test_data_json)
                 if test_data_json:
                     self._api_url = test_api_url
                     return self._api_url
             return False
 
                 print (test_data, test_data_json)
                 if test_data_json:
                     self._api_url = test_api_url
                     return self._api_url
             return False
 
-    def _normalize_api_url_tester(self, api_url):
-        data = self._fetch_http(api_url, {'action': 'query',
-                                          'meta': 'siteinfo',
-                                          'siprop': 'general',
-                                          'format': 'json'})
-        try:
-            data_json = json.loads(data)
-            # may as well set the version
-            try:
-                version_string = data_json['query']['general']['generator']
-                self._mediawiki_version = version_string.split(' ', 1)[1]
-            except KeyError:
-                pass
-            return (data, data_json)
-        except ValueError:
-            return (data, None)
 
     def login(self, user, passwd, token=None):
         """
 
     def login(self, user, passwd, token=None):
         """
@@ -203,10 +211,3 @@ class MediaWiki():
         objects.
         """
         return iso8601.parse_date(date)
         objects.
         """
         return iso8601.parse_date(date)
-
-
-__author__ = 'Ian Weller <ian@ianweller.org>'
-__version__ = '1.0.1'
-DEFAULT_UA = 'python-simplemediawiki/%s ' + \
-        '+https://github.com/ianweller/python-simplemediawiki' \
-        % __version__

Benjamin Mako Hill || Want to submit a patch?