jenkins-bot has submitted this change and it was merged.
Change subject: [PEP8] changes ......................................................................
[PEP8] changes
Change-Id: Ifc50fef858c4724238db811c90ecc38e3bfe8ff9 --- M pagefromfile.py M pageimport.py M parserfunctioncount.py M piper.py M protect.py M pwb.py 6 files changed, 142 insertions(+), 94 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pagefromfile.py b/pagefromfile.py index 31c34fe..23a46ef 100644 --- a/pagefromfile.py +++ b/pagefromfile.py @@ -42,26 +42,30 @@ """ # # (C) Andre Engels, 2004 -# (C) Pywikipedia bot team, 2005-2010 +# (C) Pywikibot team, 2005-2013 # # Distributed under the terms of the MIT license. # -__version__='$Id$' +__version__ = '$Id$' #
-import re, codecs -import wikipedia as pywikibot +import re +import codecs +import pywikibot import config +
class NoTitle(Exception): """No title found""" def __init__(self, offset): self.offset = offset
+ class PageFromFileRobot: """ Responsible for writing pages to the wiki, with the titles and contents given by a PageFromFileReader. + """
msg = { @@ -152,7 +156,7 @@ self.minor = minor self.autosummary = autosummary self.dry = dry - self.nocontents=nocontents + self.nocontents = nocontents
def run(self): for title, contents in self.reader.run(): @@ -180,24 +184,29 @@ self.msg_force) + " ***"
# Remove trailing newlines (cause troubles when creating redirects) - contents = re.sub('^[\r\n]*','', contents) + contents = re.sub('^[\r\n]*', '', contents)
if page.exists(): if self.append == "Top": - if appendtops.find(self.nocontents)==-1 and appendtops.find(self.nocontents.lower())==-1: - contents=contents +appendtops - pywikibot.output(u"Page %s already exists, appending on top!" - % title) + if appendtops.find(self.nocontents) == -1 and \ + appendtops.find(self.nocontents.lower()) == -1: + contents += appendtops + pywikibot.output( + u"Page %s already exists, appending on top!" + % title) else: - pywikibot.output(u'Page had %s so it is skipped' % (self.nocontents)) + pywikibot.output(u'Page had %s so it is skipped' + % (self.nocontents)) return - contents = contents + page.get() + contents += page.get() comment = comment_top elif self.append == "Bottom": - if appendtops.find(self.nocontents)==-1 and appendtops.find(self.nocontents.lower())==-1: - contents=contents +appendtops - pywikibot.output(u"Page %s already exists, appending on bottom!" - % title) + if appendtops.find(self.nocontents) == -1 and \ + appendtops.find(self.nocontents.lower()) == -1: + contents += appendtops + pywikibot.output( + u"Page %s already exists, appending on bottom!" + % title) else: pywikibot.output(u'Page had %s so it is skipped' % (self.nocontents)) return @@ -211,19 +220,20 @@ pywikibot.output(u"Page %s already exists, not adding!" % title) return else: - if self.autosummary: + if self.autosummary: comment = '' pywikibot.setAction('')
if self.dry: - pywikibot.output("*** Dry mode ***\n" + \ - "\03{lightpurple}title\03{default}: " + title + "\n" + \ - "\03{lightpurple}contents\03{default}:\n" + contents + "\n" \ - "\03{lightpurple}comment\03{default}: " + comment + "\n") + pywikibot.output( + u"*** Dry mode ***\n" + u"\03{lightpurple}title\03{default}: " + title + "\n" + u"\03{lightpurple}contents\03{default}:\n" + contents + "\n" + u"\03{lightpurple}comment\03{default}: " + comment + "\n") return
try: - page.put(contents, comment = comment, minorEdit = self.minor) + page.put(contents, comment=comment, minorEdit=self.minor) except pywikibot.LockedPage: pywikibot.output(u"Page %s is locked; skipping." % title) except pywikibot.EditConflict: @@ -232,6 +242,7 @@ pywikibot.output( u'Cannot change %s because of spam blacklist entry %s' % (title, error.url)) +
class PageFromFileReader: """ @@ -279,8 +290,10 @@ yield title, contents
def findpage(self, text): - pageR = re.compile(self.pageStartMarker + "(.*?)" + self.pageEndMarker, re.DOTALL) - titleR = re.compile(self.titleStartMarker + "(.*?)" + self.titleEndMarker) + pageR = re.compile(self.pageStartMarker + "(.*?)" + self.pageEndMarker, + re.DOTALL) + titleR = re.compile(self.titleStartMarker + "(.*?)" + + self.titleEndMarker)
location = pageR.search(text) if self.include: @@ -291,11 +304,12 @@ title = titleR.search(contents).group(1) if self.notitle: #Remove title (to allow creation of redirects) - contents = titleR.sub('', contents, count = 1) + contents = titleR.sub('', contents, count=1) except AttributeError: raise NoTitle(location.end()) else: return location.end(), title, contents +
def main(): # Adapt these to the file you are using. 'pageStartMarker' and @@ -308,7 +322,7 @@ pageEndMarker = "{{-stop-}}" titleStartMarker = u"'''" titleEndMarker = u"'''" - nocontents=u"" + nocontents = u"" include = False force = False append = None @@ -331,7 +345,7 @@ elif arg == "-appendbottom": append = "Bottom" elif arg == "-force": - force=True + force = True elif arg == "-safe": force = False append = None @@ -340,7 +354,7 @@ elif arg == '-minor': minor = True elif arg.startswith('-nocontent:'): - nocontents=arg[11:] + nocontents = arg[11:] elif arg.startswith("-titlestart:"): titleStartMarker = arg[12:] elif arg.startswith("-titleend:"): @@ -359,6 +373,7 @@ pywikibot.simulate, nocontents) bot.run()
+ if __name__ == "__main__": try: main() diff --git a/pageimport.py b/pageimport.py index d102787..dce9604 100644 --- a/pageimport.py +++ b/pageimport.py @@ -12,8 +12,8 @@ # Defing what page to load.. pagetoload = 'Apple' site = wikipedia.getSite() - importerbot = Importer(site) # Inizializing - importerbot.Import(pagetoload, prompt = True) + importerbot = Importer(site) # Inizializing + importerbot.Import(pagetoload, prompt=True) try: main() finally: @@ -21,7 +21,7 @@ """ # # (C) Filnik, 2007 -# (C) Pywikipedia bot team, 2008-2010 +# (C) Pywikibot team, 2008-2013 # # Greetings: # Lorenzo Paulatto and Misza13 @@ -33,16 +33,20 @@
import urllib import wikipedia as pywikibot -import login, config +import login +import config +
class Importer(pywikibot.Page): + def __init__(self, site): self.importsite = site pywikibot.Page.__init__(self, site, 'Special:Import', None, 0)
- def Import(self, target, project = 'w', crono = '1', namespace = '', prompt = True): + def Import(self, target, project='w', crono='1', namespace='', prompt=True): """Import the page from the wiki. Requires administrator status. If prompt is True, asks the user if he wants to delete the page. + """ if project == 'w': site = pywikibot.getSite(fam = 'wikipedia') @@ -57,37 +61,39 @@ else: site = pywikibot.getSite() # Fixing the crono value... - if crono == True: + if crono: crono = '1' - elif crono == False: + else: crono = '0' # Fixing namespace's value. if namespace == '0': namespace == '' answer = 'y' if prompt: - answer = pywikibot.inputChoice(u'Do you want to import %s?' % target, ['Yes', 'No'], ['y', 'N'], 'N') + answer = pywikibot.inputChoice(u'Do you want to import %s?' + % target, ['Yes', 'No'], ['y', 'N'], + 'N') if answer == 'y': host = self.site().hostname() address = self.site().path() + '?title=%s&action=submit' % self.urlname() # You need to be a sysop for the import. - self.site().forceLogin(sysop = True) + self.site().forceLogin(sysop=True) # Getting the token. - token = self.site().getToken(self, sysop = True) + token = self.site().getToken(self, sysop=True) # Defing the predata. predata = { - 'action' : 'submit', - 'source' : 'interwiki', + 'action': 'submit', + 'source': 'interwiki', # from what project do you want to import the page? - 'interwiki' : project, + 'interwiki': project, # What is the page that you want to import? - 'frompage' : target, + 'frompage': target, # The entire history... or not? - 'interwikiHistory' : crono, + 'interwikiHistory': crono, # What namespace do you want? 'namespace': '', } - response, data = self.site().postForm(address, predata, sysop = True) + response, data = self.site().postForm(address, predata, sysop=True) if data: pywikibot.output(u'Page imported, checking...') if pywikibot.Page(self.importsite, target).exists(): @@ -97,6 +103,8 @@ pywikibot.output(u'Import failed!') return False
-if __name__=='__main__': - pywikibot.output(u'This is just a module! Read the documentation and write your own script!') + +if __name__ == '__main__': + pywikibot.output(u'This is just a module! Read the documentation and ' + u'write your own script!') pywikibot.stopme() diff --git a/parserfunctioncount.py b/parserfunctioncount.py index 230c306..2c5c863 100644 --- a/parserfunctioncount.py +++ b/parserfunctioncount.py @@ -36,10 +36,13 @@
# # (C) Bináris, 2013 +# (C) Pywikibot team, 2013 # # Distributed under the terms of the MIT license. # -__version__='$Id$' +__version__ = '$Id$' +# + ''' Todo: * Using xml and xmlstart @@ -48,11 +51,12 @@ for the beauty of the program, does not effect anything). '''
-import codecs, re -import wikipedia as pywikibot -from pagegenerators import \ - AllpagesPageGenerator as APG, \ - RegexFilterPageGenerator as RPG +import codecs +import re +import pywikibot +from pagegenerators import AllpagesPageGenerator as APG +from pagegenerators import RegexFilterPageGenerator as RPG +
def main(*args): words = ['expr', 'if', 'ifeq', 'ifexpr', 'iferror', 'switch', 'ifexist', @@ -81,7 +85,7 @@ first = None atleast = None nosort = False - filename = None # The name of the file to save titles + filename = None # The name of the file to save titles titlefile = None uploadpage = None count = 0 @@ -90,7 +94,7 @@ for arg in pywikibot.handleArgs(*args): if arg == '-start': start = pywikibot.input( - u'From which title do you want to continue?') + u'From which title do you want to continue?') elif arg.startswith('-start:'): start = arg[7:] elif arg == '-save': @@ -114,7 +118,6 @@ elif arg == '-nosort': nosort = True
- # File operations: if filename: try: @@ -136,7 +139,7 @@ if atleast: try: atleast = int(atleast) - if atleast < 2: # 1 has no effect, don't waste resources. + if atleast < 2: # 1 has no effect, don't waste resources. atleast = None except ValueError: atleast = None @@ -145,7 +148,7 @@ site = pywikibot.getSite() lang = site.lang try: - words.extend(addwords[lang]) # Adding translated function names + words.extend(addwords[lang]) # Adding translated function names except KeyError: pass try: @@ -153,10 +156,10 @@ except KeyError: comment = editcomment['en'] try: - docregex = documentsubpage[lang] # Finding document subpage names + docregex = documentsubpage[lang] # Finding document subpage names except KeyError: docregex = ur'(?i).*/doc' - regex = re.compile(ur'(?i)#('+ur'|'.join(words)+'):') + regex = re.compile(ur'(?i)#(' + ur'|'.join(words) + '):') gen1 = APG(start=start, namespace=10, includeredirects=False, site=site) gen = RPG(gen1, docregex, inverse=True)
@@ -168,18 +171,18 @@ title = page.title() if not count % 50: # Don't let the poor user panic in front of a black screen. - pywikibot.output('%dth template is beeing processed: %s' % - (count, title)) + pywikibot.output('%dth template is beeing processed: %s' + % (count, title)) text = page.get() functions = regex.findall(text) if functions: - results.append((title,len(functions))) + results.append((title, len(functions))) if nosort and first and len(results) == first: break
# Combing the results: if not nosort: - results.sort(key=lambda x:str(5000-x[1])+'.'+x[0]) + results.sort(key=lambda x: str(5000-x[1]) + '.' + x[0]) if first: results = results[:first] if atleast: @@ -198,6 +201,7 @@ page = pywikibot.Page(site, uploadpage) page.put(resultlist, comment)
+ if __name__ == "__main__": try: main() diff --git a/piper.py b/piper.py index cd9f870..74d3812 100644 --- a/piper.py +++ b/piper.py @@ -32,7 +32,7 @@
""" # -# (C) Pywikipedia bot team, 2008-2013 +# (C) Pywikibot team, 2008-2013 # # Distributed under the terms of the MIT license. # @@ -42,7 +42,7 @@ import os import pipes import tempfile -import wikipedia as pywikibot +import pywikibot import pagegenerators
# This is required for the text that is shown when you run this script @@ -125,7 +125,7 @@
# Munge! for program in self.filters: - text = self.pipe(program, text); + text = self.pipe(program, text)
# only save if something was changed if text != page.get(): @@ -202,6 +202,7 @@ else: pywikibot.showHelp()
+ if __name__ == "__main__": try: main() diff --git a/protect.py b/protect.py index 120b896..85cfeb4 100644 --- a/protect.py +++ b/protect.py @@ -40,14 +40,14 @@ # Written by http://it.wikisource.org/wiki/Utente:Qualc1 # Created by modifying delete.py # -# (C) Pywikipedia bot team, 2008-2012 +# (C) Pywikibot team, 2008-2013 # # Distributed under the terms of the MIT license. # __version__ = '$Id$' #
-import wikipedia as pywikibot +import pywikibot import catlib import pagegenerators
@@ -102,19 +102,21 @@ 'zh': u'機器人: 保護頁面 %s 中的所有圖條', }
+ class ProtectionRobot: """ This robot allows protection of pages en masse. """
def __init__(self, generator, summary, always=False, unprotect=False, - edit='sysop', move='sysop', create='sysop'): + edit='sysop', move='sysop', create='sysop'): """ Arguments: * generator - A page generator. * always - Protect without prompting? * edit, move, create - protection level for these operations * unprotect - unprotect pages (and ignore edit, move, create params) + """ self.generator = generator self.summary = summary @@ -135,6 +137,7 @@ prompt=self.prompt, editcreate=self.edit, move=self.move)
+ # Asks a valid protection level for "operation". # Returns the protection level chosen by user. def choiceProtectionLevel(operation, default): @@ -147,6 +150,7 @@ for level in protectionLevels: if level.startswith(choiceChar): return level +
def main(): global protectionLevels @@ -173,18 +177,21 @@ always = True elif arg.startswith('-file'): if len(arg) == len('-file'): - fileName = pywikibot.input(u'Enter name of file to protect pages from:') + fileName = pywikibot.input( + u'Enter name of file to protect pages from:') else: fileName = arg[len('-file:'):] elif arg.startswith('-summary'): if len(arg) == len('-summary'): - summary = pywikibot.input(u'Enter a reason for the protection:') + summary = pywikibot.input( + u'Enter a reason for the protection:') else: summary = arg[len('-summary:'):] elif arg.startswith('-cat'): doCategory = True if len(arg) == len('-cat'): - pageName = pywikibot.input(u'Enter the category to protect from:') + pageName = pywikibot.input( + u'Enter the category to protect from:') else: pageName = arg[len('-cat:'):] elif arg.startswith('-nosubcats'): @@ -241,8 +248,8 @@ msg_protect_category) % pageName ns = mysite.category_namespace() categoryPage = catlib.Category(mysite, ns + ':' + pageName) - gen = pagegenerators.CategorizedPageGenerator(categoryPage, - recurse=protectSubcategories) + gen = pagegenerators.CategorizedPageGenerator( + categoryPage, recurse=protectSubcategories) elif doLinks: if not summary: summary = pywikibot.translate(mysite, diff --git a/pwb.py b/pwb.py index f3e538a..577ba98 100755 --- a/pwb.py +++ b/pwb.py @@ -16,7 +16,9 @@ ## @package pwb # @brief General DrTrigonBot Robot(s) Wrapper (see rewrite/pwb.py too) # -# @copyright Dr. Trigon, 2008-2013 +# @copyright +# (C) Dr. Trigon, 2008-2013 +# (C) Pywikibot team, 2013 # # @todo look also at and learn from daemonize.py (@ref daemonize) # @@ -39,7 +41,10 @@ #
-import sys, os, traceback, shelve +import sys +import os +import traceback +import shelve
# wikipedia-bot imports import userlib @@ -53,20 +58,24 @@ # @todo consider solving/doing this with a logging handler like SMTPHandler # def send_mailnotification(text, subject): - username = pywikibot.config.usernames[pywikibot.config.family][pywikibot.config.mylang] + username = pywikibot.config.usernames[ + pywikibot.config.family][pywikibot.config.mylang] pos = username.lower().find('bot') username = username[:pos] if (pos > 0) else username
- pywikibot.output(u'Sending mail "%s" to "%s" as notification!' % (subject, username)) + pywikibot.output(u'Sending mail "%s" to "%s" as notification!' + % (subject, username)) # JIRA: DRTRIGON-87; output even more debug info (tip by: valhallasw@arctus.nl) site = pywikibot.getSite() - pywikibot.output(u'Bot allowed to send email: %r' % (site.isAllowed('sendemail'),)) + pywikibot.output(u'Bot allowed to send email: %r' + % (site.isAllowed('sendemail'),)) pywikibot.output(u'Permissions: %r' % (site._rights,)) if not site.isAllowed('sendemail'): - pywikibot.output(u'Try getting new token: %r' % (site.getToken(getagain=True),)) + pywikibot.output(u'Try getting new token: %r' + % (site.getToken(getagain=True),)) usr = userlib.User(site, username) try: - if usr.sendMail(subject=subject, text=text): # 'text' should be unicode! + if usr.sendMail(subject=subject, text=text): # 'text' should be unicode! return except: # break exception handling recursion pywikibot.exception(tb=True) @@ -78,12 +87,12 @@ from email.mime.text import MIMEText # sender's and recipient's email addresses FROM = "%s@toolserver.org" % username.lower() - TO = [FROM] # must be a list + TO = [FROM] # must be a list # Create a text/plain message msg = MIMEText(text) msg['Subject'] = "!EMERGENCY! " + subject - msg['From'] = FROM - msg['To'] = ", ".join(TO) + msg['From'] = FROM + msg['To'] = ", ".join(TO) # Send the mail server = smtplib.SMTP("localhost") server.sendmail(FROM, TO, msg.as_string()) @@ -100,22 +109,26 @@ # @see http://segfault.in/2010/03/python-rrdtool-tutorial/ # class BotLoggerObject: + def __init__(self, layer='stdout'): self._layer = layer + def write(self, string): for line in string.strip().splitlines(): pywikibot.critical('[%s] %s' % (self._layer, line)) + def close(self): pass + def flush(self): pass
# SGE: exit errorlevel -ERROR_SGE_ok = 0 # successful termination, nothing more to do -ERROR_SGE_stop = 1 # error, but not for SGE +ERROR_SGE_ok = 0 # successful termination, nothing more to do +ERROR_SGE_stop = 1 # error, but not for SGE ERROR_SGE_restart = 99 # restart the job -ERROR_SGE_lock = 100 # stop in error state (prevents re-starts) +ERROR_SGE_lock = 100 # stop in error state (prevents re-starts)
exitcode = ERROR_SGE_stop # action in error case (stop with error) #exitcode = ERROR_SGE_restart # -------- " --------- (restart) @@ -126,7 +139,7 @@ raise RuntimeError("Not enough arguments defined.") else: logname = pywikibot.config.datafilepath('logs', '%s.log') - logfile = logname % ''.join([os.path.basename(sys.argv[0])]+sys.argv[1:]) + logfile = logname % ''.join([os.path.basename(sys.argv[0])] + sys.argv[1:])
arg = pywikibot.handleArgs() # '-family' and '-lang' have to be known for log-header sys.stdout = BotLoggerObject(layer='stdout') # handle stdout @@ -140,8 +153,8 @@ # check: http://de.wikipedia.org/wiki/Hilfe:MediaWiki/Versionen
path = os.path.split(sys.argv[0])[0] - cmd = sys.argv.pop(1) - cmd = cmd.lstrip(u'-') + cmd = sys.argv.pop(1) + cmd = cmd.lstrip(u'-') if u'.py' not in cmd.lower(): cmd += u'.py' sys.argv[0] = os.path.join(path, cmd) @@ -153,8 +166,8 @@ d = shelve.open(pywikibot.config.datafilepath('cache', 'state_bots')) d['bot_control'] = {'release_rev': str(__release['rev']), 'release_ver': __release['hsh'][:7], - 'online_ver': version.getversion_onlinerepo()[:7], - } + 'online_ver': version.getversion_onlinerepo()[:7], + } pywikibot.output(d['bot_control']) pywikibot.output(u'=== ' * 14) d.close() @@ -169,7 +182,7 @@ pywikibot.exception(tb=True) error = traceback.format_exc() if pywikibot.logger.isEnabledFor(pywikibot.DEBUG): - exitcode = ERROR_SGE_ok # print traceback of re-raised errors by skipping sys.exit() + exitcode = ERROR_SGE_ok # print traceback of re-raised errors by skipping sys.exit() raise else: send_mailnotification(error, u'Bot ERROR') @@ -182,7 +195,7 @@ d[name] = {'error': str(bool(error)), 'traceback': str(error.encode('utf-8')), 'timestamp': str(pywikibot.Timestamp.now().isoformat(' ')), - } + } d.close()
pywikibot.stopme()
pywikibot-commits@lists.wikimedia.org