Revision: 4166
Author: russblau
Date: 2007-08-31 21:18:28 +0000 (Fri, 31 Aug 2007)
Log Message:
-----------
Add a callback hook to put_async, for any bots that need to record whether page was saved successfully.
Modified Paths:
--------------
trunk/pywikipedia/wikipedia.py
Modified: trunk/pywikipedia/wikipedia.py
===================================================================
--- trunk/pywikipedia/wikipedia.py 2007-08-31 19:30:56 UTC (rev 4165)
+++ trunk/pywikipedia/wikipedia.py 2007-08-31 21:18:28 UTC (rev 4166)
@@ -1028,9 +1028,17 @@
yield Page(site, fileLink)
def put_async(self, newtext,
- comment=None, watchArticle=None, minorEdit=True, force=False):
+ comment=None, watchArticle=None, minorEdit=True, force=False,
+ callback=None):
"""Asynchronous version of put (takes the same arguments), which
places pages on a queue to be saved by a daemon thread.
+ All arguments are the same as for .put(), except --
+ callback: a callable object that will be called after the page put
+ operation; this object must take two arguments:
+ (1) a Page object, and (2) an exception instance, which
+ will be None if the page was saved successfully.
+ The callback is intended to be used by bots that need to keep track
+ of which saves were successful.
"""
try:
page_put_queue.mutex.acquire()
@@ -1040,9 +1048,11 @@
pass
finally:
page_put_queue.mutex.release()
- page_put_queue.put((self, newtext, comment, watchArticle, minorEdit, force))
+ page_put_queue.put((self, newtext, comment, watchArticle, minorEdit,
+ force, callback))
- def put(self, newtext, comment=None, watchArticle = None, minorEdit = True, force=False):
+ def put(self, newtext, comment=None, watchArticle=None, minorEdit=True,
+ force=False):
"""Replace the new page with the contents of the first argument.
The second argument is a string that is to be used as the
summary for the modification
@@ -4732,25 +4742,34 @@
Daemon that takes pages from the queue and tries to save them on the wiki.
'''
while True:
- page, newtext, comment, watchArticle, minorEdit, force = page_put_queue.get()
+ (page, newtext, comment, watchArticle,
+ minorEdit, force, callback) = page_put_queue.get()
if page is None:
- # needed for compatibility with Python 2.3 and 2.4
- # in 2.5, we could use the Queue's task_done() and join() methods
+ # an explicit end-of-Queue marker is needed for compatibility
+ # with Python 2.4; in 2.5, we could use the Queue's task_done()
+ # and join() methods
return
try:
page.put(newtext, comment, watchArticle, minorEdit, force)
- except SpamfilterError, ex:
+ error = None
+ except Exception, error:
+ pass
+ if callback is not None:
+ callback(page, error)
+ # if callback is provided, it is responsible for exception handling
+ continue
+ if isinstance(error, SpamfilterError):
output(u"Saving page [[%s]] prevented by spam filter: %s"
- % (page.title(), ex.url))
- except PageNotSaved, ex:
+ % (page.title(), error.url))
+ elif isinstance(error, PageNotSaved):
output(u"Saving page [[%s]] failed: %s"
- % (page.title(), ex.message))
- except LockedPage, ex:
+ % (page.title(), error.message))
+ elif isinstance(error, LockedPage):
output(u"Page [[%s]] is locked; not saved." % page.title())
- except NoUsername, ex:
+ elif isinstance(error, NoUsername):
output(u"Page [[%s]] not saved; sysop privileges required."
- % page.title())
- except:
+ % page.title())
+ elif error is not None:
tb = traceback.format_exception(*sys.exc_info())
output(u"Saving page [[%s]] failed:\n%s"
% (page.title(), "".join(tb)))
Revision: 4165
Author: russblau
Date: 2007-08-31 19:30:56 +0000 (Fri, 31 Aug 2007)
Log Message:
-----------
Suppress spurious error messages from getall() when a
page appeared twice in the list, and clean up docstring;
also, catch NoUsername exceptions in put_async().
Modified Paths:
--------------
trunk/pywikipedia/wikipedia.py
Modified: trunk/pywikipedia/wikipedia.py
===================================================================
--- trunk/pywikipedia/wikipedia.py 2007-08-31 18:22:00 UTC (rev 4164)
+++ trunk/pywikipedia/wikipedia.py 2007-08-31 19:30:56 UTC (rev 4165)
@@ -2159,8 +2159,6 @@
class GetAll(object):
def __init__(self, site, pages, throttle, force):
- """First argument is Site object.
- Second argument is list (should have .append and be iterable)"""
self.site = site
self.pages = []
self.throttle = throttle
@@ -2172,7 +2170,7 @@
def run(self):
dt=15
- if self.pages != []:
+ if self.pages:
while True:
try:
data = self.getData()
@@ -2234,8 +2232,9 @@
page = Page(self.site, title)
for page2 in self.pages:
if page2.sectionFreeTitle() == page.sectionFreeTitle():
- if not hasattr(page2,'_contents') and not hasattr(page2,'_getexception'):
- break
+ if hasattr(page2,'_contents') or hasattr(page2,'_getexception'):
+ return
+ break
else:
output(u"BUG>> title %s (%s) not found in list" % (title, page.aslink(forceInterwiki=True)))
output(u'Expected one of: %s' % u','.join([page2.aslink(forceInterwiki=True) for page2 in self.pages]))
@@ -2335,7 +2334,11 @@
get_throttle.setDelay(time.time() - now)
return data
-def getall(site, pages, throttle = True, force = False):
+def getall(site, pages, throttle=True, force=False):
+ """Use Special:Export to bulk-retrieve a group of pages from site
+ Arguments: site = Site object
+ pages = iterable that yields Page objects
+ """
output(u'Getting %d pages from %s...' % (len(pages), site))
return GetAll(site, pages, throttle, force).run()
@@ -4744,6 +4747,9 @@
% (page.title(), ex.message))
except LockedPage, ex:
output(u"Page [[%s]] is locked; not saved." % page.title())
+ except NoUsername, ex:
+ output(u"Page [[%s]] not saved; sysop privileges required."
+ % page.title())
except:
tb = traceback.format_exception(*sys.exc_info())
output(u"Saving page [[%s]] failed:\n%s"
Revision: 4161
Author: siebrand
Date: 2007-08-31 17:38:07 +0000 (Fri, 31 Aug 2007)
Log Message:
-----------
This script requires Python version 2.5
Modified Paths:
--------------
trunk/pywikipedia/welcome.py
Modified: trunk/pywikipedia/welcome.py
===================================================================
--- trunk/pywikipedia/welcome.py 2007-08-31 17:37:54 UTC (rev 4160)
+++ trunk/pywikipedia/welcome.py 2007-08-31 17:38:07 UTC (rev 4161)
@@ -1,6 +1,9 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
+
+ATTENTION! THIS SCRIPT *REQUIRES* PYTHON 2.5
+
Script to welcome new users. This script works out of the box for Wikis that
have been defined in the script. It is currently used on the Dutch, Norwegian,
Arabian, Albanian, Italian Wikipedia, Wikimedia Commons and English Wikiquote.
Revision: 4160
Author: btongminh
Date: 2007-08-31 17:37:54 +0000 (Fri, 31 Aug 2007)
Log Message:
-----------
After IRC discussion we decided to be compatible with 2.4. Also CVS->SVN.
Modified Paths:
--------------
trunk/pywikipedia/CONTENTS
Modified: trunk/pywikipedia/CONTENTS
===================================================================
--- trunk/pywikipedia/CONTENTS 2007-08-31 17:26:51 UTC (rev 4159)
+++ trunk/pywikipedia/CONTENTS 2007-08-31 17:37:54 UTC (rev 4160)
@@ -213,13 +213,13 @@
python interwiki.py -help
-You need to have at least python version 2.5 (http://www.python.org/download/)
+You need to have at least python version 2.4 (http://www.python.org/download/)
installed on your computer to be able to run any of the code in this package.
Support for older versions of python is not planned.
You do not need to "install" this package to be able to make use of
it. You can actually just run it from the directory where you unpacked
-it or where you have your copy of the CVS sources.
+it or where you have your copy of the SVN sources.
Before you run any of the programs, you need to create a file named
user-config.py in your current directory. It needs at least two lines:
Revision: 4156
Author: btongminh
Date: 2007-08-31 16:59:19 +0000 (Fri, 31 Aug 2007)
Log Message:
-----------
Fix getReferences() Isredirect error. (Dantman)
Modified Paths:
--------------
trunk/pywikipedia/wikipedia.py
Modified: trunk/pywikipedia/wikipedia.py
===================================================================
--- trunk/pywikipedia/wikipedia.py 2007-08-30 20:59:03 UTC (rev 4155)
+++ trunk/pywikipedia/wikipedia.py 2007-08-31 16:59:19 UTC (rev 4156)
@@ -881,7 +881,8 @@
next_msg = re.sub(r"{{PLURAL:\$1\|(.*?)\|(.*?)}}", plural, next_msg)
nextpattern = re.compile("^%s$" % next_msg.replace("$1", "[0-9]+"))
delay = 1
- self._isredirectmessage = self.site().mediawiki_message("Isredirect")
+ if self.site().has_mediawiki_message("Isredirect"):
+ self._isredirectmessage = self.site().mediawiki_message("Isredirect")
if self.site().has_mediawiki_message("Istemplate"):
self._istemplatemessage = self.site().mediawiki_message("Istemplate")
# to avoid duplicates:
@@ -926,7 +927,8 @@
isredirect, istemplate = False, False
textafter = link.a.findNextSibling(text=True)
if textafter is not None:
- if self._isredirectmessage in textafter:
+ if self.site().has_mediawiki_message("Isredirect") \
+ and self._isredirectmessage in textafter:
# make sure this is really a redirect to this page
# (MediaWiki will mark as a redirect any link that follows
# a #REDIRECT marker, not just the first one).