Revision: 8031
Author: xqt
Date: 2010-03-22 06:47:06 +0000 (Mon, 22 Mar 2010)
Log Message:
-----------
set code line length to 80 chars
Modified Paths:
--------------
trunk/pywikipedia/add_text.py
trunk/pywikipedia/pagegenerators.py
Modified: trunk/pywikipedia/add_text.py
===================================================================
--- trunk/pywikipedia/add_text.py 2010-03-22 06:45:57 UTC (rev 8030)
+++ trunk/pywikipedia/add_text.py 2010-03-22 06:47:06 UTC (rev 8031)
@@ -128,7 +128,8 @@
return text
def untaggedGenerator(untaggedProject, limit = 500):
- """ Function to get the pages returned by this tool: http://toolserver.org/~daniel/WikiSense/UntaggedImages.php """
+ """ Function to get the pages returned by this tool:
+ http://toolserver.org/~daniel/WikiSense/UntaggedImages.php """
lang = untaggedProject.split('.', 1)[0]
project = '.' + untaggedProject.split('.', 1)[1]
if lang == 'commons':
@@ -141,20 +142,23 @@
results = re.findall(regexp, text)
if results == []:
print link
- raise NothingFound('Nothing found! Try to use the tool by yourself to be sure that it works!')
+ raise NothingFound(
+'Nothing found! Try to use the tool by yourself to be sure that it works!')
else:
for result in results:
yield wikipedia.Page(wikipedia.getSite(), result)
-def add_text(page = None, addText = None, summary = None, regexSkip = None, regexSkipUrl = None,
- always = False, up = False, putText = True, oldTextGiven = None, create=False):
+def add_text(page = None, addText = None, summary = None, regexSkip = None,
+ regexSkipUrl = None, always = False, up = False, putText = True,
+ oldTextGiven = None, create=False):
if not addText:
raise NoEnoughData('You have to specify what text you want to add!')
if not summary:
summary = wikipedia.translate(wikipedia.getSite(), msg) % addText[:200]
- # When a page is tagged as "really well written" it has a star in the interwiki links.
- # This is a list of all the templates used (in regex format) to make the stars appear.
+ # When a page is tagged as "really well written" it has a star in the
+ # interwiki links. This is a list of all the templates used (in regex
+ # format) to make the stars appear.
starsList = [
u'bueno',
u'cyswllt[ _]erthygl[ _]ddethol', u'dolen[ _]ed',
@@ -171,7 +175,8 @@
u'ligam[ _]adq',
u'ligoelstara',
u'ligoleginda',
- u'link[ _][afgu]a', u'link[ _]adq', u'link[ _]f[lm]', u'link[ _]km', u'link[ _]sm', u'linkfa',
+ u'link[ _][afgu]a', u'link[ _]adq', u'link[ _]f[lm]', u'link[ _]km',
+ u'link[ _]sm', u'linkfa',
u'na[ _]lotura',
u'nasc[ _]ar',
u'tengill[ _][úg]g',
@@ -194,7 +199,8 @@
text = page.get()
except wikipedia.NoPage:
if create:
- wikipedia.output(u"%s doesn't exist, creating it!" % page.title())
+ wikipedia.output(u"%s doesn't exist, creating it!"
+ % page.title())
text = u''
else:
wikipedia.output(u"%s doesn't exist, skip!" % page.title())
@@ -210,12 +216,14 @@
url = '%s%s' % (pathWiki, page.urlname())
result = re.findall(regexSkipUrl, site.getUrl(url))
if result != []:
- wikipedia.output(u'Exception! regex (or word) used with -exceptUrl is in the page. Skip!')
+ wikipedia.output(
+u'Exception! regex (or word) used with -exceptUrl is in the page. Skip!')
return (False, False, always) # continue
if regexSkip != None:
result = re.findall(regexSkip, text)
if result != []:
- wikipedia.output(u'Exception! regex (or word) used with -except is in the page. Skip!')
+ wikipedia.output(
+u'Exception! regex (or word) used with -except is in the page. Skip!')
return (False, False, always) # continue
# If not up, text put below
if not up:
@@ -243,7 +251,8 @@
# Dealing the stars' issue
allstars = []
for star in starsList:
- regex = re.compile('(\{\{(?:template:|)%s\|.*?\}\}[\s]*)' % star, re.I)
+ regex = re.compile('(\{\{(?:template:|)%s\|.*?\}\}[\s]*)' % star,
+ re.I)
found = regex.findall(newtext)
if found != []:
newtext = regex.sub('', newtext)
@@ -259,15 +268,18 @@
else:
newtext = addText + '\n' + text
if putText and text != newtext:
- wikipedia.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % page.title())
+ wikipedia.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<"
+ % page.title())
wikipedia.showDiff(text, newtext)
# Let's put the changes.
while True:
- # If someone load it as module, maybe it's not so useful to put the text in the page
+ # If someone load it as module, maybe it's not so useful to put the
+ # text in the page
if putText:
if not always:
- choice = wikipedia.inputChoice(u'Do you want to accept these changes?',
- ['Yes', 'No', 'All'], ['y', 'N', 'a'], 'N')
+ choice = wikipedia.inputChoice(
+ u'Do you want to accept these changes?',
+ ['Yes', 'No', 'All'], ['y', 'N', 'a'], 'N')
if choice == 'a':
always = True
elif choice == 'n':
@@ -290,13 +302,16 @@
else:
raise wikipedia.ServerError(u'Fifth Server Error!')
except wikipedia.SpamfilterError, e:
- wikipedia.output(u'Cannot change %s because of blacklist entry %s' % (page.title(), e.url))
+ wikipedia.output(
+ u'Cannot change %s because of blacklist entry %s'
+ % (page.title(), e.url))
return (False, False, always)
except wikipedia.PageNotSaved, error:
wikipedia.output(u'Error putting page: %s' % error.args)
return (False, False, always)
except wikipedia.LockedPage:
- wikipedia.output(u'Skipping %s (locked page)' % page.title())
+ wikipedia.output(u'Skipping %s (locked page)'
+ % page.title())
return (False, False, always)
else:
# Break only if the errors are one after the other...
@@ -320,7 +335,8 @@
for arg in wikipedia.handleArgs():
if arg.startswith('-textfile'):
if len(arg) == 9:
- textfile = wikipedia.input(u'Which textfile do you want to add?')
+ textfile = wikipedia.input(
+ u'Which textfile do you want to add?')
else:
textfile = arg[10:]
elif arg.startswith('-text'):
@@ -335,7 +351,10 @@
summary = arg[9:]
elif arg.startswith('-page'):
if len(arg) == 5:
- generator = [wikipedia.Page(wikipedia.getSite(), wikipedia.input(u'What page do you want to use?'))]
+ generator = [wikipedia.Page(
+ wikipedia.getSite(),
+ wikipedia.input(u'What page do you want to use?')
+ )]
else:
generator = [wikipedia.Page(wikipedia.getSite(), arg[6:])]
elif arg.startswith('-excepturl'):
@@ -350,7 +369,8 @@
regexSkip = arg[8:]
elif arg.startswith('-untagged'):
if len(arg) == 9:
- untaggedProject = wikipedia.input(u'What project do you want to use?')
+ untaggedProject = wikipedia.input(
+ u'What project do you want to use?')
else:
untaggedProject = arg[10:]
generator = untaggedGenerator(untaggedProject)
@@ -370,7 +390,8 @@
generator = genFactory.getCombinedGenerator()
# Check if there are the minimal settings
if not generator:
- raise NoEnoughData('You have to specify the generator you want to use for the script!')
+ raise NoEnoughData(
+ 'You have to specify the generator you want to use for the script!')
if talkPage:
generator = pagegenerators.PageWithTalkPageGenerator(generator)
site = wikipedia.getSite()
@@ -378,10 +399,13 @@
index = site.getNamespaceIndex(namespace)
if index%2==1 and index>0:
namespaces += [index]
- generator = pagegenerators.NamespaceFilterPageGenerator(generator, namespaces)
+ generator = pagegenerators.NamespaceFilterPageGenerator(
+ generator, namespaces)
# Main Loop
for page in generator:
- (text, newtext, always) = add_text(page, addText, summary, regexSkip, regexSkipUrl, always, up, True, create=talkPage)
+ (text, newtext, always) = add_text(page, addText, summary, regexSkip,
+ regexSkipUrl, always, up, True,
+ create=talkPage)
if __name__ == "__main__":
try:
Modified: trunk/pywikipedia/pagegenerators.py
===================================================================
--- trunk/pywikipedia/pagegenerators.py 2010-03-22 06:45:57 UTC (rev 8030)
+++ trunk/pywikipedia/pagegenerators.py 2010-03-22 06:47:06 UTC (rev 8031)
@@ -13,8 +13,26 @@
¶ms;
"""
+#
+# (C) Pywikipedia bot team, 2005-2010
+#
+# Distributed under the terms of the MIT license.
+#
__version__='$Id$'
+import wikipedia as pywikibot
+import config
+
+import traceback
+import Queue
+import re
+import sys
+import threading
+import codecs
+
+import urllib, urllib2, time
+import date, catlib, userlib, query
+
parameterHelp = u"""\
-cat Work on all pages which are in a specific category.
Argument can also be given as "-cat:categoryname" or
@@ -173,16 +191,6 @@
docuReplacements = {'¶ms;': parameterHelp}
-# Standard library imports
-import re, codecs, sys
-import threading, Queue, traceback
-import urllib, urllib2, time
-
-# Application specific imports
-import wikipedia as pywikibot
-import date, catlib, userlib, query
-import config
-
# For python 2.4 compatibility
# see http://www.mail-archive.com/python-dev@python.org/msg12668.html
try:
Revision: 8029
Author: xqt
Date: 2010-03-19 06:06:48 +0000 (Fri, 19 Mar 2010)
Log Message:
-----------
typo bugfix
Modified Paths:
--------------
trunk/pywikipedia/wikipedia.py
Modified: trunk/pywikipedia/wikipedia.py
===================================================================
--- trunk/pywikipedia/wikipedia.py 2010-03-18 17:29:14 UTC (rev 8028)
+++ trunk/pywikipedia/wikipedia.py 2010-03-19 06:06:48 UTC (rev 8029)
@@ -4027,7 +4027,7 @@
# TODO: why isn't this a Site method?
pages = list(pages) # if pages is an iterator, we need to make it a list
output(u'Getting %d pages %s from %s...'
- % (len(pages), iif(site.has_api(), u'via API', u'') site))
+ % (len(pages), iif(site.has_api(), u'via API', u''), site))
limit = config.special_page_limit / 4 # default is 500/4, but It might have good point for server.
if len(pages) > limit:
# separate export pages for bulk-retrieve
Revision: 8028
Author: russblau
Date: 2010-03-18 17:29:14 +0000 (Thu, 18 Mar 2010)
Log Message:
-----------
Introducing a much saner API for user output (five functions in the pywikibot namespace: debug(), log(), output(), warning(), error() in increasing order of severity, instead of using pywikibot.output for all) and finally getting -debug:foo option to work to filter debugging output.
Modified Paths:
--------------
branches/rewrite/pywikibot/__init__.py
branches/rewrite/pywikibot/bot.py
branches/rewrite/pywikibot/comms/http.py
branches/rewrite/pywikibot/comms/threadedhttp.py
branches/rewrite/pywikibot/data/api.py
branches/rewrite/pywikibot/data/logentries.py
branches/rewrite/pywikibot/login.py
branches/rewrite/pywikibot/page.py
branches/rewrite/pywikibot/pagegenerators.py
branches/rewrite/pywikibot/site.py
branches/rewrite/pywikibot/throttle.py
branches/rewrite/pywikibot/userinterfaces/terminal_interface.py
branches/rewrite/scripts/redirect.py
branches/rewrite/scripts/solve_disambiguation.py
branches/rewrite/scripts/touch.py
branches/rewrite/scripts/upload.py
Modified: branches/rewrite/pywikibot/__init__.py
===================================================================
--- branches/rewrite/pywikibot/__init__.py 2010-03-18 16:52:31 UTC (rev 8027)
+++ branches/rewrite/pywikibot/__init__.py 2010-03-18 17:29:14 UTC (rev 8028)
@@ -83,12 +83,10 @@
funcname = method.func_name
classname = args[0].__class__.__name__
if instead:
- output(u"%s.%s is DEPRECATED, use %s instead."
- % (classname, funcname, instead),
- level=WARNING)
+ warning(u"%s.%s is DEPRECATED, use %s instead."
+ % (classname, funcname, instead))
else:
- output(u"%s.%s is DEPRECATED." % (classname, funcname),
- level=WARNING)
+ warning(u"%s.%s is DEPRECATED." % (classname, funcname))
return method(*args, **kwargs)
wrapper.func_name = method.func_name
return wrapper
@@ -96,25 +94,26 @@
def deprecate_arg(old_arg, new_arg):
"""Decorator to declare old_arg deprecated and replace it with new_arg"""
- logger = logging.getLogger("pywiki")
+ _logger = ""
def decorator(method):
def wrapper(*__args, **__kw):
meth_name = method.__name__
if old_arg in __kw:
if new_arg:
if new_arg in __kw:
- pywikibot.output(
-"%(new_arg)s argument of %(meth_name)s replaces %(old_arg)s; cannot use both."
- % locals(), level=WARNING)
+ pywikibot.warning(
+u"%(new_arg)s argument of %(meth_name)s replaces %(old_arg)s; cannot use both."
+ % locals())
else:
- pywikibot.output(
-"%(old_arg)s argument of %(meth_name)s is deprecated; use %(new_arg)s instead."
- % locals(), level=WARNING)
+ pywikibot.warning(
+u"%(old_arg)s argument of %(meth_name)s is deprecated; use %(new_arg)s instead."
+ % locals())
__kw[new_arg] = __kw[old_arg]
else:
- pywikibot.output(
- "%(old_arg)s argument of %(meth_name)s is deprecated."
- % locals(), level=DEBUG)
+ pywikibot.debug(
+ u"%(old_arg)s argument of %(meth_name)s is deprecated."
+ % locals(),
+ _logger)
del __kw[old_arg]
return method(*__args, **__kw)
wrapper.__doc__ = method.__doc__
@@ -139,7 +138,7 @@
@type user: unicode
"""
- logger = logging.getLogger("pywiki.wiki")
+ _logger = "wiki"
if code is None:
code = config.mylang
@@ -165,9 +164,9 @@
key = '%s:%s:%s' % (fam, code, user)
if not key in _sites:
_sites[key] = __Site(code=code, fam=fam, user=user, sysop=sysop)
- pywikibot.output(u"Instantiating Site object '%(site)s'"
+ pywikibot.debug(u"Instantiating Site object '%(site)s'"
% {'site': _sites[key]},
- level=pywikibot.DEBUG)
+ _logger)
return _sites[key]
getSite = Site # alias for backwards-compability
@@ -184,27 +183,6 @@
config.default_edit_summary = s
-def set_debug(layer):
- """Set the logger for specified layer to DEBUG level.
-
- The framework has four layers (by default, others can be added), each
- designated by a string --
-
- 1. "comm": the communication layer (http requests, etc.)
- 2. "data": the raw data layer (API requests, XML dump parsing)
- 3. "wiki": the wiki content representation layer (Page and Site objects)
- 4. "bot": the application layer
-
- This method sets the logger for any specified layer to the DEBUG level,
- causing it to output extensive debugging information. If this method is
- not called for a layer, the default logging setting is the INFO level.
-
- This method does not check the 'layer' argument for validity.
-
- """
- logging.getLogger("pywiki."+layer).setLevel(DEBUG)
-
-
def showDiff(oldtext, newtext):
"""
Output a string showing the differences between oldtext and newtext.
@@ -281,11 +259,10 @@
"""
global stopped
- logger = logging.getLogger("pywiki.wiki")
+ _logger = "wiki"
if not stopped:
- pywikibot.output(u"stopme() called",
- level=pywikibot.DEBUG)
+ pywikibot.debug(u"stopme() called", _logger)
count = sum(1 for thd in threadpool if thd.isAlive())
if count:
pywikibot.output(u"Waiting for about %(count)s pages to be saved."
@@ -297,7 +274,7 @@
# only need one drop() call because all throttles use the same global pid
try:
_sites[_sites.keys()[0]].throttle.drop()
- pywikibot.output(u"Dropped throttle(s).", level=VERBOSE)
+ pywikibot.log(u"Dropped throttle(s).")
except IndexError:
pass
Modified: branches/rewrite/pywikibot/bot.py
===================================================================
--- branches/rewrite/pywikibot/bot.py 2010-03-18 16:52:31 UTC (rev 8027)
+++ branches/rewrite/pywikibot/bot.py 2010-03-18 17:29:14 UTC (rev 8028)
@@ -21,7 +21,7 @@
import sys
# logging levels
-logger = logging.getLogger("pywiki.bot")
+_logger = "bot"
from logging import DEBUG, INFO, WARNING, ERROR, CRITICAL
STDOUT = 16
@@ -39,22 +39,6 @@
fromlist=['UI'] )
ui = uiModule.UI()
-# next bit filched from 1.5.2's inspect.py
-def currentframe():
- """Return the frame object for the caller's stack frame."""
- try:
- raise Exception
- except:
- return sys.exc_traceback.tb_frame.f_back
-
-if hasattr(sys, '_getframe'):
- # less portable but more efficient
- currentframe = lambda: sys._getframe(2)
- # frame0 is this lambda, frame1 is output() in this module,
- # so frame2 is whatever called output()
-
-# done filching
-
# Logging module configuration
class MaxLevelFilter(logging.Filter):
@@ -101,33 +85,55 @@
return strExc + '\n'
-def output(text, decoder=None, newline=True, toStdout=False, level=INFO):
- """Output a message to the user via the userinterface.
+# User output/logging functions
- Works like print, but uses the encoding used by the user's console
- (console_encoding in the configuration file) instead of ASCII.
- If decoder is None, text should be a unicode string. Otherwise it
- should be encoded in the given encoding.
+# Five output functions are defined. Each requires a unicode or string
+# argument. All of these functions generate a message to the log file if
+# logging is enabled ("-log" or "-debug" command line arguments).
- If newline is True, a linebreak will be added after printing the text.
+# The functions output(), warning(), and error() all display a message to the
+# user through the logger object; the only difference is the priority level,
+# which can be used by the application layer to alter the display.
- If toStdout is True, the text will be sent to standard output,
- so that it can be piped to another process. All other text will
- be sent to stderr. See: http://en.wikipedia.org/wiki/Pipeline_%28Unix%29
+# The function log() by default does not display a message to the user, but
+# this can be altered by using the "-verbose" command line option.
- text can contain special sequences to create colored output. These
- consist of the escape character \03 and the color name in curly braces,
- e. g. \03{lightpurple}. \03{default} resets the color.
+# The function debug() only logs its messages, they are never displayed on
+# the user console. debug() takes a required second argument, which is a
+# string indicating the debugging layer.
- @param level: output level for logging module; use VERBOSE for optional
- messages, INPUT for prompts requiring user reponse (not yet fully
- implemented)
+# next bit filched from 1.5.2's inspect.py
+def currentframe():
+ """Return the frame object for the caller's stack frame."""
+ try:
+ raise Exception
+ except:
+ # go back two levels, one for _fmtoutput and one for whatever called it
+ return sys.exc_traceback.tb_frame.f_back.f_back
+if hasattr(sys, '_getframe'):
+ # less portable but more efficient
+ currentframe = lambda: sys._getframe(3)
+ # frame0 is this lambda, frame1 is _fmtoutput() in this module,
+ # frame2 is the convenience function (output(), etc.)
+ # so frame3 is whatever called the convenience function
+
+# done filching
+
+def _fmtoutput(text, decoder=None, newline=True, toStdout=False,
+ _level=INFO, _logger=""):
+ """Format output and send to the logging module.
+
+ Backend function used by all the user-output convenience functions.
+
"""
+ if _logger:
+ path = "pywiki." + _logger
+ else:
+ path = "pywiki"
+
# make sure logging system has been initialized
- root = logging.getLogger("pywiki")
- if not root.level:
- # init_handlers sets this level; if it is 0, it needs to be set
+ if not _handlers_initialized:
init_handlers(strm=ui.output_stream)
frame = currentframe()
@@ -152,9 +158,46 @@
if newline:
text += "\n"
if toStdout:
- level = STDOUT
- ui.output(text, level=level, context=context)
+ _level = STDOUT
+ logger = logging.getLogger(path)
+ ui.output(text, logger, _level, context)
+def output(text, decoder=None, newline=True, toStdout=False):
+ """Output a message to the user via the userinterface.
+
+ Works like print, but uses the encoding used by the user's console
+ (console_encoding in the configuration file) instead of ASCII.
+ If decoder is None, text should be a unicode string. Otherwise it
+ should be encoded in the given encoding.
+
+ If newline is True, a linebreak will be added after printing the text.
+
+ If toStdout is True, the text will be sent to standard output,
+ so that it can be piped to another process. All other text will
+ be sent to stderr. See: http://en.wikipedia.org/wiki/Pipeline_%28Unix%29
+
+ text can contain special sequences to create colored output. These
+ consist of the escape character \03 and the color name in curly braces,
+ e. g. \03{lightpurple}. \03{default} resets the color.
+
+ """
+ _fmtoutput(text, decoder, newline, toStdout, INFO)
+
+def warning(text, decoder=None, newline=True, toStdout=False):
+ _fmtoutput(text, decoder, newline, toStdout, WARNING)
+
+def error(text, decoder=None, newline=True, toStdout=False):
+ _fmtoutput(text, decoder, newline, toStdout, ERROR)
+
+def log(text, decoder=None, newline=True, toStdout=False):
+ _fmtoutput(text, decoder, newline, toStdout, VERBOSE)
+
+def debug(text, layer, decoder=None, newline=True, toStdout=False):
+ _fmtoutput(text, decoder, newline, toStdout, DEBUG, layer)
+
+
+# User input functions
+
def input(question, password=False):
"""Ask the user a question, return the user's answer.
@@ -168,8 +211,7 @@
"""
# make sure logging system has been initialized
- root = logging.getLogger("pywiki")
- if root.level == 30: # init_handlers sets this level
+ if not _handlers_initialized:
init_handlers(strm=ui.output_stream)
data = ui.input(question, password)
@@ -194,14 +236,15 @@
"""
# make sure logging system has been initialized
- root = logging.getLogger("pywiki")
- if root.level == 30: # init_handlers sets this level
+ if not _handlers_initialized:
init_handlers(strm=ui.output_stream)
data = ui.inputChoice(question, answers, hotkeys, default).lower()
return data
+_handlers_initialized = False
+
def init_handlers(strm=None):
"""Initialize logging system for terminal-based bots.
@@ -212,6 +255,8 @@
was defined, otherwise uses sys.stderr
"""
+ # Note: this function is called by handleArgs(), so it should normally
+ # not need to be called explicitly
# All user output is routed through the logging module.
# Each type of output is handled by an appropriate handler object.
@@ -230,6 +275,8 @@
# Accordingly, do ''not'' use print statements in bot code; instead,
# use pywikibot.output function.
+ global _handlers_initialized
+
global _stream
if strm:
_stream = strm
@@ -254,7 +301,7 @@
root_logger.setLevel(DEBUG+1) # all records except DEBUG go to logger
root_logger.handlers = [] # remove any old handlers
- # configure default handler for VERBOSE and INFO levels
+ # configure default handler for display to user interface
default_handler = ui.OutputHandlerClass(strm=_stream)
if config.verbose_output:
default_handler.setLevel(VERBOSE)
@@ -305,6 +352,7 @@
LoggingFormatter(fmt="%(levelname)s: %(message)s"))
root_logger.addHandler(warning_handler)
+ _handlers_initialized = True
# Command line parsing and help
@@ -387,6 +435,31 @@
elif arg == '-nolog':
if moduleName in config.log:
config.log.remove(moduleName)
+ #
+ # DEBUG control:
+ #
+ # The framework has four layers (by default, others can be added),
+ # each designated by a string --
+ #
+ # 1. "comm": the communication layer (http requests, etc.)
+ # 2. "data": the raw data layer (API requests, XML dump parsing)
+ # 3. "wiki": the wiki content representation layer (Page and Site
+ # objects)
+ # 4. "bot": the application layer (user scripts should always
+ # send any debug() messages to this layer)
+ #
+ # The "-debug:layer" flag sets the logger for any specified
+ # layer to the DEBUG level, causing it to output extensive debugging
+ # information. Otherwise, the default logging setting is the INFO
+ # level. "-debug" with no layer specified sets _all_ loggers to
+ # DEBUG level.
+ #
+ # This method does not check the 'layer' part of the flag for
+ # validity.
+ #
+ # If used, "-debug" turns on file logging, regardless of any
+ # other settings.
+ #
elif arg == "-debug":
if moduleName not in config.log:
config.log.append(moduleName)
@@ -426,7 +499,7 @@
if do_help:
showHelp()
sys.exit(0)
- pywikibot.output(u"handleArgs() completed.", level=pywikibot.DEBUG)
+ pywikibot.debug(u"handleArgs() completed.", _logger)
return nonGlobalArgs
@@ -485,13 +558,13 @@
if hasattr(module, 'docuReplacements'):
for key, value in module.docuReplacements.iteritems():
helpText = helpText.replace(key, value.strip('\n\r'))
- pywikibot.output(helpText, level=pywikibot.STDOUT) # output to STDOUT
+ pywikibot.output(helpText, toStdout=True) # output to STDOUT
except Exception:
if modname:
pywikibot.output(u'Sorry, no help available for %s' % modname,
- level=pywikibot.STDOUT)
+ toStdout=True)
logger.exception('showHelp:')
- pywikibot.output(globalHelp, level=pywikibot.STDOUT)
+ pywikibot.output(globalHelp, toStdout=True)
class Bot(object):
"""
@@ -526,9 +599,8 @@
self.options[opt] = kwargs[opt]
for opt in receivedOptions - validOptions:
- pywikibot.output(u'%s is not a valid option. It was ignored\n'
- % opt,
- level=pywikibot.WARNING)
+ pywikibot.warning(u'%s is not a valid option. It was ignored.'
+ % opt)
def getOption(self, option):
"""
Modified: branches/rewrite/pywikibot/comms/http.py
===================================================================
--- branches/rewrite/pywikibot/comms/http.py 2010-03-18 16:52:31 UTC (rev 8027)
+++ branches/rewrite/pywikibot/comms/http.py 2010-03-18 17:29:14 UTC (rev 8028)
@@ -33,7 +33,7 @@
import cookielib
import threadedhttp
-logger = logging.getLogger("pywiki.comms.http")
+_logger = "comm.http"
# global variables
@@ -50,16 +50,13 @@
try:
cookie_jar.load()
except (IOError, cookielib.LoadError):
- pywikibot.output(u"Loading cookies failed.",
- level=pywikibot.DEBUG)
+ pywikibot.debug(u"Loading cookies failed.", _logger)
else:
- pywikibot.output(u"Loaded cookies from file.",
- level=pywikibot.DEBUG)
+ pywikibot.debug(u"Loaded cookies from file.", _logger)
# Build up HttpProcessors
-pywikibot.output('Starting %(numthreads)i threads...' % locals(),
- level=pywikibot.VERBOSE)
+pywikibot.log('Starting %(numthreads)i threads...' % locals())
for i in range(numthreads):
proc = threadedhttp.HttpProcessor(http_queue, cookie_jar, connection_pool)
proc.setDaemon(True)
@@ -70,12 +67,10 @@
def _flush():
for i in threads:
http_queue.put(None)
- pywikibot.output(u'Waiting for threads to finish... ',
- level=pywikibot.VERBOSE)
+ pywikibot.log(u'Waiting for threads to finish... ')
for i in threads:
i.join()
- pywikibot.output(u"All threads finished.",
- level=pywikibot.VERBOSE)
+ pywikibot.log(u"All threads finished.")
atexit.register(_flush)
# export cookie_jar to global namespace
@@ -91,7 +86,7 @@
@param uri: the URI to retrieve (relative to the site's scriptpath)
@param ssl: Use https connection
@return: The received data (a unicode string).
-
+
"""
if ssl:
proto = "https"
@@ -101,7 +96,7 @@
proto = site.protocol()
host = site.hostname()
baseuri = urlparse.urljoin("%(proto)s://%(host)s" % locals(), uri)
-
+
# set default user-agent string
kwargs.setdefault("headers", {})
kwargs["headers"].setdefault("user-agent", useragent)
@@ -118,8 +113,7 @@
raise Server504Error("Server %s timed out" % site.hostname())
if request.data[0].status != 200:
- pywikibot.output(u"Http response status %(status)s"
- % {'status': request.data[0].status},
- level=pywikibot.WARNING)
+ pywikibot.warning(u"Http response status %(status)s"
+ % {'status': request.data[0].status})
- return request.data[1]
+ return request.data[1]
Modified: branches/rewrite/pywikibot/comms/threadedhttp.py
===================================================================
--- branches/rewrite/pywikibot/comms/threadedhttp.py 2010-03-18 16:52:31 UTC (rev 8027)
+++ branches/rewrite/pywikibot/comms/threadedhttp.py 2010-03-18 17:29:14 UTC (rev 8028)
@@ -12,7 +12,7 @@
# (C) 2007 Pywikipedia bot team, 2007
# (C) 2006 Httplib 2 team, 2006
# (C) 2007 Metaweb Technologies, Inc.
-#
+#
# Partially distributed under the MIT license
# Partially distributed under Metaweb Technologies, Incs license
# which is compatible with the MIT license
@@ -33,16 +33,16 @@
import pywikibot
from pywikibot import config
-logger = logging.getLogger("pywiki.comms.threadedhttp")
+_logger = "comm.threadedhttp"
# easy_install safeguarded dependencies
try:
import pkg_resources
except ImportError:
- pywikibot.output(
+ pywikibot.error(
u"Error: You need the python module setuptools to use this module",
- level=pywikibot.CRITICAL)
+ _level=pywikibot.CRITICAL)
sys.exit(1)
try:
import httplib2
@@ -58,8 +58,7 @@
The pool drops excessive connections added.
"""
- pywikibot.output(u"Creating connection pool.",
- level=pywikibot.DEBUG)
+ pywikibot.debug(u"Creating connection pool.", _logger)
self.connections = {}
self.lock = threading.Lock()
self.maxnum = maxnum
@@ -68,9 +67,9 @@
"""Destructor to close all connections in the pool."""
self.lock.acquire()
try:
- pywikibot.output(u"Closing connection pool (%s connections)"
+ pywikibot.debug(u"Closing connection pool (%s connections)"
% len(self.connections),
- level=pywikibot.DEBUG)
+ _logger)
for key in self.connections:
for connection in self.connections[key]:
connection.close()
@@ -93,9 +92,9 @@
try:
if identifier in self.connections:
if len(self.connections[identifier]) > 0:
- pywikibot.output(u"Retrieved connection from '%s' pool."
+ pywikibot.debug(u"Retrieved connection from '%s' pool."
% identifier,
- level=pywikibot.DEBUG)
+ _logger)
return self.connections[identifier].pop()
return None
finally:
@@ -114,9 +113,9 @@
self.connections[identifier] = []
if len(self.connections[identifier]) == self.maxnum:
- pywikibot.output(u"closing %s connection %r"
+ pywikibot.debug(u"closing %s connection %r"
% (identifier, connection),
- level=pywikibot.DEBUG)
+ _logger)
connection.close()
del connection
else:
@@ -179,7 +178,7 @@
@return: (response, content) tuple
- """
+ """
if max_redirects is None:
max_redirects = self.max_redirects
if headers is None:
@@ -210,11 +209,11 @@
# Redirect hack: we want to regulate redirects
follow_redirects = self.follow_redirects
self.follow_redirects = False
- pywikibot.output(u"%r" % (
+ pywikibot.debug(u"%r" % (
(uri.replace("%7C","|"), method, body,
headers, max_redirects,
connection_type),),
- level=pywikibot.DEBUG)
+ _logger)
try:
(response, content) = httplib2.Http.request(
self, uri, method, body, headers,
@@ -229,9 +228,9 @@
self.connections[conn_key])
del self.connections[conn_key]
- # First write cookies
+ # First write cookies
self.cookiejar.lock.acquire()
- try:
+ try:
self.cookiejar.extract_cookies(DummyResponse(response), req)
finally:
self.cookiejar.lock.release()
@@ -270,13 +269,13 @@
location)
if authority == None:
response['location'] = httplib2.urlparse.urljoin(uri, location)
- pywikibot.output(u"Relative redirect: changed [%s] to [%s]"
+ pywikibot.debug(u"Relative redirect: changed [%s] to [%s]"
% (location, response['location']),
- level=pywikibot.DEBUG)
+ _logger)
if response.status == 301 and method in ["GET", "HEAD"]:
response['-x-permanent-redirect-url'] = response['location']
if "content-location" not in response:
- response['content-location'] = absolute_uri
+ response['content-location'] = absolute_uri
httplib2._updateCache(headers, response, content, self.cache,
cachekey)
@@ -337,13 +336,11 @@
def run(self):
# The Queue item is expected to either an HttpRequest object
# or None (to shut down the thread)
- pywikibot.output(u"Thread started, waiting for requests.",
- level=pywikibot.DEBUG)
+ pywikibot.debug(u"Thread started, waiting for requests.", _logger)
while (True):
item = self.queue.get()
if item is None:
- pywikibot.output(u"Shutting down thread.",
- level=pywikibot.DEBUG)
+ pywikibot.debug(u"Shutting down thread.", _logger)
return
try:
item.data = self.http.request(*item.args, **item.kwargs)
@@ -368,7 +365,7 @@
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
- #
+ #
# THIS SOFTWARE IS PROVIDED BY METAWEB TECHNOLOGIES AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
Modified: branches/rewrite/pywikibot/data/api.py
===================================================================
--- branches/rewrite/pywikibot/data/api.py 2010-03-18 16:52:31 UTC (rev 8027)
+++ branches/rewrite/pywikibot/data/api.py 2010-03-18 17:29:14 UTC (rev 8028)
@@ -27,13 +27,15 @@
from pywikibot import config, login
from pywikibot.exceptions import *
-logger = logging.getLogger("pywiki.data.api")
+_logger = "data.api"
lagpattern = re.compile(r"Waiting for [\d.]+: (?P<lag>\d+) seconds? lagged")
_modules = {} # cache for retrieved API parameter information
-
+##logger = logging.getLogger("pywiki.data.api")
+##print "level =", logger.getEffectiveLevel()
+##
class APIError(pywikibot.Error):
"""The wiki site returned an error message."""
def __init__(self, code, info, **kwargs):
@@ -129,8 +131,7 @@
"import", "userrights", "upload"
)
if self.write:
- pywikibot.output(u"Adding user assertion",
- level=pywikibot.DEBUG)
+ pywikibot.debug(u"Adding user assertion", _logger)
self.params["assert"] = "user" # make sure user is logged in
# implement dict interface
@@ -269,22 +270,18 @@
'application/x-www-form-urlencoded'},
body=paramstring)
except Server504Error:
- pywikibot.output(u"Caught 504 error",
- level=pywikibot.DEBUG)
+ pywikibot.debug(u"Caught 504 error", _logger)
raise
#TODO: what other exceptions can occur here?
except Exception, e:
# for any other error on the http request, wait and retry
- pywikibot.output(traceback.format_exc(),
- level=pywikibot.ERROR)
- pywikibot.output(u"%s, %s" % (uri, paramstring),
- level=pywikibot.VERBOSE)
+ pywikibot.error(traceback.format_exc())
+ pywikibot.log(u"%s, %s" % (uri, paramstring))
self.wait()
continue
if not isinstance(rawdata, unicode):
rawdata = rawdata.decode(self.site.encoding())
- pywikibot.output(u"API response received:\n" + rawdata,
- level=pywikibot.DEBUG)
+ pywikibot.debug(u"API response received:\n" + rawdata, _logger)
if rawdata.startswith(u"unknown_action"):
raise APIError(rawdata[:14], rawdata[16:])
try:
@@ -292,12 +289,10 @@
except ValueError:
# if the result isn't valid JSON, there must be a server
# problem. Wait a few seconds and try again
- pywikibot.output(
+ pywikibot.warning(
"Non-JSON response received from server %s; the server may be down."
- % self.site,
- level=pywikibot.WARNING)
- pywikibot.output(rawdata,
- level=pywikibot.DEBUG)
+ % self.site)
+ pywikibot.debug(rawdata, _logger)
self.wait()
continue
if not result:
@@ -331,10 +326,9 @@
if "warnings" in result:
modules = [k for k in result["warnings"] if k != "info"]
for mod in modules:
- pywikibot.output(
+ pywikibot.warning(
u"API warning (%s): %s"
- % (mod, result["warnings"][mod]["*"]),
- level=pywikibot.WARNING)
+ % (mod, result["warnings"][mod]["*"]))
if "error" not in result:
return result
if "*" in result["error"]:
@@ -345,9 +339,8 @@
if code == "maxlag":
lag = lagpattern.search(info)
if lag:
- pywikibot.output(
- u"Pausing due to database lag: " + info,
- level=pywikibot.VERBOSE)
+ pywikibot.log(
+ u"Pausing due to database lag: " + info)
self.site.throttle.lag(int(lag.group("lag")))
continue
if code in (u'internal_api_error_DBConnectionError', ):
@@ -364,9 +357,8 @@
self.max_retries -= 1
if self.max_retries < 0:
raise TimeoutError("Maximum retries attempted without success.")
- pywikibot.output(u"Waiting %s seconds before retrying."
- % self.retry_wait,
- level=pywikibot.WARNING)
+ pywikibot.warning(u"Waiting %s seconds before retrying."
+ % self.retry_wait)
time.sleep(self.retry_wait)
# double the next wait, but do not exceed 120 seconds
self.retry_wait = min(120, self.retry_wait * 2)
@@ -488,10 +480,10 @@
self.query_limit = int(param["max"])
if self.prefix is None:
self.prefix = _modules[mod]["prefix"]
- pywikibot.output(u"%s: Set query_limit to %i."
+ pywikibot.debug(u"%s: Set query_limit to %i."
% (self.__class__.__name__,
self.query_limit),
- level=pywikibot.DEBUG)
+ _logger)
return
def set_namespace(self, namespaces):
@@ -534,39 +526,37 @@
old_limit = self.query_limit
if old_limit is None or old_limit < 2:
raise
- pywikibot.output("Setting query limit to %s" % (old_limit // 2),
- level=pywikibot.VERBOSE)
+ pywikibot.log("Setting query limit to %s" % (old_limit // 2))
self.set_query_increment(old_limit // 2)
continue
if not self.data or not isinstance(self.data, dict):
- pywikibot.output(
+ pywikibot.debug(
u"%s: stopped iteration because no dict retrieved from api."
% self.__class__.__name__,
- level=pywikibot.DEBUG)
+ _logger)
return
if not ("query" in self.data
and self.resultkey in self.data["query"]):
- pywikibot.output(
+ pywikibot.debug(
u"%s: stopped iteration because 'query' and '%s' not found in api response."
% (self.__class__.__name__, self.resultkey),
- level=pywikibot.DEBUG)
- pywikibot.output(unicode(self.data),
- level=pywikibot.DEBUG)
+ _logger)
+ pywikibot.debug(unicode(self.data), _logger)
return
resultdata = self.data["query"][self.resultkey]
if isinstance(resultdata, dict):
- pywikibot.output(u"%s received %s; limit=%s"
- % (self.__class__.__name__,
- resultdata.keys(),
- self.limit),
- level=pywikibot.DEBUG)
+ pywikibot.debug(u"%s received %s; limit=%s"
+ % (self.__class__.__name__,
+ resultdata.keys(),
+ self.limit),
+ _logger)
resultdata = [resultdata[k] for k in sorted(resultdata.keys())]
else:
- pywikibot.output(u"%s received %s; limit=%s"
- % (self.__class__.__name__,
- resultdata,
- self.limit),
- level=pywikibot.DEBUG)
+ pywikibot.debug(u"%s received %s; limit=%s"
+ % (self.__class__.__name__,
+ resultdata,
+ self.limit),
+ _logger)
if "normalized" in self.data["query"]:
self.normalized = dict((item['to'], item['from'])
for item in
@@ -582,10 +572,9 @@
if not "query-continue" in self.data:
return
if not self.continuekey in self.data["query-continue"]:
- pywikibot.output(
+ pywikibot.log(
u"Missing '%s' key in ['query-continue'] value."
- % self.continuekey,
- level=pywikibot.VERBOSE)
+ % self.continuekey)
return
update = self.data["query-continue"][self.continuekey]
for key, value in update.iteritems():
@@ -744,9 +733,8 @@
if hasattr(self, '_waituntil'):
if datetime.now() < self._waituntil:
diff = self._waituntil - datetime.now()
- pywikibot.output(u"Too many tries, waiting %s seconds before retrying."
- % diff.seconds,
- level=pywikibot.WARNING)
+ pywikibot.warning(u"Too many tries, waiting %s seconds before retrying."
+ % diff.seconds)
time.sleep(diff.seconds)
login_request = Request(site=self.site,
action="login",
@@ -838,8 +826,8 @@
if __name__ == "__main__":
- from pywikibot import Site
- logger.setLevel(pywikibot.logging.DEBUG)
+ from pywikibot import Site, logging
+ logging.getLogger("pywiki.data.api").setLevel(logging.DEBUG)
mysite = Site("en", "wikipedia")
pywikibot.output(u"starting test....")
def _test():
Modified: branches/rewrite/pywikibot/data/logentries.py
===================================================================
--- branches/rewrite/pywikibot/data/logentries.py 2010-03-18 16:52:31 UTC (rev 8027)
+++ branches/rewrite/pywikibot/data/logentries.py 2010-03-18 17:29:14 UTC (rev 8028)
@@ -11,21 +11,23 @@
from pywikibot.exceptions import *
import pywikibot
-
+
+_logger = "wiki"
+
class LogDict(dict):
"""
Simple custom dictionary that raises a custom KeyError and logs
debugging information when a key is missing
"""
def __missing__(self, key):
- pywikibot.output(u"API log entry received:\n" + repr(self),
- level=pywikibot.DEBUG)
+ pywikibot.debug(u"API log entry received:\n" + repr(self),
+ _logger)
raise KeyError("Log entry has no '%s' key" % key, key)
class LogEntry(object):
"""Generic log entry"""
- # Log type expected. None for every type, or one of the (letype) str :
+ # Log type expected. None for every type, or one of the (letype) str :
# block/patrol/etc...
# Overriden in subclasses.
_expectedType = None
@@ -36,13 +38,13 @@
if self._expectedType is not None and self._expectedType != self.type():
raise Error("Wrong log type! Expecting %s, received %s instead." \
% (self._expectedType, self.type()))
-
+
def __hash__(self):
return self.logid()
def logid(self):
return self.data['logid']
-
+
def pageid(self):
return self.data['pageid']
@@ -54,7 +56,7 @@
if not hasattr(self, '_title'):
self._title = pywikibot.Page(pywikibot.Link(self.data['title']))
return self._title
-
+
def type(self):
return self.data['type']
@@ -89,7 +91,7 @@
def title(self):
"""
- * Returns the Page object of username or IP
+ * Returns the Page object of username or IP
if this block action targets a username or IP.
* Returns the blockid if this log reflects the removal of an autoblock
"""
@@ -101,7 +103,7 @@
def isAutoblockRemoval(self):
return self.isAutoblockRemoval
-
+
def _getBlockDetails(self):
try:
return self.data['block']
@@ -174,13 +176,13 @@
def suppressedredirect(self):
"""
- Returns True if no redirect was created from the old title
+ Returns True if no redirect was created from the old title
to the new title during the move
"""
# Introduced in MW r47901
return 'suppressedredirect' in self.data['move']
-
+
class ImportEntry(LogEntry):
_expectedType = 'import'
@@ -251,9 +253,9 @@
Checks for logtype from data, and creates the correct LogEntry
"""
try:
- logtype = logdata['type']
+ logtype = logdata['type']
return LogEntryFactory._getEntryClass(logtype)(logdata)
except KeyError:
- pywikibot.output(u"API log entry received:\n" + logdata,
- level=pywikibot.DEBUG)
+ pywikibot.debug(u"API log entry received:\n" + logdata,
+ _logger)
raise Error("Log entry has no 'type' key")
Modified: branches/rewrite/pywikibot/login.py
===================================================================
--- branches/rewrite/pywikibot/login.py 2010-03-18 16:52:31 UTC (rev 8027)
+++ branches/rewrite/pywikibot/login.py 2010-03-18 17:29:14 UTC (rev 8028)
@@ -51,7 +51,7 @@
from pywikibot import config
from pywikibot.exceptions import NoSuchSite, NoUsername
-logger = logging.getLogger("pywiki.wiki.login")
+logger = "wiki.login"
# On some wikis you are only allowed to run a bot if there is a link to
@@ -143,8 +143,8 @@
"""
# THIS IS OVERRIDDEN IN data/api.py
filename = config.datafilepath('pywikibot.lwp')
- pywikibot.output(u"Storing cookies to %s" % filename,
- level=pywikibot.DEBUG)
+ pywikibot.debug(u"Storing cookies to %s" % filename,
+ _logger)
f = open(filename, 'w')
f.write(data)
f.close()
@@ -195,15 +195,14 @@
try:
cookiedata = self.getCookie()
except pywikibot.data.api.APIError, e:
- pywikibot.output(u"Login failed (%s)." % e.code,
- level=pywikibot.ERROR)
+ pywikibot.error(u"Login failed (%s)." % e.code)
if retry:
self.password = None
return self.login(retry = True)
else:
return False
self.storecookiedata(cookiedata)
- pywikibot.output(u"Should be logged in now", level=pywikibot.VERBOSE)
+ pywikibot.log(u"Should be logged in now")
## # Show a warning according to the local bot policy
## FIXME: disabled due to recursion; need to move this to the Site object after
## login
Modified: branches/rewrite/pywikibot/page.py
===================================================================
--- branches/rewrite/pywikibot/page.py 2010-03-18 16:52:31 UTC (rev 8027)
+++ branches/rewrite/pywikibot/page.py 2010-03-18 17:29:14 UTC (rev 8028)
@@ -454,10 +454,9 @@
+ ":" + args[0].strip()
break
except IndexError:
- pywikibot.output(
+ pywikibot.warning(
u"No target for category redirect on %s"
- % self.title(asLink=True),
- level=pywikibot.WARNING)
+ % self.title(asLink=True))
self._catredirect = False
break
else:
@@ -735,8 +734,7 @@
done = self.site().editpage(self, summary=comment, minor=minor,
watch=watch, unwatch=unwatch)
if not done:
- pywikibot.output(u"Page %s not saved" % link,
- level=pywikibot.WARNING)
+ pywikibot.warning(u"Page %s not saved" % link)
raise pywikibot.PageNotSaved(link)
else:
pywikibot.output(u"Page %s saved" % link)
@@ -1707,9 +1705,8 @@
catname = self.site().category_namespace() + ':' + catname
targetCat = Category(self.site(), catname)
if targetCat.exists():
- pywikibot.output(u'Target page %s already exists!'
- % targetCat.title(),
- level=pywikibot.WARNING)
+ pywikibot.warning(u'Target page %s already exists!'
+ % targetCat.title())
return False
else:
pywikibot.output('Moving text from %s to %s.'
Modified: branches/rewrite/pywikibot/pagegenerators.py
===================================================================
--- branches/rewrite/pywikibot/pagegenerators.py 2010-03-18 16:52:31 UTC (rev 8027)
+++ branches/rewrite/pywikibot/pagegenerators.py 2010-03-18 17:29:14 UTC (rev 8028)
@@ -1071,7 +1071,7 @@
gen = genFactory.getCombinedGenerator()
if gen:
for page in gen:
- pywikibot.output(page.title(), level=pywikibot.STDOUT)
+ pywikibot.output(page.title(), toStdout=True)
else:
pywikibot.showHelp()
except Exception:
Modified: branches/rewrite/pywikibot/site.py
===================================================================
--- branches/rewrite/pywikibot/site.py 2010-03-18 16:52:31 UTC (rev 8027)
+++ branches/rewrite/pywikibot/site.py 2010-03-18 17:29:14 UTC (rev 8028)
@@ -23,7 +23,6 @@
from hashlib import md5
except ImportError:
from md5 import md5
-import logging
import os
import re
import sys
@@ -31,7 +30,7 @@
import time
import urllib
-logger = logging.getLogger("pywiki.wiki.site")
+_logger = "wiki.site"
class PageInUse(pywikibot.Error):
"""Page cannot be reserved for writing due to existing lock."""
@@ -758,9 +757,9 @@
def isBlocked(self, sysop=False):
"""Deprecated synonym for is_blocked"""
- pywikibot.output(
+ pywikibot.debug(
u"Site method 'isBlocked' should be changed to 'is_blocked'",
- level=pywikibot.DEBUG)
+ _logger)
return self.is_blocked(sysop)
def checkBlocks(self, sysop = False):
@@ -1025,15 +1024,13 @@
# page title was normalized by api
# this should never happen because the Link() constructor
# normalizes the title
- pywikibot.output(
+ pywikibot.log(
u"loadpageinfo: Page title '%s' was normalized to '%s'"
- % (title, pageitem['title']),
- level=pywikibot.ERROR)
+ % (title, pageitem['title']))
else:
- pywikibot.output(
+ pywikibot.warning(
u"loadpageinfo: Query on %s returned data on '%s'"
- % (page, pageitem['title']),
- level=pywikibot.WARNING)
+ % (page, pageitem['title']))
continue
api.update_page(page, pageitem)
@@ -1174,23 +1171,17 @@
% (len(cache), self)
)
for pagedata in rvgen:
- pywikibot.output(u"Preloading %s" % pagedata,
- level=pywikibot.DEBUG)
+ pywikibot.debug(u"Preloading %s" % pagedata, _logger)
try:
if pagedata['title'] not in cache:
- pywikibot.output(
+ pywikibot.warning(
u"preloadpages: Query returned unexpected title '%s'"
- % pagedata['title'],
- level=pywikibot.WARNING
- )
+ % pagedata['title'])
continue
except KeyError:
- pywikibot.output(u"No 'title' in %s" % pagedata,
- level=pywikibot.DEBUG)
- pywikibot.output(u"pageids=%s" % pageids,
- level=pywikibot.DEBUG)
- pywikibot.output(u"titles=%s" % cache.keys(),
- level=pywikibot.DEBUG)
+ pywikibot.debug(u"No 'title' in %s" % pagedata, _logger)
+ pywikibot.debug(u"pageids=%s" % pageids, _logger)
+ pywikibot.debug(u"titles=%s" % cache.keys(), _logger)
continue
page = cache[pagedata['title']]
api.update_page(page, pagedata)
@@ -1215,8 +1206,7 @@
% (page.title(withSection=False, asLink=True),
item['title']))
api.update_page(page, item)
- pywikibot.output(unicode(item),
- level=pywikibot.DEBUG)
+ pywikibot.debug(unicode(item), _logger)
return item[tokentype + "token"]
# following group of methods map more-or-less directly to API queries
@@ -1621,9 +1611,9 @@
if not isinstance(namespace, int):
raise Error("allpages: only one namespace permitted.")
if includeredirects is not None:
- pywikibot.output(
+ pywikibot.debug(
u"allpages: 'includeRedirects' argument is deprecated; use 'filterredirs'.",
- level=pywikibot.DEBUG)
+ _logger)
if includeredirects:
if includeredirects == "only":
filterredirs = True
@@ -1964,9 +1954,8 @@
rcgen.request["rcdir"] = "newer"
if pagelist:
if self.versionnumber() > 14:
- pywikibot.output(
- u"recentchanges: pagelist option is disabled",
- level=pywikibot.ERROR)
+ pywikibot.warning(
+ u"recentchanges: pagelist option is disabled; ignoring.")
else:
rcgen.request["rctitles"] = u"|".join(p.title(withSection=False)
for p in pagelist)
@@ -2007,8 +1996,7 @@
if where not in ("text", "titles"):
raise Error("search: unrecognized 'where' value: %s" % where)
if not namespaces:
- pywikibot.output(u"search: namespaces cannot be empty; using [0].",
- level=pywikibot.WARNING)
+ pywikibot.warning(u"search: namespaces cannot be empty; using [0].")
namespaces = [0]
srgen = self._generator(api.PageGenerator, type_arg="search",
gsrsearch=searchstring, gsrwhat=where,
@@ -2308,14 +2296,14 @@
while True:
try:
result = req.submit()
- pywikibot.output(u"editpage response: %s" % result,
- level=pywikibot.DEBUG)
+ pywikibot.debug(u"editpage response: %s" % result,
+ _logger)
except api.APIError, err:
self.unlock_page(page)
if err.code.endswith("anon") and self.logged_in():
- pywikibot.output(
+ pywikibot.debug(
u"editpage: received '%s' even though bot is logged in" % err.code,
- level=pywikibot.DEBUG)
+ _logger)
errdata = {
'site': self,
'title': page.title(withSection=False),
@@ -2332,10 +2320,10 @@
raise LockedPage(errdata['title'])
if err.code in self._ep_errors:
raise Error(self._ep_errors[err.code] % errdata)
- pywikibot.output(
+ pywikibot.debug(
u"editpage: Unexpected error code '%s' received."
% err.code,
- level=pywikibot.DEBUG)
+ _logger)
raise
assert ("edit" in result and "result" in result["edit"]), result
if result["edit"]["result"] == "Success":
@@ -2363,24 +2351,21 @@
continue
else:
self.unlock_page(page)
- pywikibot.output(
+ pywikibot.error(
u"editpage: unknown CAPTCHA response %s, page not saved"
- % captcha,
- level=pywikibot.ERROR)
+ % captcha)
return False
else:
self.unlock_page(page)
- pywikibot.output(u"editpage: unknown failure reason %s"
- % str(result),
- level=pywikibot.ERROR)
+ pywikibot.error(u"editpage: unknown failure reason %s"
+ % str(result))
return False
else:
self.unlock_page(page)
- pywikibot.output(
+ pywikibot.error(
u"editpage: Unknown result code '%s' received; page not saved"
- % result["edit"]["result"],
- level=pywikibot.ERROR)
- pywikibot.output(str(result), level=pywikibot.VERBOSE)
+ % result["edit"]["result"])
+ pywikibot.log(str(result))
return False
# catalog of move errors for use in error messages
@@ -2446,13 +2431,13 @@
req['noredirect'] = ""
try:
result = req.submit()
- pywikibot.output(u"movepage response: %s" % result,
- level=pywikibot.DEBUG)
+ pywikibot.debug(u"movepage response: %s" % result,
+ _logger)
except api.APIError, err:
if err.code.endswith("anon") and self.logged_in():
- pywikibot.output(
+ pywikibot.debug(
u"movepage: received '%s' even though bot is logged in" % err.code,
- level=pywikibot.DEBUG)
+ _logger)
errdata = {
'site': self,
'oldtitle': oldtitle,
@@ -2463,20 +2448,19 @@
}
if err.code in self._mv_errors:
raise Error(self._mv_errors[err.code] % errdata)
- pywikibot.output(u"movepage: Unexpected error code '%s' received."
+ pywikibot.debug(u"movepage: Unexpected error code '%s' received."
% err.code,
- level=pywikibot.DEBUG)
+ _logger)
raise
finally:
self.unlock_page(page)
if "move" not in result:
- pywikibot.output(u"movepage: %s" % result, level=pywikibot.ERROR)
+ pywikibot.error(u"movepage: %s" % result)
raise Error("movepage: unexpected response")
#TODO: Check for talkmove-error messages
if "talkmove-error-code" in result["move"]:
- pywikibot.output(u"movepage: Talk page %s not moved"
- % (page.toggleTalkPage().title(asLink=True)),
- level=pywikibot.WARNING)
+ pywikibot.warning(u"movepage: Talk page %s not moved"
+ % (page.toggleTalkPage().title(asLink=True)))
return pywikibot.Page(page, newtitle)
# catalog of rollback errors for use in error messages
@@ -2535,9 +2519,9 @@
}
if err.code in self._rb_errors:
raise Error(self._rb_errors[err.code] % errdata)
- pywikibot.output(u"rollback: Unexpected error code '%s' received."
- % err.code,
- level=pywikibot.DEBUG)
+ pywikibot.debug(u"rollback: Unexpected error code '%s' received."
+ % err.code,
+ _logger)
raise
finally:
self.unlock_page(page)
@@ -2583,9 +2567,9 @@
}
if err.code in self._dl_errors:
raise Error(self._dl_errors[err.code] % errdata)
- pywikibot.output(u"delete: Unexpected error code '%s' received."
- % err.code,
- level=pywikibot.DEBUG)
+ pywikibot.debug(u"delete: Unexpected error code '%s' received."
+ % err.code,
+ _logger)
raise
finally:
self.unlock_page(page)
@@ -2711,7 +2695,7 @@
# TODO: catch and process foreseeable errors
raise
result = result["upload"]
- pywikibot.output(result, level=pywikibot.DEBUG)
+ pywikibot.debug(result, _logger)
if "warnings" in result:
warning = result["warnings"].keys()[0]
message = result["warnings"][warning]
Modified: branches/rewrite/pywikibot/throttle.py
===================================================================
--- branches/rewrite/pywikibot/throttle.py 2010-03-18 16:52:31 UTC (rev 8027)
+++ branches/rewrite/pywikibot/throttle.py 2010-03-18 17:29:14 UTC (rev 8028)
@@ -17,7 +17,7 @@
import threading
import time
-logger = logging.getLogger("pywiki.wiki.throttle")
+_logger = "wiki.throttle"
pid = False # global process identifier
# when the first Throttle is instantiated, it will set this
@@ -70,8 +70,8 @@
global pid
self.lock.acquire()
mysite = self.mysite
- pywikibot.output(u"Checking multiplicity: pid = %(pid)s" % globals(),
- level=pywikibot.DEBUG)
+ pywikibot.debug(u"Checking multiplicity: pid = %(pid)s" % globals(),
+ _logger)
try:
processes = []
my_pid = pid or 1 # start at 1 if global pid not yet set
@@ -128,6 +128,10 @@
pywikibot.output(
u"Found %(count)s %(mysite)s processes running, including this one."
% locals())
+ else:
+ pywikibot.log(
+ u"Found %(count)s %(mysite)s processes running, including this one."
+ % locals())
finally:
self.lock.release()
@@ -250,6 +254,14 @@
'now' : time.strftime("%Y-%m-%d %H:%M:%S",
time.localtime())
} )
+ else:
+ pywikibot.log(
+ u"Sleeping for %(wait).1f seconds, %(now)s"
+ % {'wait': wait,
+ 'now' : time.strftime("%Y-%m-%d %H:%M:%S",
+ time.localtime())
+ } )
+
time.sleep(wait)
if write:
self.last_write = time.time()
@@ -280,6 +292,13 @@
'now': time.strftime("%Y-%m-%d %H:%M:%S",
time.localtime())
} )
+ else:
+ pywikibot.log(
+ u"Sleeping for %(wait).1f seconds, %(now)s"
+ % {'wait': wait,
+ 'now': time.strftime("%Y-%m-%d %H:%M:%S",
+ time.localtime())
+ } )
time.sleep(wait)
finally:
self.lock.release()
Modified: branches/rewrite/pywikibot/userinterfaces/terminal_interface.py
===================================================================
--- branches/rewrite/pywikibot/userinterfaces/terminal_interface.py 2010-03-18 16:52:31 UTC (rev 8027)
+++ branches/rewrite/pywikibot/userinterfaces/terminal_interface.py 2010-03-18 17:29:14 UTC (rev 8028)
@@ -120,15 +120,11 @@
self.OutputHandlerClass = TerminalHandler
self.output_stream = sys.stderr
- def output(self, text, level=logging.INFO, context=None):
- """
- If a character can't be displayed in the encoding used by the user's
- terminal, it will be replaced with a question mark or by a
- transliteration.
- """
+ def output(self, text, logger, level=logging.INFO, context=None):
+ """Send text to the logger for output to terminal."""
self.writelock.acquire()
try:
- logging.getLogger("pywiki").log(level, text, extra=context)
+ logger.log(level, text, extra=context)
finally:
self.writelock.release()
@@ -147,7 +143,8 @@
# While we're waiting for user input,
# we don't want terminal writes from other Threads
self.writelock.acquire()
- self.output(question + ' ', level=pywikibot.INPUT)
+ pywikibot.bot._fmtoutput(question + ' ', newline=False,
+ _level=pywikibot.INPUT)
try:
if password:
Modified: branches/rewrite/scripts/redirect.py
===================================================================
--- branches/rewrite/scripts/redirect.py 2010-03-18 16:52:31 UTC (rev 8027)
+++ branches/rewrite/scripts/redirect.py 2010-03-18 17:29:14 UTC (rev 8028)
@@ -774,8 +774,7 @@
pywikibot.output(u'Unknown argument: %s' % arg)
if xmlFilename:
- pywikibot.output("Sorry, xmlreader is not yet implemented in rewrite",
- level=pywikibot.ERROR)
+ pywikibot.error(u"Sorry, xmlreader is not yet implemented in rewrite")
elif not action: # or (xmlFilename and moved_pages)
# or (api and xmlFilename):
pywikibot.showHelp('redirect')
Modified: branches/rewrite/scripts/solve_disambiguation.py
===================================================================
--- branches/rewrite/scripts/solve_disambiguation.py 2010-03-18 16:52:31 UTC (rev 8027)
+++ branches/rewrite/scripts/solve_disambiguation.py 2010-03-18 17:29:14 UTC (rev 8028)
@@ -475,9 +475,8 @@
][self.disambPage.site().lang]:
for i in range(len(refs)-1, -1, -1):
if re.match(ig, refs[i].title()):
- pywikibot.output(u'Ignoring page %s'
- % refs[i].title(),
- level=pywikibot.VERBOSE)
+ pywikibot.log(u'Ignoring page %s'
+ % refs[i].title())
del refs[i]
elif self.primaryIgnoreManager.isIgnored(refs[i]):
del refs[i]
@@ -724,9 +723,8 @@
continue
except pywikibot.Error:
# must be a broken link
- pywikibot.output("Invalid link [[%s]] in page [[%s]]"
- % (m.group('title'), refPage.title()),
- level=pywikibot.VERBOSE)
+ pywikibot.log(u"Invalid link [[%s]] in page [[%s]]"
+ % (m.group('title'), refPage.title()))
continue
n += 1
# how many bytes should be displayed around the current link
Modified: branches/rewrite/scripts/touch.py
===================================================================
--- branches/rewrite/scripts/touch.py 2010-03-18 16:52:31 UTC (rev 8027)
+++ branches/rewrite/scripts/touch.py 2010-03-18 17:29:14 UTC (rev 8028)
@@ -46,20 +46,17 @@
text = page.get(get_redirect = self.touch_redirects)
page.save("Pywikibot touch script")
except pywikibot.NoPage:
- pywikibot.output(u"Page %s does not exist?!" % page.title(asLink=True),
- level=pywikibot.ERROR)
+ pywikibot.error(u"Page %s does not exist"
+ % page.title(asLink=True))
except pywikibot.IsRedirectPage:
- pywikibot.output(u"Page %s is a redirect; skipping."
- % page.title(asLink=True),
- level=pywikibot.WARNING)
+ pywikibot.warning(u"Page %s is a redirect; skipping."
+ % page.title(asLink=True))
except pywikibot.LockedPage:
- pywikibot.output(u"Page %s is locked?!"
- % page.title(asLink=True),
- level=pywikibot.ERROR)
+ pywikibot.error(u"Page %s is locked"
+ % page.title(asLink=True))
except pywikibot.PageNotSaved:
- pywikibot.output(u"Page %s not saved"
- % page.title(asLink=True),
- level=pywikibot.ERROR)
+ pywikibot.error(u"Page %s not saved"
+ % page.title(asLink=True))
def main(*args):
Modified: branches/rewrite/scripts/upload.py
===================================================================
--- branches/rewrite/scripts/upload.py 2010-03-18 16:52:31 UTC (rev 8027)
+++ branches/rewrite/scripts/upload.py 2010-03-18 17:29:14 UTC (rev 8028)
@@ -110,9 +110,8 @@
elif dt < 360:
dt += 60
else:
- pywikibot.output(
- u"WARNING: No check length to retrieved data is possible.",
- level=pywikibot.VERBOSE)
+ pywikibot.log(
+ u"WARNING: No check length to retrieved data is possible.")
handle, tempname = tempfile.mkstemp()
t = os.fdopen(handle, "wb")
t.write(_contents)
@@ -220,7 +219,7 @@
else:
pywikibot.output(u"Upload aborted.")
return
-
+
except Exception, e:
pywikibot.logger.exception("Upload error: " + str(e))
Revision: 8027
Author: xqt
Date: 2010-03-18 16:52:31 +0000 (Thu, 18 Mar 2010)
Log Message:
-----------
getall() bulk pages via api
Modified Paths:
--------------
trunk/pywikipedia/wikipedia.py
Modified: trunk/pywikipedia/wikipedia.py
===================================================================
--- trunk/pywikipedia/wikipedia.py 2010-03-18 15:02:49 UTC (rev 8026)
+++ trunk/pywikipedia/wikipedia.py 2010-03-18 16:52:31 UTC (rev 8027)
@@ -3664,7 +3664,7 @@
def run(self):
if self.pages:
# Sometimes query does not contains revisions
- if self.site.has_api() and debug:
+ if self.site.has_api():
while True:
try:
data = self.getDataApi()
@@ -4026,10 +4026,8 @@
"""
# TODO: why isn't this a Site method?
pages = list(pages) # if pages is an iterator, we need to make it a list
- output(u'Getting %d pages from %s' % (len(pages), site), newline=False)
- if site.has_api() and debug:
- output(u' via API', newline=False)
- output(u'...')
+ output(u'Getting %d pages %s from %s...'
+ % (len(pages), iif(site.has_api(), u'via API', u'') site))
limit = config.special_page_limit / 4 # default is 500/4, but It might have good point for server.
if len(pages) > limit:
# separate export pages for bulk-retrieve
@@ -4057,6 +4055,11 @@
global action
action = s
+def iif(q, a, b):
+ """inline if"""
+ if q: return a
+ else: return b
+
# Default action
setAction('Wikipedia python library')
Revision: 8026
Author: xqt
Date: 2010-03-18 15:02:49 +0000 (Thu, 18 Mar 2010)
Log Message:
-----------
dealing with error type for #2972249
Modified Paths:
--------------
trunk/pywikipedia/weblinkchecker.py
Modified: trunk/pywikipedia/weblinkchecker.py
===================================================================
--- trunk/pywikipedia/weblinkchecker.py 2010-03-18 14:33:58 UTC (rev 8025)
+++ trunk/pywikipedia/weblinkchecker.py 2010-03-18 15:02:49 UTC (rev 8026)
@@ -425,7 +425,7 @@
msg = error[1]
except IndexError:
print u'### DEBUG information for #2972249'
- raise IndexError, error
+ raise IndexError, type(error)
# TODO: decode msg. On Linux, it's encoded in UTF-8.
# How is it encoded in Windows? Or can we somehow just
# get the English message?
Revision: 8025
Author: xqt
Date: 2010-03-18 14:33:58 +0000 (Thu, 18 Mar 2010)
Log Message:
-----------
pl.archive text for weblinkchecker (patch #2972577 given by masti)
Modified Paths:
--------------
trunk/pywikipedia/weblinkchecker.py
Modified: trunk/pywikipedia/weblinkchecker.py
===================================================================
--- trunk/pywikipedia/weblinkchecker.py 2010-03-18 14:27:48 UTC (rev 8024)
+++ trunk/pywikipedia/weblinkchecker.py 2010-03-18 14:33:58 UTC (rev 8025)
@@ -179,6 +179,7 @@
'ja': u'\nウェブ・ページはインターネット・アーカイブによって保存されました。アーカイブに保管された適切なバージョンにリンクすることを検討してください: [%s]. ',
'nl': u'\nDeze website is bewaard in het Internet Archive. Overweeg te verwijzen naar een gearchiveerde pagina: [%s]. ',
'no': u'\nDenne nettsiden er lagra i Internet Archive. Vurder om lenka kan endres til å peke til en av de arkiverte versjonene: [%s]. ',
+ 'pl': u'\nStrona została zarchiwizowana przez Internet Archive. Możesz wykorzystać link archiwalny: [%s]. ',
'pt': u'Esta página web foi gravada na Internet Archive. Por favor considere o link para a versão arquivada: [%s]. ',
'zh': u'这个网页已经被保存在互联网档案馆(Internet Archive)。请为该网页提供一个合适的存档版本: [%s]。',
}