Просмотр исходного кода

Add loggers to Category, Page, and User.

tags/v0.1^2
Ben Kurtovic 12 лет назад
Родитель
Сommit
d07f0b5f9a
3 измененных файлов: 25 добавлений и 6 удалений
  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 Просмотреть файл

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

from hashlib import md5
from logging import getLogger, NullHandler
import re
from time import gmtime, strftime
from urllib import quote
@@ -82,7 +83,8 @@ class Page(CopyvioMixIn):
PAGE_MISSING = 2
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.

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._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._is_redirect = None
self._lastrevid = None


+ 5
- 4
earwigbot/wiki/site.py Просмотреть файл

@@ -789,8 +789,9 @@ class Site(object):
prefix = title.split(":", 1)[0]
if prefix != title: # Avoid a page that is simply "Category"
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):
"""Return a :py:class:`Category` object for the given category name.
@@ -802,7 +803,7 @@ class Site(object):
catname = self._unicodeify(catname)
prefix = self.namespace_id_to_name(constants.NS_CATEGORY)
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):
"""Return a :py:class:`User` object for the given username.
@@ -815,7 +816,7 @@ class Site(object):
username = self._unicodeify(username)
else:
username = self._get_username()
return User(self, username)
return User(self, username, self._logger)

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


+ 9
- 1
earwigbot/wiki/user.py Просмотреть файл

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

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

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

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

Takes two arguments, a Site object (necessary for doing API queries),
@@ -76,6 +77,13 @@ class User(object):
self._site = site
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):
"""Return the canonical string representation of the User."""
return "User(name={0!r}, site={1!r})".format(self._name, self._site)


Загрузка…
Отмена
Сохранить