Browse Source

Add loggers to Category, Page, and User.

tags/v0.1^2
Ben Kurtovic 12 years ago
parent
commit
d07f0b5f9a
3 changed files with 25 additions and 6 deletions
  1. +11
    -1
      earwigbot/wiki/page.py
  2. +5
    -4
      earwigbot/wiki/site.py
  3. +9
    -1
      earwigbot/wiki/user.py

+ 11
- 1
earwigbot/wiki/page.py View File

@@ -21,6 +21,7 @@
# SOFTWARE. # SOFTWARE.


from hashlib import md5 from hashlib import md5
from logging import getLogger, NullHandler
import re import re
from time import gmtime, strftime from time import gmtime, strftime
from urllib import quote from urllib import quote
@@ -82,7 +83,8 @@ class Page(CopyvioMixIn):
PAGE_MISSING = 2 PAGE_MISSING = 2
PAGE_EXISTS = 3 PAGE_EXISTS = 3


def __init__(self, site, title, follow_redirects=False, pageid=None):
def __init__(self, site, title, follow_redirects=False, pageid=None,
logger=None):
"""Constructor for new Page instances. """Constructor for new Page instances.


Takes four arguments: a Site object, the Page's title (or pagename), Takes four arguments: a Site object, the Page's title (or pagename),
@@ -101,6 +103,14 @@ class Page(CopyvioMixIn):
self._follow_redirects = self._keep_following = follow_redirects self._follow_redirects = self._keep_following = follow_redirects
self._pageid = pageid self._pageid = pageid


# Set up our internal logger:
if logger:
self._logger = logger
else: # Just set up a null logger to eat up our messages:
self._logger = getLogger("earwigbot.wiki")
self._logger.addHandler(NullHandler())

# Attributes to be loaded through the API:
self._exists = self.PAGE_UNKNOWN self._exists = self.PAGE_UNKNOWN
self._is_redirect = None self._is_redirect = None
self._lastrevid = None self._lastrevid = None


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

@@ -789,8 +789,9 @@ class Site(object):
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"
if prefix in prefixes: if prefix in prefixes:
return Category(self, title, follow_redirects, pageid)
return Page(self, title, follow_redirects, pageid)
return Category(self, title, follow_redirects, pageid,
self._logger)
return Page(self, title, follow_redirects, pageid, self._logger)


def get_category(self, catname, follow_redirects=False, pageid=None): def get_category(self, catname, follow_redirects=False, pageid=None):
"""Return a :py:class:`Category` object for the given category name. """Return a :py:class:`Category` object for the given category name.
@@ -802,7 +803,7 @@ class Site(object):
catname = self._unicodeify(catname) catname = self._unicodeify(catname)
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))
return Category(self, pagename, follow_redirects, pageid)
return Category(self, pagename, follow_redirects, pageid, self._logger)


def get_user(self, username=None): def get_user(self, username=None):
"""Return a :py:class:`User` object for the given username. """Return a :py:class:`User` object for the given username.
@@ -815,7 +816,7 @@ class Site(object):
username = self._unicodeify(username) username = self._unicodeify(username)
else: else:
username = self._get_username() username = self._get_username()
return User(self, username)
return User(self, username, self._logger)


def delegate(self, services, args=None, kwargs=None): def delegate(self, services, args=None, kwargs=None):
"""Delegate a task to either the API or SQL depending on conditions. """Delegate a task to either the API or SQL depending on conditions.


+ 9
- 1
earwigbot/wiki/user.py View File

@@ -20,6 +20,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE. # SOFTWARE.


from logging import getLogger, NullHandler
from time import gmtime, strptime from time import gmtime, strptime


from earwigbot.exceptions import UserNotFoundError from earwigbot.exceptions import UserNotFoundError
@@ -60,7 +61,7 @@ class User(object):
talkpage talkpage
""" """


def __init__(self, site, name):
def __init__(self, site, name, logger=None):
"""Constructor for new User instances. """Constructor for new User instances.


Takes two arguments, a Site object (necessary for doing API queries), Takes two arguments, a Site object (necessary for doing API queries),
@@ -76,6 +77,13 @@ class User(object):
self._site = site self._site = site
self._name = name self._name = name


# Set up our internal logger:
if logger:
self._logger = logger
else: # Just set up a null logger to eat up our messages:
self._logger = getLogger("earwigbot.wiki")
self._logger.addHandler(NullHandler())

def __repr__(self): def __repr__(self):
"""Return the canonical string representation of the User.""" """Return the canonical string representation of the User."""
return "User(name={0!r}, site={1!r})".format(self._name, self._site) return "User(name={0!r}, site={1!r})".format(self._name, self._site)


Loading…
Cancel
Save