[Pywikipedia-l] SVN: [6058] branches/rewrite/pywikibot
russblau at svn.wikimedia.org
russblau at svn.wikimedia.org
Tue Nov 4 20:42:39 UTC 2008
Revision: 6058
Author: russblau
Date: 2008-11-04 20:42:39 +0000 (Tue, 04 Nov 2008)
Log Message:
-----------
bugfixes and backwards-compatibility improvements
Modified Paths:
--------------
branches/rewrite/pywikibot/__init__.py
branches/rewrite/pywikibot/family.py
branches/rewrite/pywikibot/login.py
branches/rewrite/pywikibot/site.py
Modified: branches/rewrite/pywikibot/__init__.py
===================================================================
--- branches/rewrite/pywikibot/__init__.py 2008-11-04 17:43:43 UTC (rev 6057)
+++ branches/rewrite/pywikibot/__init__.py 2008-11-04 20:42:39 UTC (rev 6058)
@@ -15,11 +15,43 @@
from exceptions import *
import config
+
+def deprecate_arg(old_arg, new_arg):
+ """Decorator to declare old_arg deprecated and replace it with new_arg"""
+ logger = logging.getLogger()
+ def decorator(method):
+ def wrapper(*__args, **__kw):
+ meth_name = method.__name__
+ if old_arg in __kw:
+ if new_arg:
+ if new_arg in __kw:
+ logger.warn(
+"%(new_arg)s argument of %(meth_name)s replaces %(old_arg)s; cannot use both."
+ % locals())
+ else:
+ logger.debug(
+"%(old_arg)s argument of %(meth_name)s is deprecated; use %(new_arg)s instead."
+ % locals())
+ __kw[new_arg] = __kw[old_arg]
+ else:
+ logger.debug(
+ "%(old_arg)s argument of %(meth_name)s is deprecated."
+ % locals())
+ del __kw[old_arg]
+ return method(*__args, **__kw)
+ wrapper.__doc__ = method.__doc__
+ wrapper.__name__ = method.__name__
+ return wrapper
+ return decorator
+
+
_sites = {}
default_family = config.family
default_code = config.mylang
-def Site(code=None, fam=None, user=None, interface=None):
+
+ at deprecate_arg("persistent_http", None)
+def Site(code=None, fam=None, user=None, sysop=None, interface=None):
"""Return the specified Site object.
Returns a cached object if possible, otherwise instantiates a new one.
@@ -43,6 +75,11 @@
user = config.usernames[fam][code]
except KeyError:
user = None
+ if sysop is None:
+ try:
+ sysop = config.sysopnames[fam][code]
+ except KeyError:
+ sysop = None
if interface is None:
interface = config.site_interface
try:
@@ -51,7 +88,7 @@
raise ValueError("Invalid interface name '%(interface)s'" % locals())
key = '%s:%s:%s' % (fam, code, user)
if not _sites.has_key(key):
- _sites[key] = __Site(code=code, fam=fam, user=user)
+ _sites[key] = __Site(code=code, fam=fam, user=user, sysop=sysop)
logger.debug("Instantiating Site object '%(site)s'"
% {'site': _sites[key]})
return _sites[key]
Modified: branches/rewrite/pywikibot/family.py
===================================================================
--- branches/rewrite/pywikibot/family.py 2008-11-04 17:43:43 UTC (rev 6057)
+++ branches/rewrite/pywikibot/family.py 2008-11-04 20:42:39 UTC (rev 6058)
@@ -7,7 +7,7 @@
import re
import urllib
-from datetime import timedelta
+from datetime import datetime, timedelta
logger = logging.getLogger("wiki")
@@ -1144,13 +1144,13 @@
"""Return the shared image repository, if any."""
return (None, None)
- def server_time(self):
- """Returns a datetime object representing server time"""
- # TODO : If the local computer time is wrong, result wll be wrong
+ def server_time(self, code):
+ """Return a datetime object representing server time"""
+ # TODO : If the local computer time is wrong, result will be wrong
return datetime.utcnow() + self.servergmtoffset
- def isPublic(self):
- """Does the wiki require logging in before viewing it ?"""
+ def isPublic(self, code):
+ """Does the wiki require logging in before viewing it?"""
return True
def post_get_convert(self, site, getText):
Modified: branches/rewrite/pywikibot/login.py
===================================================================
--- branches/rewrite/pywikibot/login.py 2008-11-04 17:43:43 UTC (rev 6057)
+++ branches/rewrite/pywikibot/login.py 2008-11-04 20:42:39 UTC (rev 6058)
@@ -4,7 +4,7 @@
Script to log the robot in to a wiki account.
Suggestion is to make a special account to use for robot use only. Make
-sure this robot account is well known on your home wikipedia before using.
+sure this robot account is well known on your home wiki before using.
Parameters:
@@ -133,67 +133,8 @@
Returns cookie data if succesful, None otherwise.
"""
- if config.use_api_login:
- predata = {
- 'action': 'login',
- 'lgname': self.username.encode(self.site.encoding()),
- 'lgpassword': self.password,
- 'lgdomain': self.site.family.ldapDomain,
- }
- address = self.site.api_address()
- else:
- predata = {
- "wpName": self.username.encode(self.site.encoding()),
- "wpPassword": self.password,
- "wpDomain": self.site.family.ldapDomain, # VistaPrint fix
- "wpLoginattempt": "Aanmelden & Inschrijven", # dutch button label seems to work for all wikis
- "wpRemember": str(int(bool(remember))),
- "wpSkipCookieCheck": '1'
- }
- if captcha:
- predata["wpCaptchaId"] = captcha['id']
- predata["wpCaptchaWord"] = captcha['answer']
- login_address = self.site.login_address()
- address = login_address + '&action=submit'
+ # NOT IMPLEMENTED - see data/api.py for implementation
- if self.site.hostname() in config.authenticate.keys():
- headers = {
- "Content-type": "application/x-www-form-urlencoded",
- "User-agent": wikipedia.useragent
- }
- data = self.site.urlEncode(predata)
- response = urllib2.urlopen(
- urllib2.Request(self.site.protocol()
- + '://' + self.site.hostname()
- + address, data, headers))
- data = response.read()
- wikipedia.cj.save(wikipedia.COOKIEFILE)
- return "Ok"
- else:
- response, data = self.site.postData(address, self.site.urlEncode(predata))
- Reat=re.compile(': (.*?);')
- L = []
-
- for eat in response.msg.getallmatchingheaders('set-cookie'):
- m = Reat.search(eat)
- if m:
- L.append(m.group(1))
-
- got_token = got_user = False
- for Ldata in L:
- if 'Token=' in Ldata:
- got_token = True
- if 'User=' in Ldata or 'UserName=' in Ldata:
- got_user = True
-
- if got_token and got_user:
- return "\n".join(L)
- elif not captcha:
- solve = self.site.solveCaptcha(data)
- if solve:
- return self.getCookie(remember = remember, captcha = solve)
- return None
-
def storecookiedata(self, data):
"""
Store cookie data.
Modified: branches/rewrite/pywikibot/site.py
===================================================================
--- branches/rewrite/pywikibot/site.py 2008-11-04 17:43:43 UTC (rev 6057)
+++ branches/rewrite/pywikibot/site.py 2008-11-04 20:42:39 UTC (rev 6058)
@@ -11,6 +11,7 @@
__version__ = '$Id: $'
import pywikibot
+from pywikibot import deprecate_arg
from pywikibot.throttle import Throttle
from pywikibot.data import api
from pywikibot.exceptions import *
@@ -29,35 +30,6 @@
logger = logging.getLogger("wiki")
-
-def deprecate_arg(old_arg, new_arg):
- """Decorator to declare old_arg deprecated and replace it with new_arg"""
- def decorator(method):
- def wrapper(*__args, **__kw):
- meth_name = method.__name__
- if old_arg in __kw:
- if new_arg:
- if new_arg in __kw:
- logger.warn(
-"%(new_arg)s argument of %(meth_name)s replaces %(old_arg)s; cannot use both."
- % locals())
- else:
- logger.debug(
-"%(old_arg)s argument of %(meth_name)s is deprecated; use %(new_arg)s instead."
- % locals())
- __kw[new_arg] = __kw[old_arg]
- else:
- logger.debug(
- "%(old_arg)s argument of %(meth_name)s is deprecated."
- % locals())
- del __kw[old_arg]
- return method(*__args, **__kw)
- wrapper.__doc__ = method.__doc__
- wrapper.__name__ = method.__name__
- return wrapper
- return decorator
-
-
class PageInUse(pywikibot.Error):
"""Page cannot be reserved for writing due to existing lock."""
@@ -146,7 +118,8 @@
"""Return this Site's throttle. Initialize a new one if needed."""
if not hasattr(self, "_throttle"):
- self._throttle = Throttle(self, multiplydelay=True, verbosedelay=True)
+ self._throttle = Throttle(self, multiplydelay=True,
+ verbosedelay=True)
try:
self.login(False)
except pywikibot.NoUsername:
@@ -192,6 +165,9 @@
return self._username[False]
return None
+ def username(self, sysop = False):
+ return self._username[sysop]
+
def __getattr__(self, attr):
"""Calls to methods not defined in this object are passed to Family."""
@@ -389,7 +365,36 @@
return pywikibot.Site(code=code, fam=self.family, user=self.user)
+ def urlEncode(self, query):
+ """DEPRECATED"""
+ return urllib.urlencode(query)
+ def getUrl(self, path, retry=True, sysop=False, data=None,
+ compress=True, no_hostname=False, cookie_only=False):
+ """DEPRECATED.
+
+ Retained for compatibility only. All arguments except path and data
+ are ignored.
+
+ """
+ if data:
+ if not isinstance(data, basestring):
+ data = urllib.urlencode(data)
+ return pywikibot.comms.data.request(self, path, method="PUT",
+ body=data)
+ else:
+ return pywikibot.comms.data.request(self, path)
+
+ def postForm(self, address, predata, sysop=False, cookies=None):
+ """DEPRECATED"""
+ return self.getUrl(address, data=predata)
+
+ def postData(self, address, data, contentType=None, sysop=False,
+ compress=True, cookies=None):
+ """DEPRECATED"""
+ return self.getUrl(address, data=data)
+
+
class APISite(BaseSite):
"""API interface to MediaWiki site.
@@ -399,10 +404,8 @@
## Site methods from version 1.0 (as these are implemented in this file,
## or declared deprecated/obsolete, they will be removed from this list)
##########
-## messages: return True if there are new messages on the site
## cookies: return user's cookies as a string
##
-## getUrl: retrieve an URL from the site
## urlEncode: Encode a query to be sent using an http POST request.
## postForm: Post form data to an address at this site.
## postData: Post encoded form data to an http address at this site.
@@ -439,8 +442,8 @@
## withoutinterwiki: Special:Withoutinterwiki
## linksearch: Special:Linksearch
- def __init__(self, code, fam=None, user=None):
- BaseSite.__init__(self, code, fam, user)
+ def __init__(self, code, fam=None, user=None, sysop=None):
+ BaseSite.__init__(self, code, fam, user, sysop)
self._namespaces = {
# these are the MediaWiki built-in names, which always work
# localized names are loaded later upon accessing the wiki
@@ -566,6 +569,12 @@
"Site method 'isBlocked' should be changed to 'is_blocked'")
return self.is_blocked(sysop)
+ def checkBlocks(self, sysop = False):
+ """Check if the user is blocked, and raise an exception if so."""
+ if self.is_blocked(sysop):
+ # User blocked
+ raise UserBlocked('User is blocked in site %s' % self)
+
def has_right(self, right, sysop=False):
"""Return true if and only if the user has a specific right.
@@ -1203,10 +1212,11 @@
yield linkdata['*']
@deprecate_arg("throttle", None)
+ @deprecate_arg("includeredirects", "filterredir")
def allpages(self, start="!", prefix="", namespace=0, filterredir=None,
filterlanglinks=None, minsize=None, maxsize=None,
protect_type=None, protect_level=None, limit=None,
- reverse=False, includeRedirects=None):
+ reverse=False, includeredirects=None):
"""Iterate pages in a single namespace.
Note: parameters includeRedirects and throttle are deprecated and
@@ -1234,15 +1244,16 @@
all pages in namespace)
@param reverse: if True, iterate in reverse Unicode lexigraphic
order (default: iterate in forward order)
+ @param includeredirects: DEPRECATED, use filterredirs instead
"""
if not isinstance(namespace, int):
raise Error("allpages: only one namespace permitted.")
- if includeRedirects is not None:
+ if includeredirects is not None:
logger.debug(
"allpages: 'includeRedirects' argument is deprecated; use 'filterredirs'.")
- if includeRedirects:
- if includeRedirects == "only":
+ if includeredirects:
+ if includeredirects == "only":
filterredirs = True
else:
filterredirs = None
@@ -1283,7 +1294,7 @@
"""
logger.debug("Site.prefixindex() is deprecated; use allpages instead.")
return self.allpages(prefix=prefix, namespace=namespace,
- includeRedirects=includeredirects)
+ includeredirects=includeredirects)
def alllinks(self, start="!", prefix="", namespace=0, unique=False,
More information about the Pywikipedia-l
mailing list