jenkins-bot has submitted this change and it was merged.
Change subject: [PEP8] changes ......................................................................
[PEP8] changes
Change-Id: Ifdb9192402363eaf6070832db4fd95b2503567b0 --- M solve_disambiguation.py 1 file changed, 99 insertions(+), 89 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/solve_disambiguation.py b/solve_disambiguation.py index 8fe8cd6..5b8bc18 100644 --- a/solve_disambiguation.py +++ b/solve_disambiguation.py @@ -82,16 +82,18 @@ # (C) Daniel Herding, 2004 # (C) Andre Engels, 2003-2004 # (C) WikiWichtel, 2004 -# (C) Pywikipedia team, 2003-2012 +# (C) Pywikibot team, 2003-2013 # -__version__='$Id$' +__version__ = '$Id$' # # Distributed under the terms of the MIT license. #
# Standard library imports -import re, sys, codecs +import re +import sys +import codecs
# Application specific imports import wikipedia as pywikibot @@ -112,7 +114,7 @@
# Disambiguation Needed template dn_template = { - 'en' : u'{{dn}}', + 'en': u'{{dn}}', }
# Summary message when adding Disambiguation Needed template @@ -149,7 +151,7 @@ 'sr': u'%s_(вишезначна одредница)', 'sv': u'%s_(olika betydelser)', 'uk': u'%s_(значення)', - } +}
# List pages that will be ignored if they got a link to a disambiguation # page. An example is a page listing disambiguations articles. @@ -337,11 +339,11 @@ u'Wikipedia:Woorden die niet als zoekterm gebruikt kunnen worden', u'Overleg gebruiker:Taka(/.*)?', u"Wikipedia:Links naar doorverwijspagina's/Artikelen", - ], + ], 'pl': [ u'Wikipedysta:.+', u'Dyskusja.+:.+', - ], + ], 'pt': [ u'Usuário:.+', u'Usuário Discussão:.+', @@ -368,18 +370,21 @@ }, }
+ def firstcap(string): return string[0].upper()+string[1:] +
def correctcap(link, text): # If text links to a page with title link uncapitalized, uncapitalize link, # otherwise capitalize it linkupper = link.title() linklower = linkupper[0].lower() + linkupper[1:] - if "[[%s]]"%linklower in text or "[[%s|"%linklower in text: + if "[[%s]]" % linklower in text or "[[%s|" % linklower in text: return linklower else: return linkupper +
def firstlinks(page): #Returns a list of first links of every line beginning with * @@ -397,16 +402,16 @@ list.append(found.group(1)) return list
+ class ReferringPageGeneratorWithIgnore: - def __init__(self, disambPage, primary=False, minimum = 0): + def __init__(self, disambPage, primary=False, minimum=0): self.disambPage = disambPage self.minimum = minimum
def __iter__(self): generator = pagegenerators.ReferringPageGenerator( - self.disambPage, - followRedirects=False, - withTemplateInclusion=False) + self.disambPage, followRedirects=False, + withTemplateInclusion=False) generator = pagegenerators.PageTitleFilterPageGenerator(generator, ignore_title)
@@ -420,20 +425,21 @@ for ref in refs: yield ref
+ class PrimaryIgnoreManager(object): - ''' + """ If run with the -primary argument, reads from a file which pages should not be worked on; these are the ones where the user pressed n last time. If run without the -primary argument, doesn't ignore any pages. - ''' - def __init__(self, disambPage, enabled = False): + + """ + def __init__(self, disambPage, enabled=False): self.disambPage = disambPage self.enabled = enabled
self.ignorelist = [] filename = pywikibot.config.datafilepath( - 'disambiguations', - self.disambPage.titleForFilename() + '.txt') + 'disambiguations', self.disambPage.titleForFilename() + '.txt') try: # The file is stored in the disambiguation/ subdir. # Create if necessary. @@ -456,8 +462,7 @@ if self.enabled: # Skip this occurence next time. filename = pywikibot.config.datafilepath( - 'disambiguations', - self.disambPage.urlname() + '.txt') + 'disambiguations', self.disambPage.urlname() + '.txt') try: # Open file for appending. If none exists yet, create a new one. f = codecs.open(filename, 'a', 'utf-8') @@ -468,21 +473,22 @@
class DisambiguationRobot(object): + ignore_contents = { - 'de':(u'{{[Ii]nuse}}', - u'{{[Ll]öschen}}', - ), - 'fi':(u'{{[Tt]yöstetään}}', - ), - 'kk':(u'{{[Ii]nuse}}', - u'{{[Pp]rocessing}}', - ), - 'nl':(u'{{wiu2}}', - u'{{nuweg}}', - ), - 'ru':(u'{{[Ii]nuse}}', - u'{{[Pp]rocessing}}', - ), + 'de': (u'{{[Ii]nuse}}', + u'{{[Ll]öschen}}', + ), + 'fi': (u'{{[Tt]yöstetään}}', + ), + 'kk': (u'{{[Ii]nuse}}', + u'{{[Pp]rocessing}}', + ), + 'nl': (u'{{wiu2}}', + u'{{nuweg}}', + ), + 'ru': (u'{{[Ii]nuse}}', + u'{{[Pp]rocessing}}', + ), }
primary_redir_template = { @@ -491,7 +497,7 @@ }
def __init__(self, always, alternatives, getAlternatives, dnSkip, generator, - primary, main_only, first_only, minimum = 0): + primary, main_only, first_only, minimum=0): self.always = always self.alternatives = alternatives self.getAlternatives = getAlternatives @@ -524,9 +530,9 @@
def makeAlternativesUnique(self): # remove duplicate entries - result={} + result = {} for i in self.alternatives: - result[i]=None + result[i] = None self.alternatives = result.keys()
def listAlternatives(self): @@ -562,7 +568,7 @@ #disambiguation pages and leave the first link of each asterisked #line only. This must be done if -first is used in command line. titles = [firstcap(t) for t in firstlinks(page)] - for l in links[:]: #uses a copy because of remove! + for l in links[:]: # uses a copy because of remove! if l.title() not in titles: links.remove(l) return links @@ -584,7 +590,7 @@ unlink = False new_targets = [] try: - text=refPage.get(throttle=False) + text = refPage.get(throttle=False) ignoreReason = self.checkContents(text) if ignoreReason: pywikibot.output('\n\nSkipping %s because it contains %s.\n\n' @@ -603,7 +609,7 @@ redir_text = '#%s [[%s]]' \ % (self.mysite.redirect(default=True), target) try: - refPage.put_async(redir_text,comment=self.comment) + refPage.put_async(redir_text, comment=self.comment) except pywikibot.PageNotSaved, error: pywikibot.output(u'Page not saved: %s' % error.args) else: @@ -620,7 +626,7 @@ if not self.treat(refPage2, refPage): break elif choice == 'c': - text=refPage.get(throttle=False,get_redirect=True) + text = refPage.get(throttle=False, get_redirect=True) include = "redirect" except pywikibot.NoPage: pywikibot.output( @@ -636,7 +642,7 @@ edited = False # This loop will run until we have finished the current page while True: - m = self.linkR.search(text, pos = curpos) + m = self.linkR.search(text, pos=curpos) if not m: if n == 0: pywikibot.output(u"No changes necessary in %s" @@ -663,7 +669,7 @@ # how many bytes should be displayed around the current link context = 60 #there's a {{dn}} here already - already_dn = text[m.end() : m.end() + 8].find(dn_template_str[:4]) > -1 + already_dn = text[m.end():m.end() + 8].find(dn_template_str[:4]) > -1 if already_dn and self.dnSkip: continue
@@ -679,12 +685,13 @@ if not self.always: # at the beginning of the link, start red color. # at the end of the link, reset the color to default - pywikibot.output(text[max(0, m.start() - context) - : m.start()] + pywikibot.output(text[max(0, + m.start() - context): + m.start()] + '\03{lightred}' - + text[m.start() : m.end()] + + text[m.start():m.end()] + '\03{default}' - + text[m.end() : m.end() + context]) + + text[m.end():m.end() + context]) if edited: choice = pywikibot.input( u"Option (#, r#, [s]kip link, [e]dit page, [n]ext page, [u]nlink, [q]uit,\n" @@ -714,14 +721,14 @@ if disambPage.isRedirectPage(): disambredir = disambPage.getRedirectTarget() disambigText = editor.edit( - disambredir.get(), - jumpIndex=m.start(), - highlight=disambredir.title()) + disambredir.get(), + jumpIndex=m.start(), + highlight=disambredir.title()) else: disambigText = editor.edit( - disambPage.get(), - jumpIndex=m.start(), - highlight=disambPage.title()) + disambPage.get(), + jumpIndex=m.start(), + highlight=disambPage.title()) elif choice in ['l', 'L']: self.listAlternatives() elif choice in ['m', 'M']: @@ -738,7 +745,8 @@ elif choice in ['n', 'N']: # skip this page if self.primary: - # If run with the -primary argument, skip this occurence next time. + # If run with the -primary argument, skip this + # occurence next time. self.primaryIgnoreManager.ignore(refPage) return True elif choice in ['q', 'Q']: @@ -760,7 +768,7 @@ if not link_text: # or like this: [[page_title]]trailing_chars link_text = page_title - if m.group('section') == None: + if m.group('section') is None: section = '' else: section = m.group('section') @@ -769,9 +777,10 @@ link_text += trailing_chars # '?', '/' for old choice if choice in ['t', 'T', '?', '/']: - #small chunk of text to search - search_text = text[m.end() : m.end() + context] - #figure out where the link (and sentance) ends, put note there + # small chunk of text to search + search_text = text[m.end():m.end() + context] + # figure out where the link (and sentance) ends, put note + # there end_of_word_match = re.search("\s", search_text) if end_of_word_match: position_split = end_of_word_match.start(0) @@ -788,7 +797,7 @@ unlink = True continue else: - if len(choice)>0 and choice[0] == 'r': + if len(choice) > 0 and choice[0] == 'r': # we want to throw away the original link text replaceit = True choice = choice[1:] @@ -798,7 +807,7 @@ replaceit = False
try: - choice=int(choice) + choice = int(choice) except ValueError: pywikibot.output(u"Unknown option") # step back to ask the user again what to do with the @@ -807,8 +816,8 @@ continue if choice >= len(self.alternatives) or choice < 0: pywikibot.output( -u"Choice out of range. Please select a number between 0 and %i." - % (len(self.alternatives) - 1)) + u"Choice out of range. Please select a number " + u"between 0 and %i." % (len(self.alternatives) - 1)) # show list of possible choices self.listAlternatives() # step back to ask the user again what to do with the @@ -818,7 +827,7 @@ new_page_title = self.alternatives[choice] repPl = pywikibot.Page(disambPage.site(), new_page_title) if (new_page_title[0].isupper() - or link_text[0].isupper()): + or link_text[0].isupper()): new_page_title = repPl.title() else: new_page_title = repPl.title() @@ -850,7 +859,7 @@ text = text[:m.start()] + newlink + text[m.end():] continue
- pywikibot.output(text[max(0,m.start()-30):m.end()+30]) + pywikibot.output(text[max(0, m.start() - 30):m.end() + 30]) if text == original_text: pywikibot.output(u'\nNo changes have been made:\n') else: @@ -860,7 +869,7 @@ # save the page self.setSummaryMessage(disambPage, new_targets, unlink, dn) try: - refPage.put_async(text,comment=self.comment) + refPage.put_async(text, comment=self.comment) except pywikibot.LockedPage: pywikibot.output(u'Page not saved: page is locked') except pywikibot.PageNotSaved, error: @@ -871,20 +880,20 @@ if disambPage.isRedirectPage() and not self.primary: if (disambPage.site().lang in self.primary_redir_template and self.primary_redir_template[disambPage.site().lang] - in disambPage.templates(get_redirect = True)): + in disambPage.templates(get_redirect=True)): baseTerm = disambPage.title() for template in disambPage.templatesWithParams( - get_redirect=True): + get_redirect=True): if template[0] == self.primary_redir_template[ disambPage.site().lang] \ - and len(template[1]) > 0: + and len(template[1]) > 0: baseTerm = template[1][1] disambTitle = primary_topic_format[self.mylang] % baseTerm try: disambPage2 = pywikibot.Page( self.mysite, disambTitle) links = disambPage2.linkedPages() - links = [correctcap(l,disambPage2.get()) for l in links] + links = [correctcap(l, disambPage2.get()) for l in links] except pywikibot.NoPage: pywikibot.output(u"No page at %s, using redirect target." % disambTitle) @@ -959,22 +968,22 @@
# first check whether user has customized the edit comment if (self.mysite.family.name in pywikibot.config.disambiguation_comment - and self.mylang in pywikibot.config.disambiguation_comment - [self.mysite.family.name]): + and self.mylang in + pywikibot.config.disambiguation_comment[ + self.mysite.family.name]): try: self.comment = pywikibot.translate( - self.mysite, - pywikibot.config.disambiguation_comment[ - self.mysite.family.name] - ) % (disambPage.title(), targets) + self.mysite, + pywikibot.config.disambiguation_comment[ + self.mysite.family.name]) % (disambPage.title(), + targets) # Backwards compatibility, type error probably caused by too # many arguments for format string except TypeError: self.comment = pywikibot.translate( - self.mysite, - pywikibot.config.disambiguation_comment[ - self.mysite.family.name] - ) % disambPage.title() + self.mysite, + pywikibot.config.disambiguation_comment[ + self.mysite.family.name]) % disambPage.title() elif disambPage.isRedirectPage(): # when working on redirects, there's another summary message if unlink and not new_targets: @@ -1013,7 +1022,7 @@
for disambPage in self.generator: self.primaryIgnoreManager = PrimaryIgnoreManager( - disambPage, enabled=self.primary) + disambPage, enabled=self.primary)
if not self.findAlternatives(disambPage): continue @@ -1021,7 +1030,7 @@ self.makeAlternativesUnique() # sort possible choices if pywikibot.config.sort_ignore_case: - self.alternatives.sort(lambda x,y: cmp(x.lower(), y.lower())) + self.alternatives.sort(lambda x, y: cmp(x.lower(), y.lower())) else: self.alternatives.sort() self.listAlternatives() @@ -1037,6 +1046,7 @@
# clear alternatives before working on next disambiguation page self.alternatives = [] +
def main(*args): # the option that's always selected when the bot wonders what to do with @@ -1072,20 +1082,20 @@ elif arg.startswith('-file'): if len(arg) == 5: generator = pagegenerators.TextfilePageGenerator( - filename=None) + filename=None) else: generator = pagegenerators.TextfilePageGenerator( - filename=arg[6:]) + filename=arg[6:]) elif arg.startswith('-pos:'): - if arg[5]!=':': + if arg[5] != ':': mysite = pywikibot.getSite() page = pywikibot.Page(mysite, arg[5:]) if page.exists(): alternatives.append(page.title()) else: answer = pywikibot.inputChoice( - u'Possibility %s does not actually exist. Use it anyway?' - % page.title(), ['yes', 'no'], ['y', 'N'], 'N') + u'Possibility %s does not actually exist. Use it anyway?' + % page.title(), ['yes', 'no'], ['y', 'N'], 'N') if answer == 'y': alternatives.append(page.title()) else: @@ -1104,13 +1114,13 @@ try: if len(arg) <= len('-start:'): generator = pagegenerators.CategorizedPageGenerator( - pywikibot.getSite().disambcategory()) + pywikibot.getSite().disambcategory()) else: generator = pagegenerators.CategorizedPageGenerator( - pywikibot.getSite().disambcategory(), - start=arg[7:]) + pywikibot.getSite().disambcategory(), + start=arg[7:]) generator = pagegenerators.NamespaceFilterPageGenerator( - generator, [0]) + generator, [0]) except pywikibot.NoPage: print "Disambiguation category for your wiki is not known." raise
pywikibot-commits@lists.wikimedia.org