Revision: 5514
Author: russblau
Date: 2008-06-04 20:56:45 +0000 (Wed, 04 Jun 2008)
Log Message:
-----------
Prevent unnecessary instantiation of Site objects
Modified Paths:
--------------
branches/rewrite/pywikibot/__init__.py
branches/rewrite/pywikibot/data/api.py
branches/rewrite/pywikibot/login.py
branches/rewrite/pywikibot/page.py
Modified: branches/rewrite/pywikibot/__init__.py
===================================================================
--- branches/rewrite/pywikibot/__init__.py 2008-06-04 14:29:31 UTC (rev 5513)
+++ branches/rewrite/pywikibot/__init__.py 2008-06-04 20:56:45 UTC (rev 5514)
@@ -51,6 +51,8 @@
key = '%s:%s:%s' % (fam, code, user)
if not _sites.has_key(key):
_sites[key] = __Site(code=code, fam=fam, user=user)
+ logging.debug("Instantiating Site object '%s'"
+ % _sites[key])
return _sites[key]
getSite = Site # alias for backwards-compability
Modified: branches/rewrite/pywikibot/data/api.py
===================================================================
--- branches/rewrite/pywikibot/data/api.py 2008-06-04 14:29:31 UTC (rev 5513)
+++ branches/rewrite/pywikibot/data/api.py 2008-06-04 20:56:45 UTC (rev 5514)
@@ -89,7 +89,10 @@
"""
def __init__(self, **kwargs):
- self.site = kwargs.pop("site", pywikibot.Site())
+ try:
+ self.site = kwargs.pop("site")
+ except KeyError:
+ self.site = pywikibot.Site()
self.max_retries = kwargs.pop("max_retries", 25)
self.retry_wait = kwargs.pop("retry_wait", 5)
self.params = {}
@@ -160,7 +163,9 @@
self.site.throttle() # TODO: add write=True when needed
uri = self.site.scriptpath() + "/api.php"
try:
- if self.params.get("action", "") in ("login",):
+ if self.params.get("action", "") in (
+ "login", "edit"):
+ # add other actions that require POST requests above
rawdata = http.request(self.site, uri, method="POST",
headers={'Content-Type':
'application/x-www-form-urlencoded'},
@@ -259,7 +264,10 @@
% (self.__class__.__name__, kwargs["query"]))
else:
kwargs["action"] = "query"
- self.site = kwargs.get("site", pywikibot.Site())
+ try:
+ self.site = kwargs["site"]
+ except KeyError:
+ self.site = pywikibot.Site()
# make sure request type is valid, and get limit key if any
if "generator" in kwargs:
self.module = kwargs["generator"]
Modified: branches/rewrite/pywikibot/login.py
===================================================================
--- branches/rewrite/pywikibot/login.py 2008-06-04 14:29:31 UTC (rev 5513)
+++ branches/rewrite/pywikibot/login.py 2008-06-04 20:56:45 UTC (rev 5514)
@@ -68,7 +68,10 @@
class LoginManager:
def __init__(self, password = None, sysop = False, site = None):
- self.site = site or pywikibot.Site()
+ if site is not None:
+ self.site = site
+ else:
+ self.site = pywikibot.Site()
if sysop:
try:
self.username = config.sysopnames\
Modified: branches/rewrite/pywikibot/page.py
===================================================================
--- branches/rewrite/pywikibot/page.py 2008-06-04 14:29:31 UTC (rev 5513)
+++ branches/rewrite/pywikibot/page.py 2008-06-04 20:56:45 UTC (rev 5514)
@@ -152,9 +152,10 @@
encodedTitle = title.encode(self.site().encoding())
title = urllib.quote(encodedTitle)
if asLink:
- if forceInterwiki or (
- allowInterwiki and self.site() != pywikibot.Site()):
- if self.site().family != pywikibot.Site().family \
+ if forceInterwiki or (allowInterwiki and
+ (self.site().family != pywikibot.default_family
+ or self.site().code != pywikibot.default_code)):
+ if self.site().family != pywikibot.default_family \
and self.site().family.name != self.site().code:
return u'[[%s:%s:%s]]' % (self.site().family.name,
self.site().code,
@@ -332,7 +333,8 @@
if not hasattr(self, '_text'):
try:
self._text = self.get()
- except NoPage: # TODO: what other exceptions might be returned?
+ except pywikibot.NoPage:
+ # TODO: what other exceptions might be returned?
self._text = u""
return self._text