Quellcode durchsuchen

Improvements to Site and User by removing unnecessary API queries.

* Site: New _get_logged_in_user() method, name self-explanatory. This acts
  as a replacement for the former crud in get_user(), which now calls this
  when the username arg is None. This method will first try to determine
  our username based on a special cookie in self._cookiejar (cookie.name is
  self._name + "UserName", e.g. "enwikiUserName"), and will only do an API
  query if no cookie was found. This removes an API query that is usually
  only necessary if we are not logged in.
* Site: silly bugfix in __init__().
* User: Reverted earlier change to _get_attribute() (addition of
  raise_exception arg); name() and exists() now use their own code, which
  is simpler.
* User: Calling name() does not do an API query unless force=True, unlike
  the other "get" methods.
* User: .join() instead of .format() because I feel it looks cleaner and is
  probably more efficient.
tags/v0.1^2
Ben Kurtovic vor 13 Jahren
Ursprung
Commit
c7bbb21117
2 geänderte Dateien mit 32 neuen und 12 gelöschten Zeilen
  1. +22
    -6
      wiki/tools/site.py
  2. +10
    -6
      wiki/tools/user.py

+ 22
- 6
wiki/tools/site.py Datei anzeigen

@@ -2,8 +2,9 @@

from cookielib import CookieJar
from json import loads
from urllib import urlencode
from urllib import unquote_plus, urlencode
from urllib2 import build_opener, HTTPCookieProcessor, URLError
from urlparse import urlparse

from wiki.tools.category import Category
from wiki.tools.constants import *
@@ -34,7 +35,7 @@ class Site(object):
self._namespaces = namespaces

# set up cookiejar and URL opener for making API queries
self._cookiejar = CookieJar(cookie_file)
self._cookiejar = CookieJar()
self._opener = build_opener(HTTPCookieProcessor(self._cookiejar))
self._opener.addheaders = [('User-agent', USER_AGENT)]

@@ -72,6 +73,24 @@ class Site(object):
e = "Couldn't login; server says '{0}'.".format(res)
raise LoginError(e)

def _get_logged_in_user(self):
"""
Docstring needed
"""
# first try to get username from the cookie jar to avoid an
# unnecessary API query
cookie_name = ''.join((self._name, "UserName"))
cookie_domain = urlparse(self._base_url).netloc
for cookie in self._cookiejar:
if cookie.name == cookie_name and cookie.domain == cookie_domain:
return unquote_plus(cookie.value)
# if we end up here, we're probably an anon and thus an API query
# will be required to get our username
params = {"action": "query", "meta": "userinfo"}
result = self.api_query(params)
return result["query"]["userinfo"]["name"]

def _load_attributes(self, force=False):
"""
Docstring needed
@@ -253,8 +272,5 @@ class Site(object):
Docstring needed
"""
if username is None:
params = {"action": "query", "meta": "userinfo"}
result = self.api_query(params)
username = result["query"]["userinfo"]["name"]

username = self._get_logged_in_user()
return User(self, username)

+ 10
- 6
wiki/tools/user.py Datei anzeigen

@@ -30,13 +30,13 @@ class User(object):
self._emailable = None
self._gender = None

def _get_attribute(self, attr, force, raise_exception=True):
def _get_attribute(self, attr, force):
"""
Docstring needed
"""
if self._exists is None or force:
self._load_attributes()
if self._exists is False and raise_exception:
if self._exists is False:
e = "User '{0}' does not exist.".format(self._name)
raise UserNotFoundError(e)
return getattr(self, attr)
@@ -84,13 +84,17 @@ class User(object):
"""
Docstring needed
"""
return self._get_attribute("_name", force, raise_exception=False)
if force:
self._load_attributes()
return self._name

def exists(self, force=False):
"""
Docstring needed
"""
return self._get_attribute("_exists", force, raise_exception=False)
if self._exists is None or force:
self._load_attributes()
return self._exists

def userid(self, force=False):
"""
@@ -145,7 +149,7 @@ class User(object):
Docstring needed
"""
prefix = self.site.namespace_id_to_name(NS_USER)
pagename = "{0}:{1}".format(prefix, self._name)
pagename = ''.join((prefix, ":", self._name))
return Page(self.site, pagename)

def talkpage(self):
@@ -153,5 +157,5 @@ class User(object):
Docstring needed
"""
prefix = self.site.namespace_id_to_name(NS_USER_TALK)
pagename = "{0}:{1}".format(prefix, self._name)
pagename = ''.join((prefix, ":", self._name))
return Page(self.site, pagename)

Laden…
Abbrechen
Speichern