jenkins-bot has submitted this change and it was merged.
Change subject: [PEP8] changes ......................................................................
[PEP8] changes
Change-Id: I7e5b91177806e19100e8e140f4e8674397b9a522 --- M rciw.py M rcsort.py M replace.py M replicate_wiki.py M revertbot.py 5 files changed, 137 insertions(+), 122 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/rciw.py b/rciw.py index 1bdfa9f..38d01ae 100644 --- a/rciw.py +++ b/rciw.py @@ -21,7 +21,7 @@ # http://hu.wikipedia.org/wiki/User:Kisbes # License : GFDL # -# (C) Pywikipedia bot team, 2008, 2010 +# (C) Pywikibot team, 2008-2013 # # Distributed under the terms of the MIT license. # @@ -35,8 +35,10 @@ import wikipedia as pywikibot import interwiki
+ class IWRCBot(): - def __init__(self, site, safe = True): + + def __init__(self, site, safe=True): self.other_ns = re.compile(u'14[[07(' + u'|'.join(site.namespaces()) + u')') interwiki.globalvar.autonomous = True self.site = site @@ -69,8 +71,11 @@ # it is a simple atomic append(), no need to acquire a semaphore self.queue.put_nowait(page)
+ def main(): - pywikibot.warning('this script can not be run manually/directly, but automatically by maintainer.py') + pywikibot.warning('this script can not be run manually/directly, but ' + 'automatically by maintainer.py') +
if __name__ == "__main__": main() diff --git a/rcsort.py b/rcsort.py index 70f6087..f338404 100644 --- a/rcsort.py +++ b/rcsort.py @@ -9,7 +9,7 @@ usable. Permission has been asked to run this on the toolserver. """ -# (C) Pywikipedia bot team, 2007-2012 +# (C) Pywikibot team, 2007-2013 # # Distributed under the terms of the MIT license. # @@ -19,7 +19,7 @@ import cgi import cgitb import re -import wikipedia as pywikibot +import pywikibot
cgitb.enable()
@@ -69,17 +69,18 @@ except AttributeError: user = None count += 1 - lines.append((user,count,line)) + lines.append((user, count, line)) elif 'rcoptions' in line: - print line.replace(mysite.path() + "?title=Speciaal:RecenteWijzigingen&", + print line.replace(mysite.path() + + "?title=Speciaal:RecenteWijzigingen&", "rcsort.py?") rcoptions = True elif newbies and 'Nieuwste' in line: - line = line.replace(mysite.path() + "?title=Speciaal:Bijdragen&", - "rcsort.py?").replace("target=newbies", - "newbies=true") + line = line.replace(mysite.path() + "?title=Speciaal:Bijdragen&", + "rcsort.py?").replace("target=newbies", + "newbies=true") if '</fieldset>' in line: - line = line[line.find('</fieldset>')+11:] + line = line[line.find('</fieldset>') + 11:] print line rcoptions = True lines.sort() @@ -88,13 +89,15 @@ for line in lines: if line[0] != last: print "</ul>" - if line[0] == None: + if line[0] is None: print "<h2>Gebruiker onbekend</h2>" else: - pywikibot.output(u"<h2>%s</h2>"%line[0],toStdout=True) + pywikibot.output(u"<h2>%s</h2>" % line[0], toStdout=True) print "<ul>" last = line[0] - pywikibot.output(line[2].replace('href="/w','href="http://nl.wikipedia.org/w'), toStdout = True) + pywikibot.output(line[2].replace('href="/w', + 'href="http://nl.wikipedia.org/w'), + toStdout=True) print
print "</ul>" diff --git a/replace.py b/replace.py index b347062..76adcfa 100644 --- a/replace.py +++ b/replace.py @@ -160,15 +160,19 @@ Please type "replace.py -help | more" if you can't read the top of the help. """ # -# (C) Daniel Herding & the Pywikipedia team, 2004-2012 +# (c) Daniel Herding, 2004-2007 +# (c) Pywikibot team, 2004-2013 # -__version__='$Id$' +__version__ = '$Id$' # # Distributed under the terms of the MIT license. #
-import sys, re, time, codecs -import wikipedia as pywikibot +import sys +import re +import time +import codecs +import pywikibot import pagegenerators import editarticle from pywikibot import i18n @@ -249,7 +253,7 @@ return True if "require-title" in self.exceptions: for req in self.exceptions['require-title']: - if not req.search(title): # if not all requirements are met: + if not req.search(title): # if not all requirements are met: return True
return False @@ -263,9 +267,8 @@
class ReplaceRobot: - """ - A bot that can do text replacements. - """ + """ A bot that can do text replacements. """ + def __init__(self, generator, replacements, exceptions={}, acceptall=False, allowoverlap=False, recursive=False, addedCat=None, sleep=None, editSummary='', articles=None, @@ -419,8 +422,8 @@ if self.editcounter % 100: return '' else: - return (u'<!-- ***** %dth title is above this line. ***** -->\n' % - self.editcounter) + return (u'<!-- ***** %dth title is above this line. ***** -->\n' + % self.editcounter)
def run(self): """ @@ -447,8 +450,8 @@ new_text = original_text while True: if self.isTextExcepted(new_text): - pywikibot.output( - u'Skipping %s because it contains text that is on the exceptions list.' + pywikibot.output(u'Skipping %s because it contains text ' + u'that is on the exceptions list.' % page.title(asLink=True)) break new_text = self.doReplacements(new_text) @@ -458,7 +461,7 @@ break if self.recursive: newest_text = self.doReplacements(new_text) - while (newest_text!=new_text): + while newest_text != new_text: new_text = newest_text newest_text = self.doReplacements(new_text) if hasattr(self, "addedCat"): @@ -476,16 +479,15 @@ break if self.exctitles: choice = pywikibot.inputChoice( - u'Do you want to accept these changes?', - ['Yes', 'No', 'no+eXcept', 'Edit', - 'open in Browser', 'All', 'Quit'], - ['y', 'N', 'x', 'e', 'b', 'a', 'q'], 'N') + u'Do you want to accept these changes?', + ['Yes', 'No', 'no+eXcept', 'Edit', + 'open in Browser', 'All', 'Quit'], + ['y', 'N', 'x', 'e', 'b', 'a', 'q'], 'N') else: choice = pywikibot.inputChoice( - u'Do you want to accept these changes?', - ['Yes', 'No', 'Edit', 'open in Browser', 'All', - 'Quit'], - ['y', 'N', 'e', 'b', 'a', 'q'], 'N') + u'Do you want to accept these changes?', + ['Yes', 'No', 'Edit', 'open in Browser', 'All', 'Quit'], + ['y', 'N', 'e', 'b', 'a', 'q'], 'N') if choice == 'e': editor = editarticle.TextEditor() as_edited = editor.edit(original_text) @@ -513,7 +515,7 @@ return if choice == 'a': self.acceptall = True - if choice == 'x': #May happen only if self.exctitles isn't None + if choice == 'x': # May happen only if self.exctitles isn't None self.exctitles.write( u"ur'^%s$',\n" % re.escape(page.title())) self.exctitles.flush() @@ -527,12 +529,13 @@ # This is separately in two clauses of if for # future purposes to get feedback form put_async else: - #Save the title for later processing instead of editing + # Save the title for later processing instead of editing self.editcounter += 1 self.articles.write(u'#%s\n%s' - % (page.title(asLink=True, textlink=True), - self.splitLine())) - self.articles.flush() # For the peace of our soul :-) + % (page.title(asLink=True, + textlink=True), + self.splitLine())) + self.articles.flush() # For the peace of our soul :-) # choice must be 'N' break if self.acceptall and new_text != original_text: @@ -540,7 +543,7 @@ #Primary behaviour: working on wiki try: page.put(new_text, self.editSummary) - self.editcounter += 1 #increment only on success + self.editcounter += 1 # increment only on success except pywikibot.EditConflict: pywikibot.output(u'Skipping %s because of edit conflict' % (page.title(),)) @@ -558,19 +561,20 @@ #Save the title for later processing instead of editing self.editcounter += 1 self.articles.write(u'#%s\n%s' - % (page.title(asLink=True, textlink=True), - self.splitLine())) + % (page.title(asLink=True, + textlink=True), + self.splitLine())) self.articles.flush()
#Finally: self.writeEditCounter() self.writeExceptCounter()
+ def prepareRegexForMySQL(pattern): pattern = pattern.replace('\s', '[:space:]') pattern = pattern.replace('\d', '[:digit:]') pattern = pattern.replace('\w', '[:alnum:]') - pattern = pattern.replace("'", "\" + "'") #pattern = pattern.replace('\', '\\') #for char in ['[', ']', "'"]: @@ -594,8 +598,8 @@ 'text-contains': [], 'inside': [], 'inside-tags': [], - 'require-title': [], # using a seperate requirements dict needs some - } # major refactoring of code. + 'require-title': [], # using a seperate requirements dict needs some + } # major refactoring of code.
# Should the elements of 'replacements' and 'exceptions' be interpreted # as regular expressions? @@ -634,17 +638,16 @@ # too much CPU sleep = None # Do not save the page titles, rather work on wiki - filename = None # The name of the file to save titles - titlefile = None # The file object itself + filename = None # The name of the file to save titles + titlefile = None # The file object itself # If we save, primary behaviour is append rather then new file append = True # Default: don't write titles to exception file and don't read them. - excoutfilename = None # The name of the file to save exceptions - excoutfile = None # The file object itself + excoutfilename = None # The name of the file to save exceptions + excoutfile = None # The file object itself # excinfilename: reserved for later use (reading back exceptions) # If we save exceptions, primary behaviour is append excappend = True -
# Read commandline parameters. for arg in pywikibot.handleArgs(*args): @@ -661,7 +664,7 @@ xmlFilename = i18n.input('pywikibot-enter-xml-filename') else: xmlFilename = arg[5:] - elif arg =='-sql': + elif arg == '-sql': useSql = True elif arg.startswith('-page'): if len(arg) == 5: @@ -706,7 +709,7 @@ try: commandline_replacements.extend( [x.lstrip(u'\uFEFF').rstrip('\r\n') - for x in codecs.open(replacefile, 'r', 'utf-8')]) + for x in codecs.open(replacefile, 'r', 'utf-8')]) except IOError: raise pywikibot.Error( '\n%s cannot be opened. Try again :-)' % replacefile) @@ -764,12 +767,12 @@ commandline_replacements[1])}) elif (len(commandline_replacements) > 1): if (fix is None): - for i in xrange (0, len(commandline_replacements), 2): + for i in xrange(0, len(commandline_replacements), 2): replacements.append((commandline_replacements[i], commandline_replacements[i + 1])) if not summary_commandline: - pairs = [( commandline_replacements[i], - commandline_replacements[i + 1] ) + pairs = [(commandline_replacements[i], + commandline_replacements[i + 1]) for i in range(0, len(commandline_replacements), 2)] replacementsDescription = '(%s)' % ', '.join( [('-' + pair[0] + ' +' + pair[1]) for pair in pairs]) @@ -778,8 +781,8 @@ {'description': replacementsDescription}) else: - raise pywikibot.Error( - 'Specifying -fix with replacements is undefined') + raise pywikibot.Error( + 'Specifying -fix with replacements is undefined') elif fix is None: old = pywikibot.input(u'Please enter the text that should be replaced:') new = pywikibot.input(u'Please enter the new text:') @@ -787,8 +790,8 @@ replacements.append((old, new)) while True: old = pywikibot.input( - u'Please enter another text that should be replaced,' + - u'\nor press Enter to start:') + u'Please enter another text that should be replaced,\n' + u'or press Enter to start:') if old == '': change += ')' break @@ -810,7 +813,7 @@
else: # Perform one of the predefined actions. - fixname = fix # Save the name for passing to exceptions function. + fixname = fix # Save the name for passing to exceptions function. try: fix = fixes.fixes[fix] except KeyError: @@ -839,8 +842,8 @@ baseExcDict = incl except NameError: pywikibot.output( - u'\nIncluded exceptions dictionary does not exist.' + - u' Continuing with the exceptions\ngiven in fix.\n') + u'\nIncluded exceptions dictionary does not exist.' + u' Continuing with the exceptions\ngiven in fix.\n') baseExcDict = None if baseExcDict: for l in baseExcDict: @@ -879,8 +882,8 @@ oldR = re.compile(old, flags) replacements[i] = oldR, new
- for exceptionCategory in [ - 'title', 'require-title', 'text-contains', 'inside']: + for exceptionCategory in ['title', 'require-title', + 'text-contains', 'inside']: if exceptionCategory in exceptions: patterns = exceptions[exceptionCategory] if not regex: @@ -943,8 +946,8 @@ if excoutfilename: try: excoutfile = codecs.open( - excoutfilename, encoding='utf-8', - mode=(lambda x: x and 'a' or 'w')(excappend)) + excoutfilename, encoding='utf-8', + mode=(lambda x: x and 'a' or 'w')(excappend)) except IOError: pywikibot.output("%s cannot be opened for writing." % excoutfilename) diff --git a/replicate_wiki.py b/replicate_wiki.py index d6e9ba2..17f9383 100644 --- a/replicate_wiki.py +++ b/replicate_wiki.py @@ -1,26 +1,29 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -''' -This bot replicates all pages (from specific namespaces) in a wiki to a second wiki within one family. +""" +This bot replicates all pages (from specific namespaces) in a wiki to a second +wiki within one family.
Example: python replicate_wiki.py [-r] -ns 10 -f wikipedia -o nl li fy
-to copy all templates from an nlwiki to liwiki and fywiki. It will show which pages have to be changed +to copy all templates from an nlwiki to liwiki and fywiki. It will show which +pages have to be changed if -r is not present, and will only actually write pages if -r /is/ present.
-You can add replicate_replace to your user_config.py, which has the following format: +You can add replicate_replace to your user_config.py, which has the following +format:
replicate_replace = { 'wikipedia:li': {'Hoofdpagina': 'Veurblaad'} }
-to replace all occurences of 'Hoofdpagina' with 'Veurblaad' when writing to liwiki. Note that this does -not take the origin wiki into account. -''' +to replace all occurences of 'Hoofdpagina' with 'Veurblaad' when writing to +liwiki. Note that this does not take the origin wiki into account. +""" # # (C) Kasper Souren 2012-2013 -# (C) 2013 Pywikipediabot team +# (C) 2013 Pywikibot team # # Distributed under the terms of the MIT license. # @@ -32,26 +35,27 @@ from wikipedia import * from itertools import imap
+ def namespaces(site): - '''dict from namespace number to prefix''' - ns = dict(map(lambda n: (site.getNamespaceIndex(n), n), + """dict from namespace number to prefix""" + ns = dict(map(lambda n: (site.getNamespaceIndex(n), n), site.namespaces())) ns[0] = '' return ns
def multiple_replace(text, word_dict): - '''Replace all occurrences in text of key value pairs in word_dict''' + """Replace all occurrences in text of key value pairs in word_dict""" for key in word_dict: text = text.replace(key, word_dict[key]) return text
class SyncSites: - '''Work is done in here.''' + """Work is done in here."""
def __init__(self, options): - self.options = options + self.options = options
if options.original_wiki: original_wiki = options.original_wiki @@ -61,11 +65,9 @@ print "Syncing from " + original_wiki
family = options.family or config.family - sites = options.destination_wiki - self.original = getSite(original_wiki, family) - + if options.namespace and 'help' in options.namespace: nsd = namespaces(self.original) for k in nsd: @@ -76,7 +78,7 @@
self.differences = {} self.user_diff = {} - print 'Syncing to', + print 'Syncing to', for s in self.sites: self.differences[s] = [] self.user_diff[s] = [] @@ -84,16 +86,17 @@ print
def check_sysops(self): - '''Check if sysops are the same + """Check if sysops are the same
TODO: make optional - ''' + + """ def get_users(site): userlist = site.getUrl(site.get_address('Special:Userlist&group=sysop')) # Hackery but working. At least on MW 1.15.0 # User namespace is number 2 return set(re.findall(site.namespace(2) + ':(\w+)["&]', userlist)) - + ref_users = get_users(self.original) for site in self.sites: users = get_users(site) @@ -102,18 +105,18 @@ self.user_diff[site] = diff
def check_namespaces(self): - '''Check all namespaces, to be ditched for clarity''' + """Check all namespaces, to be ditched for clarity""" namespaces = [ - 0, # Main - 8, # MediaWiki - 152, # DPL - 102, # Eigenschap - 104, # Type - 106, # Formulier - 108, # Concept - 10, # Sjabloon - ] - + 0, # Main + 8, # MediaWiki + 152, # DPL + 102, # Eigenschap + 104, # Type + 106, # Formulier + 108, # Concept + 10, # Sjabloon + ] + if self.options.namespace: print options.namespace namespaces = [int(options.namespace)] @@ -123,13 +126,13 @@ self.check_namespace(ns)
def check_namespace(self, namespace): - '''Check an entire namespace''' + """Check an entire namespace"""
print "\nCHECKING NAMESPACE", namespace pages = imap(lambda p: p.title(), - self.original.allpages('!', namespace)) + self.original.allpages('!', namespace)) for p in pages: - if not p in ['MediaWiki:Sidebar', 'MediaWiki:Mainpage', + if not p in ['MediaWiki:Sidebar', 'MediaWiki:Mainpage', 'MediaWiki:Sitenotice', 'MediaWiki:MenuSidebar']: try: self.check_page(p) @@ -139,32 +142,32 @@ print 'error: Redirectpage - todo: handle gracefully' print
- def generate_overviews(self): - '''Create page on wikis with overview of bot results''' + """Create page on wikis with overview of bot results""" for site in self.sites: sync_overview_page = Page(site, 'User:' + site.loggedInAs() + '/sync.py overview') output = "== Pages that differ from original ==\n\n" if self.differences[site]: - output += "".join(map(lambda l: '* [[:' + l + "]]\n", self.differences[site])) + output += "".join(map(lambda l: '* [[:' + l + "]]\n", + self.differences[site])) else: output += "All important pages are the same" - + output += "\n\n== Admins from original that are missing here ==\n\n" if self.user_diff[site]: - output += "".join(map(lambda l: '* ' + l.replace('_', ' ') + "\n", self.user_diff[site])) + output += "".join(map(lambda l: '* ' + l.replace('_', ' ') + "\n", + self.user_diff[site])) else: output += "All users from original are also present on this wiki"
print output sync_overview_page.put(output, self.put_message(site))
- def put_message(self, site): return site.loggedInAs() + ' sync.py synchronization from ' + str(self.original)
def check_page(self, pagename): - '''Check one page''' + """Check one page"""
print "\nChecking", pagename, sys.stdout.flush() @@ -180,16 +183,16 @@ print "\nCross namespace, new title: ", new_pagename else: new_pagename = pagename - + page2 = Page(site, new_pagename) if page2.exists(): txt2 = page2.get() - else: txt2 = '' - - if config.replicate_replace.has_key(str(site)): - txt_new = multiple_replace(txt1, config.replicate_replace[str(site)]) + + if str(site) in config.replicate_replace: + txt_new = multiple_replace(txt1, + config.replicate_replace[str(site)]) if txt1 != txt_new: print 'NOTE: text replaced using config.sync_replace' print txt1, txt_new, txt2 @@ -198,9 +201,8 @@ if txt1 != txt2: print "\n", site, 'DIFFERS' self.differences[site].append(pagename) - - if self.options.replace: - page2.put(txt1, self.put_message(site)) + if self.options.replace: + page2.put(txt1, self.put_message(site)) else: sys.stdout.write('.') sys.stdout.flush() @@ -223,7 +225,7 @@ help="specify namespace") parser.add_argument("-dns", "--dest-namespace", dest="dest_namespace", help="destination namespace (if different)") - + (options, args) = parser.parse_known_args()
# sync is global for convenient IPython debugging @@ -231,4 +233,3 @@ sync.check_sysops() sync.check_namespaces() sync.generate_overviews() - diff --git a/revertbot.py b/revertbot.py index 7f54cf5..dc58164 100644 --- a/revertbot.py +++ b/revertbot.py @@ -33,7 +33,8 @@ 'uclimit': '500', 'ucuser': self.site.username(), } - if ns: predata['ucnamespace'] = ns + if ns: + predata['ucnamespace'] = ns if max < 500 and max != -1: predata['uclimit'] = str(max)
@@ -103,8 +104,10 @@ rev = page['revisions'][1]
comment = u'Reverted to revision %s by %s on %s' % (rev['revid'], - rev['user'], rev['timestamp']) - if self.comment: comment += ': ' + self.comment + rev['user'], + rev['timestamp']) + if self.comment: + comment += ': ' + self.comment
page = pywikibot.Page(self.site, item['title']) pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" @@ -125,7 +128,7 @@ def callback(self, item): if 'top' in item: page = pywikibot.Page(self.site, item['title']) - text=page.get() + text = page.get() pattern = re.compile(u'[[.+?:.+?..+?]]', re.UNICODE) return pattern.search(text) >= 0 return False
pywikibot-commits@lists.wikimedia.org