Fix not being able to import the module
[python-simplemediawiki.debian] / simplemediawiki.py
index b3e7f1def9fdb1ef6a844f8a69b5b1433698a970..6bdd9441aada2bb7c9d94a71278a4114f3b16f69 100644 (file)
@@ -39,6 +39,12 @@ 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():
     """
@@ -49,8 +55,9 @@ class MediaWiki():
     _high_limits = None
     _namespaces = None
     _psuedo_namespaces = None
+    _mediawiki_version = None
 
-    def __init__(self, api_url, cookie_file=None):
+    def __init__(self, api_url, cookie_file=None, user_agent=DEFAULT_UA):
         self._api_url = api_url
         if cookie_file:
             self._cj = cookielib.MozillaCookieJar(cookie_file)
@@ -61,7 +68,10 @@ class MediaWiki():
                 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):
         request = urllib2.Request(url, urllib.urlencode(params))
@@ -137,12 +147,22 @@ class MediaWiki():
             data['lgtoken'] = token
         result = self.call(data)
         if result['login']['result'] == 'Success':
+            self._high_limits = None
             return True
         elif result['login']['result'] == 'NeedToken' and not token:
             return self.login(user, passwd, result['login']['token'])
         else:
             return False
 
+    def logout(self):
+        """
+        Conveinence function for logging out of the wiki.
+        """
+        data = {'action': 'logout'}
+        self.call(data)
+        self._high_limits = None
+        return True
+
     def limits(self, low, high):
         """
         Convenience function for determining appropriate limits in the API. If
@@ -192,7 +212,3 @@ class MediaWiki():
         objects.
         """
         return iso8601.parse_date(date)
-
-
-__author__ = 'Ian Weller <ian@ianweller.org>'
-__version__ = '1.0.1'

Benjamin Mako Hill || Want to submit a patch?