http://www.mediawiki.org/wiki/Special:Code/pywikipedia/10196
Revision: 10196
Author: xqt
Date: 2012-05-06 11:39:36 +0000 (Sun, 06 May 2012)
Log Message:
-----------
rewrite for PageArchiver.loadConfig() using templatesWithParams to enable previous malformed templates;
new option --simulate for simulating changes;
last minute fix moved to the method PageArchiver.set()
Modified Paths:
--------------
trunk/pywikipedia/archivebot.py
Modified: trunk/pywikipedia/archivebot.py
===================================================================
--- trunk/pywikipedia/archivebot.py 2012-05-06 07:12:44 UTC (rev 10195)
+++ trunk/pywikipedia/archivebot.py 2012-05-06 11:39:36 UTC (rev 10196)
@@ -53,22 +53,24 @@
subpages of the page being archived.
-Options:
+Options (may be omitted):
-h, --help show this help message and exit
+ -c PAGE, --calc=PAGE calculate key for PAGE and exit
-f FILE, --file=FILE load list of pages from FILE
- -p PAGE, --page=PAGE archive a single PAGE
- -n NAMESPACE, --namespace=NAMESPACE
- only archive pages from a given namespace
- -s SALT, --salt=SALT specify salt
-F, --force override security options
- -c PAGE, --calc=PAGE calculate key for PAGE and exit
-l LOCALE, --locale=LOCALE
switch to locale LOCALE
-L LANG, --lang=LANG set the language code to work on
+ -n NAMESPACE, --namespace=NAMESPACE
+ only archive pages from a given namespace
+ -p PAGE, --page=PAGE archive a single PAGE
+ -s SALT, --salt=SALT specify salt
+ -S --simulate Do not change pages, just simulate
"""
#
# (C) Misza13, 2006-2010
-# (C) Pywikipedia bot team, 2007-2010
+# (C) xqt, 2009-2012
+# (C) Pywikipedia bot team, 2007-2012
#
# Distributed under the terms of the MIT license.
#
@@ -93,20 +95,25 @@
def message(key, lang=Site.language()):
return i18n.twtranslate(lang, key)
+
class MalformedConfigError(pywikibot.Error):
"""There is an error in the configuration template."""
+
class MissingConfigError(pywikibot.Error):
"""The config is missing in the header (either it's in one of the threads
or transcluded from another page)."""
+
class AlgorithmError(MalformedConfigError):
"""Invalid specification of archiving algorithm."""
+
class ArchiveSecurityError(pywikibot.Error):
"""Archive is not a subpage of page being archived and key not specified
(or incorrect)."""
+
def str2time(str):
"""Accepts a string defining a time period:
7d - 7 days
@@ -191,6 +198,7 @@
eicontinue):
yield page
+
class DiscussionThread(object):
"""An object representing a discussion thread on a page, that is something of the form:
@@ -285,15 +293,14 @@
return message('archivebot-older-than') + ' ' + reT.group(1)
return ''
+
class DiscussionPage(pywikibot.Page):
"""A class that represents a single discussion page as well as an archive
page. Feed threads to it and run an update() afterwards."""
def __init__(self, title, archiver, vars=None):
pywikibot.Page.__init__(self, Site, title, defaultNamespace=3)
-## self.title = title
self.threads = []
-## self.Page = self
self.full = False
self.archiver = archiver
self.vars = vars
@@ -312,17 +319,17 @@
self.archives = {}
self.archivedThreads = 0
lines = self.get().split('\n')
- state = 0 #Reading header
+ found = False #Reading header
curThread = None
for line in lines:
threadHeader = re.search('^== *([^=].*?) *== *$',line)
if threadHeader:
- state = 1 #Reading threads now
+ found = True #Reading threads now
if curThread:
self.threads.append(curThread)
curThread = DiscussionThread(threadHeader.group(1))
else:
- if state == 1:
+ if found:
curThread.feedLine(line)
else:
self.header += line + '\n'
@@ -355,6 +362,7 @@
summary += ' ' + message('archivebot-archive-full')
self.put(newtext, minorEdit=True, comment=summary)
+
class PageArchiver(object):
"""A class that encapsulates all archiving methods.
__init__ expects a pywikibot.Page object.
@@ -385,6 +393,8 @@
return self.attributes.get(attr,[default])[0]
def set(self, attr, value, out=True):
+ if attr = 'archive':
+ value = value.replace('_',' ')
self.attributes[attr] = [value, out]
def saveables(self):
@@ -404,26 +414,20 @@
return self.get('key') == s.hexdigest()
def loadConfig(self):
- hdrlines = self.Page.header.split('\n')
- pywikibot.output(u'Looking for: %s in %s' % (self.tpl, self.Page.title()))
- mode = 0
- for line in hdrlines:
- if mode == 0 and re.search('{{'+self.tpl,line):
- mode = 1
- continue
- if mode == 1 and re.match('}}',line):
+ pywikibot.output(u'Looking for: {{%s}} in %s' % (self.tpl, self.Page))
+ found = False
+ for tpl in self.Page.templatesWithParams(thistxt=self.Page.header):
+ if tpl[0] == self.tpl:
+ for param in tpl[1]:
+ item, value = param.split('=')
+ self.set(item, value)
+ found = True
break
- attRE = re.search(r'^\| *(\w+) *= *(.*?) *$',line)
- if mode == 1 and attRE:
- self.set(attRE.group(1),attRE.group(2))
- continue
+ if not found:
+ raise MissingConfigError(u'Missing or malformed template')
+ if not self.get('algo', ''):
+ raise MissingConfigError(u'Missing algo')
- if mode == 0 or not self.get('algo',''):
- raise MissingConfigError(u'Missing od malformed template or missing algo')
-
- #Last minute fix:
- self.set('archive', self.get('archive').replace('_',' '), True)
-
def feedArchive(self, archive, thread, maxArchiveSize, vars=None):
"""Feed the thread to one of the archives.
If it doesn't exist yet, create it.
@@ -510,6 +514,7 @@
self.commentParams)
self.Page.update(comment)
+
def main():
global Site, language
from optparse import OptionParser
@@ -532,6 +537,8 @@
help='current language code', metavar='lang')
parser.add_option('-T', '--timezone', dest='timezone',
help='switch timezone to TIMEZONE', metavar='TIMEZONE')
+ parser.add_option('-S', '--simulate', action='store_true', dest='simulate',
+ help='Do not change pages, just simulate')
(options, args) = parser.parse_args()
if options.locale:
@@ -567,6 +574,9 @@
Site = pywikibot.getSite(options.lang)
language = Site.language()
+ if options.simulate:
+ pywikibot.simulate = True
+
if not args:
pywikibot.output(u'NOTE: you must specify a template to run the bot')
pywikibot.showHelp('archivebot')
http://www.mediawiki.org/wiki/Special:Code/pywikipedia/10195
Revision: 10195
Author: xqt
Date: 2012-05-06 07:12:44 +0000 (Sun, 06 May 2012)
Log Message:
-----------
Python 2.7.3 is released
Modified Paths:
--------------
trunk/pywikipedia/wikipedia.py
Modified: trunk/pywikipedia/wikipedia.py
===================================================================
--- trunk/pywikipedia/wikipedia.py 2012-05-06 06:41:57 UTC (rev 10194)
+++ trunk/pywikipedia/wikipedia.py 2012-05-06 07:12:44 UTC (rev 10195)
@@ -8270,7 +8270,7 @@
Pywikibot has detected that you use this outdated version of Python:
%s.
We would like to hear your voice before ceasing support of this version.
-Please update to \03{lightyellow}Python 2.7.2\03{default} if possible or visit
+Please update to \03{lightyellow}Python 2.7.2\03{default} or higher if possible or visit
http://www.mediawiki.org/wiki/Pywikipediabot/Survey2012 to tell us why we
should support your version and to learn how to hide this message.
After collecting opinions for a time we will decide and announce the deadline
http://www.mediawiki.org/wiki/Special:Code/pywikipedia/10193
Revision: 10193
Author: xqt
Date: 2012-05-06 06:23:41 +0000 (Sun, 06 May 2012)
Log Message:
-----------
i18n.input() from trunk
Modified Paths:
--------------
branches/rewrite/scripts/redirect.py
Modified: branches/rewrite/scripts/redirect.py
===================================================================
--- branches/rewrite/scripts/redirect.py 2012-05-06 06:22:31 UTC (rev 10192)
+++ branches/rewrite/scripts/redirect.py 2012-05-06 06:23:41 UTC (rev 10193)
@@ -50,7 +50,7 @@
# (C) Daniel Herding, 2004.
# (C) Purodha Blissenbach, 2009.
# (C) xqt, 2009-2012
-# (C) Pywikipedia bot team, 2004-2011
+# (C) Pywikipedia bot team, 2004-2012
#
# Distributed under the terms of the MIT license.
#
@@ -667,8 +667,7 @@
action = 'both'
elif arg.startswith('-xml'):
if len(arg) == 4:
- xmlFilename = pywikibot.input(
- u'Please enter the XML dump\'s filename: ')
+ xmlFilename = i18n.input('pywikibot-enter-xml-filename')
else:
xmlFilename = arg[5:]
elif arg.startswith('-moves'):
@@ -677,10 +676,8 @@
ns = arg[11:]
if ns == '':
## "-namespace:" does NOT yield -namespace:0 further down the road!
- ns = pywikibot.input(
- u'Please enter a namespace by its number: ')
-# u'Please enter a namespace by its name or number: ')
-# TODO! at least for some generators.
+ ns = i18n.input('pywikibot-enter-namespace-number')
+# TODO! at least for some generators enter a namespace by its name or number
if ns == '':
ns = '0'
try:
http://www.mediawiki.org/wiki/Special:Code/pywikipedia/10192
Revision: 10192
Author: xqt
Date: 2012-05-06 06:22:31 +0000 (Sun, 06 May 2012)
Log Message:
-----------
use xrange iterator instead of range list in for statements
Modified Paths:
--------------
branches/rewrite/scripts/interwiki.py
Modified: branches/rewrite/scripts/interwiki.py
===================================================================
--- branches/rewrite/scripts/interwiki.py 2012-05-06 06:21:27 UTC (rev 10191)
+++ branches/rewrite/scripts/interwiki.py 2012-05-06 06:22:31 UTC (rev 10192)
@@ -2091,7 +2091,7 @@
% fs.originPage)
pywikibot.output(u"NOTE: Number of pages queued is %d, trying to add %d more."
% (len(self.subjects), number))
- for i in range(number):
+ for i in xrange(number):
try:
while True:
try:
@@ -2248,7 +2248,7 @@
def queryStep(self):
self.oneQuery()
# Delete the ones that are done now.
- for i in range(len(self.subjects)-1, -1, -1):
+ for i in xrange(len(self.subjects)-1, -1, -1):
subj = self.subjects[i]
if subj.isDone():
subj.finish(self)