Browse Source

Rollback most of the last commit, which was unnecessary as these values are API-loaded and therefore always unicode.

tags/v0.1^2
Ben Kurtovic 12 years ago
parent
commit
0365e0b3b3
4 changed files with 26 additions and 30 deletions
  1. +0
    -20
      earwigbot/wiki/__init__.py
  2. +2
    -3
      earwigbot/wiki/page.py
  3. +22
    -5
      earwigbot/wiki/site.py
  4. +2
    -2
      earwigbot/wiki/user.py

+ 0
- 20
earwigbot/wiki/__init__.py View File

@@ -43,26 +43,6 @@ of your :py:class:`~earwigbot.wiki.site.Site` (and thus,
:py:class:`~earwigbot.wiki.user.User`) needs. :py:class:`~earwigbot.wiki.user.User`) needs.
""" """


from urllib import quote_plus

# Some helper functions:

def unicodeify(value, encoding="utf8"):
"""Return input as unicode if it's not unicode to begin with."""
if isinstance(value, unicode):
return value
return unicode(value, encoding)

def urlencode_utf8(params):
"""Implement urllib.urlencode() with support for unicode input."""
enc = lambda s: s.encode("utf8") if isinstance(s, unicode) else str(s)
args = []
for key, val in params.iteritems():
key = quote_plus(enc(key))
val = quote_plus(enc(val))
args.append(key + "=" + val)
return "&".join(args)

from earwigbot.wiki.category import * from earwigbot.wiki.category import *
from earwigbot.wiki.constants import * from earwigbot.wiki.constants import *
from earwigbot.wiki.page import * from earwigbot.wiki.page import *


+ 2
- 3
earwigbot/wiki/page.py View File

@@ -31,7 +31,6 @@ except ImportError:
mwparserfromhell = None mwparserfromhell = None


from earwigbot import exceptions from earwigbot import exceptions
from earwigbot.wiki import unicodeify
from earwigbot.wiki.copyright import CopyrightMixIn from earwigbot.wiki.copyright import CopyrightMixIn


__all__ = ["Page"] __all__ = ["Page"]
@@ -150,7 +149,7 @@ class Page(CopyrightMixIn):
contains "[") it will always be invalid, and cannot be edited. contains "[") it will always be invalid, and cannot be edited.
""" """
if self._exists == self.PAGE_INVALID: if self._exists == self.PAGE_INVALID:
e = u"Page '{0}' is invalid.".format(unicodeify(self._title))
e = u"Page '{0}' is invalid.".format(self._title)
raise exceptions.InvalidPageError(e) raise exceptions.InvalidPageError(e)


def _assert_existence(self): def _assert_existence(self):
@@ -162,7 +161,7 @@ class Page(CopyrightMixIn):
""" """
self._assert_validity() self._assert_validity()
if self._exists == self.PAGE_MISSING: if self._exists == self.PAGE_MISSING:
e = u"Page '{0}' does not exist.".format(unicodeify(self._title))
e = u"Page '{0}' does not exist.".format(self._title)
raise exceptions.PageNotFoundError(e) raise exceptions.PageNotFoundError(e)


def _load(self): def _load(self):


+ 22
- 5
earwigbot/wiki/site.py View File

@@ -29,6 +29,7 @@ from re import escape as re_escape, match as re_match
from StringIO import StringIO from StringIO import StringIO
from threading import Lock from threading import Lock
from time import sleep, time from time import sleep, time
from urllib import quote_plus
from urllib2 import build_opener, HTTPCookieProcessor, URLError from urllib2 import build_opener, HTTPCookieProcessor, URLError
from urlparse import urlparse from urlparse import urlparse


@@ -38,7 +39,7 @@ except ImportError:
oursql = None oursql = None


from earwigbot import exceptions from earwigbot import exceptions
from earwigbot.wiki import constants, unicodeify, urlencode_utf8
from earwigbot.wiki import constants
from earwigbot.wiki.category import Category from earwigbot.wiki.category import Category
from earwigbot.wiki.page import Page from earwigbot.wiki.page import Page
from earwigbot.wiki.user import User from earwigbot.wiki.user import User
@@ -184,6 +185,22 @@ class Site(object):
res = "<Site {0} ({1}:{2}) at {3}>" res = "<Site {0} ({1}:{2}) at {3}>"
return res.format(self.name, self.project, self.lang, self.domain) return res.format(self.name, self.project, self.lang, self.domain)


def _unicodeify(self, value, encoding="utf8"):
"""Return input as unicode if it's not unicode to begin with."""
if isinstance(value, unicode):
return value
return unicode(value, encoding)

def _urlencode_utf8(self, params):
"""Implement urllib.urlencode() with support for unicode input."""
enc = lambda s: s.encode("utf8") if isinstance(s, unicode) else str(s)
args = []
for key, val in params.iteritems():
key = quote_plus(enc(key))
val = quote_plus(enc(val))
args.append(key + "=" + val)
return "&".join(args)

def _api_query(self, params, tries=0, wait=5): def _api_query(self, params, tries=0, wait=5):
"""Do an API query with *params* as a dict of parameters. """Do an API query with *params* as a dict of parameters.


@@ -234,7 +251,7 @@ class Site(object):
if self._maxlag: # If requested, don't overload the servers if self._maxlag: # If requested, don't overload the servers
params["maxlag"] = self._maxlag params["maxlag"] = self._maxlag


data = urlencode_utf8(params)
data = self._urlencode_utf8(params)
return url, data return url, data


def _handle_api_query_result(self, result, params, tries, wait): def _handle_api_query_result(self, result, params, tries, wait):
@@ -690,7 +707,7 @@ class Site(object):
redirect-following: :py:class:`~earwigbot.wiki.page.Page`'s methods redirect-following: :py:class:`~earwigbot.wiki.page.Page`'s methods
provide that. provide that.
""" """
title = unicodeify(title)
title = self._unicodeify(title)
prefixes = self.namespace_id_to_name(constants.NS_CATEGORY, all=True) prefixes = self.namespace_id_to_name(constants.NS_CATEGORY, all=True)
prefix = title.split(":", 1)[0] prefix = title.split(":", 1)[0]
if prefix != title: # Avoid a page that is simply "Category" if prefix != title: # Avoid a page that is simply "Category"
@@ -705,7 +722,7 @@ class Site(object):
really just shorthand for :py:meth:`get_page("Category:" + catname) really just shorthand for :py:meth:`get_page("Category:" + catname)
<get_page>`. <get_page>`.
""" """
catname = unicodeify(catname)
catname = self._unicodeify(catname)
name = name if isinstance(name, unicode) else name.decode("utf8") name = name if isinstance(name, unicode) else name.decode("utf8")
prefix = self.namespace_id_to_name(constants.NS_CATEGORY) prefix = self.namespace_id_to_name(constants.NS_CATEGORY)
pagename = u':'.join((prefix, catname)) pagename = u':'.join((prefix, catname))
@@ -718,7 +735,7 @@ class Site(object):
:py:class:`~earwigbot.wiki.user.User` object representing the currently :py:class:`~earwigbot.wiki.user.User` object representing the currently
logged-in (or anonymous!) user is returned. logged-in (or anonymous!) user is returned.
""" """
username = unicodeify(username)
username = self._unicodeify(username)
if not username: if not username:
username = self._get_username() username = self._get_username()
return User(self, username) return User(self, username)

+ 2
- 2
earwigbot/wiki/user.py View File

@@ -23,7 +23,7 @@
from time import gmtime, strptime from time import gmtime, strptime


from earwigbot.exceptions import UserNotFoundError from earwigbot.exceptions import UserNotFoundError
from earwigbot.wiki import constants, unicodeify
from earwigbot.wiki import constants
from earwigbot.wiki.page import Page from earwigbot.wiki.page import Page


__all__ = ["User"] __all__ = ["User"]
@@ -96,7 +96,7 @@ class User(object):
if not hasattr(self, attr): if not hasattr(self, attr):
self._load_attributes() self._load_attributes()
if not self._exists: if not self._exists:
e = u"User '{0}' does not exist.".format(unicodeify(self._name))
e = u"User '{0}' does not exist.".format(self._name)
raise UserNotFoundError(e) raise UserNotFoundError(e)
return getattr(self, attr) return getattr(self, attr)




Loading…
Cancel
Save