jenkins-bot has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/445856 )
Change subject: [cleanup] Code cleanup
......................................................................
[cleanup] Code cleanup
- keep lines below 80 chars
- remove preleading "u" from strings
- use single quotes
- use str.format() instead of modula operator when the line is changed
detached from
https://gerrit.wikimedia.org/r/#/c/pywikibot/core/+/444437/
Change-Id: I18d514a2f2707209220ddec9decc8421b28489f8
---
M scripts/casechecker.py
1 file changed, 99 insertions(+), 91 deletions(-)
Approvals:
Dalba: Looks good to me, approved
jenkins-bot: Verified
diff --git a/scripts/casechecker.py b/scripts/casechecker.py
index 6c50bc3..792b11b 100755
--- a/scripts/casechecker.py
+++ b/scripts/casechecker.py
@@ -34,46 +34,46 @@
# These words are always in one language, even though they could be typed
# in both
- alwaysInLocal = [u'СССР', u'Как', u'как']
- alwaysInLatin = [u'II', u'III']
+ alwaysInLocal = ['СССР', 'Как', 'как']
+ alwaysInLatin = ['II', 'III']
- localUpperLtr = u'ЁІЇЎАБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯҐ'
- localLowerLtr = u'ёіїўабвгдежзийклмнопрстуфхцчшщъыьэюяґ'
+ localUpperLtr = 'ЁІЇЎАБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯҐ'
+ localLowerLtr = 'ёіїўабвгдежзийклмнопрстуфхцчшщъыьэюяґ'
localLtr = localUpperLtr + localLowerLtr
- localSuspects = u'АВЕКМНОРСТХІЁЇаеорсухіёї'
- latinSuspects = u'ABEKMHOPCTXIËÏaeopcyxiëï'
+ localSuspects = 'АВЕКМНОРСТХІЁЇаеорсухіёї'
+ latinSuspects = 'ABEKMHOPCTXIËÏaeopcyxiëï'
# possibly try to fix one character mistypes in an alternative keyboard
# layout
- localKeyboard = u'йцукенгшщзфывапролдячсмить'
- latinKeyboard = u'qwertyuiopasdfghjklzxcvbnm'
+ localKeyboard = 'йцукенгшщзфывапролдячсмить'
+ latinKeyboard = 'qwertyuiopasdfghjklzxcvbnm'
- romanNumChars = u'IVXLCDM'
+ romanNumChars = 'IVXLCDM'
# all letters that may be used as suffixes after roman numbers: "Iый"
romannumSuffixes = localLowerLtr
romanNumSfxPtrn = re.compile(
- u'^[' + romanNumChars + ']+[' + localLowerLtr + ']+$')
+ '^[' + romanNumChars + ']+[' + localLowerLtr + ']+$')
whitelists = {
- 'ru': u'ВП:КЛ/Проверенные',
+ 'ru': 'ВП:КЛ/Проверенные',
}
- lclClrFnt = u'<font color=green>'
- latClrFnt = u'<font color=brown>'
- suffixClr = u'</font>'
+ lclClrFnt = '<font color=green>'
+ latClrFnt = '<font color=brown>'
+ suffixClr = '</font>'
colorFormatLocalColor = '{green}'
colorFormatLatinColor = '{red}'
colorFormatSuffix = '{default}'
wordBreaker = re.compile(r'[ _\-/\|#[\]():]')
- stripChars = u' \t,'
+ stripChars = ' \t,'
titles = True
links = False
aplimit = None
- apfrom = u''
+ apfrom = ''
title = None
replace = False
stopAfter = -1
@@ -120,12 +120,12 @@
elif arg == '-failed':
self.doFailed = True
else:
- pywikibot.output(u'Unknown argument %s.' % arg)
+ pywikibot.output('Unknown argument {}.'.format(arg))
pywikibot.showHelp()
sys.exit()
if self.namespaces == [] and not self.doFailed:
- if self.apfrom == u'':
+ if self.apfrom == '':
# 0 should be after templates ns
self.namespaces = [14, 10, 12, 0]
else:
@@ -141,8 +141,8 @@
'gapfilterredir': self.filterredir}
else:
self.queryParams = {'action': 'query'}
- if self.apfrom != u'':
- pywikibot.output(u'Argument "-from" is ignored with
"-failed"')
+ if self.apfrom != '':
+ pywikibot.output('Argument "-from" is ignored with
"-failed"')
propParam = 'info'
if self.links:
@@ -155,16 +155,17 @@
self.site = pywikibot.Site()
if len(self.localSuspects) != len(self.latinSuspects):
- raise ValueError(u'Suspects must be the same size')
+ raise ValueError('Suspects must be the same size')
if len(self.localKeyboard) != len(self.latinKeyboard):
- raise ValueError(u'Keyboard info must be the same size')
+ raise ValueError('Keyboard info must be the same size')
if not os.path.isabs(self.wikilogfile):
self.wikilogfile = pywikibot.config.datafilepath(self.wikilogfile)
self.wikilog = self.OpenLogFile(self.wikilogfile)
if not os.path.isabs(self.failedTitles):
- self.failedTitles = pywikibot.config.datafilepath(self.failedTitles)
+ self.failedTitles = pywikibot.config.datafilepath(
+ self.failedTitles)
if self.doFailed:
with codecs.open(self.failedTitles, 'r', 'utf-8') as f:
@@ -187,13 +188,12 @@
self.lclToLatKeybDict = {}
self.latToLclKeybDict = {}
- badPtrnStr = u'([%s][%s]|[%s][%s])' \
- % (ascii_letters, self.localLtr,
- self.localLtr, ascii_letters)
- self.badWordPtrn = re.compile(u'[%s%s]*%s[%s%s]*'
- % (ascii_letters, self.localLtr,
- badPtrnStr, ascii_letters,
- self.localLtr))
+ badPtrnStr = '([{ascii}][{local}]|[{local}][{ascii}])'.format(
+ ascii=ascii_letters, local=self.localLtr)
+ self.badWordPtrn = re.compile('[{ascii}{local}]*{bad}[{ascii}{local}]*'
+ .format(ascii=ascii_letters,
+ local=self.localLtr,
+ bad=badPtrnStr))
# Get whitelist
self.knownWords = set()
@@ -202,7 +202,7 @@
# TODO: handle "continue"
if self.site.code in self.whitelists:
wlpage = self.whitelists[self.site.code]
- pywikibot.output(u'Loading whitelist from %s' % wlpage)
+ pywikibot.output('Loading whitelist from {}'.format(wlpage))
wlparams = {
'action': 'query',
'prop': 'links',
@@ -223,16 +223,16 @@
self.knownWords = set(allWords)
else:
- raise ValueError(u'The number of pageids is not 1')
+ raise ValueError('The number of pageids is not 1')
- pywikibot.output(u'Loaded whitelist with %i items'
+ pywikibot.output('Loaded whitelist with %i items'
% len(self.knownWords))
if len(self.knownWords) > 0:
- pywikibot.log(u'Whitelist: %s'
- % u', '.join([self.MakeLink(i, False)
- for i in self.knownWords]))
+ pywikibot.log('Whitelist: '
+ + ', '.join([self.MakeLink(i, False)
+ for i in self.knownWords]))
else:
- pywikibot.output(u'Whitelist is not known for language %s'
+ pywikibot.output('Whitelist is not known for language %s'
% self.site.code)
def RunQuery(self, params):
@@ -266,7 +266,8 @@
elif 'allpages' in qc:
params.update(qc['allpages'])
else:
- raise ValueError(u'Unexpected query-continue values: %s' % qc)
+ raise ValueError('Unexpected query-continue values: {}'
+ .format(qc))
continue
def Run(self):
@@ -290,13 +291,13 @@
for data in self.RunQuery(self.queryParams):
self.ProcessDataBlock(data)
except Exception:
- pywikibot.output(u'Exception at Title = %s, Next = %s'
+ pywikibot.output('Exception at Title = %s, Next = %s'
% (self.currentTitle, self.apfrom))
try:
import traceback
pywikibot.output(traceback.format_exc())
except Exception:
- pywikibot.output(u'Unable to print exception info')
+ pywikibot.output('Unable to print exception info')
raise
def ProcessDataBlock(self, data):
@@ -353,12 +354,14 @@
replErrors = True
if not replErrors:
editSummary = i18n.twtranslate(
- self.site,
"casechecker-delete-summary")
+ self.site,
+ 'casechecker-delete-summary')
newText = i18n.twtranslate(
self.site,
- "casechecker-delete-reason",
redirTitle)
+ 'casechecker-delete-reason',
+ redirTitle)
if newText:
- src.text = u'{{delete}}\n\n' + newText
+ src.text = '{{delete}}\n\n' + newText
src.save(editSummary, minor=False)
changed = True
@@ -370,7 +373,7 @@
src.title(with_ns=False),
dst.title(with_ns=False),
self.autonomous,
- editSummary + u' ' +
+ editSummary + ' ' +
self.MakeMoveSummary(title, newTitle),
True)
bot.run()
@@ -385,7 +388,7 @@
else:
self.AddNoSuggestionTitle(title)
- self.WikiLog(u"* " + err[0])
+ self.WikiLog('* ' + err[0])
printed = True
if self.links:
@@ -428,12 +431,14 @@
newTitle)
if not newTitle:
if not printed:
- self.WikiLog(u"* %s: link to %s"
- % (self.MakeLink(title, False),
- err[0]))
+ self.WikiLog('* {}: link to {}'
+ .format(self.MakeLink(title,
+ False),
+ err[0]))
printed = True
else:
- self.WikiLog(u"** link to %s" % err[0])
+ self.WikiLog('** link to {}'
+ .format(err[0]))
if pageObj is not None:
if self.PutNewPage(pageObj, pageTxt, msg):
# done, no need to log anything
@@ -444,12 +449,12 @@
if self.stopAfter > 0:
self.stopAfter -= 1
if self.stopAfter == 0:
- raise ValueError(u'Stopping because we are done')
+ raise ValueError('Stopping because we are done')
def WikiLog(self, text):
"""Write log."""
pywikibot.output(text)
- self.wikilog.write(text + u'\n')
+ self.wikilog.write(text + '\n')
self.wikilog.flush()
def FindBadWords(self, title):
@@ -486,7 +491,7 @@
if mightBeLcl and l not in self.latinSuspects:
mightBeLcl = False
if l not in ascii_letters:
- raise ValueError(u'Assert failed')
+ raise ValueError('Assert failed')
# Some words are well known and frequently mixed-typed
if mightBeLcl and mightBeLat:
@@ -521,8 +526,8 @@
kw = [w for w in kw if p < len(w) and
(w[p] == c or (c2 is not None and w[p] == c2))]
if len(kw) > 1:
- pywikibot.output(u"Word '%s' could be treated as more
than "
- u"one known words" % badWord)
+ pywikibot.output("Word '{}' could be treated as more
than "
+ 'one known words'.format(badWord))
elif len(kw) == 1:
mapLcl[badWord] = kw[0]
count += 1
@@ -534,10 +539,10 @@
# We cannot auto-translate - offer a list of suggested words
suggestions = list(mapLcl.values()) + list(mapLat.values())
if len(suggestions) > 0:
- infoText += u", word suggestions: " + u', '.join(
+ infoText += ', word suggestions: ' + ', '.join(
[self.ColorCodeWord(t) for t in suggestions])
else:
- infoText += u", no suggestions"
+ infoText += ', no suggestions'
else:
# Replace all unambiguous bad words
@@ -547,7 +552,7 @@
if len(ambigBadWords) == 0:
# There are no ambiguity, we can safelly convert
possibleAlternatives.append(title)
- infoText += u", convert to " + self.MakeLink(title)
+ infoText += ', convert to ' + self.MakeLink(title)
else:
# Try to pick 0, 1, 2, ..., len(ambiguous words) unique
# combinations from the bad words list, and convert just the
@@ -566,10 +571,10 @@
possibleAlternatives.append(title2)
if len(possibleAlternatives) > 0:
- infoText += u", can be converted to " + u',
'.join(
+ infoText += ', can be converted to ' + ', '.join(
[self.MakeLink(t) for t in possibleAlternatives])
else:
- infoText += u", no suggestions"
+ infoText += ', no suggestions'
return (infoText, possibleAlternatives)
def PickTarget(self, title, original, candidates):
@@ -608,22 +613,21 @@
return list(pagesRedir.keys())[0]
if not self.autonomous:
- pywikibot.output(u'Could not auto-decide for page %s. Which link '
- u'should be chosen?' % self.MakeLink(title, False))
- pywikibot.output(u'Original title: ', newline=False)
- self.ColorCodeWord(original + "\n", True)
- count = 1
- for t in candidates:
+ pywikibot.output('Could not auto-decide for page %s. Which link '
+ 'should be chosen?' % self.MakeLink(title, False))
+ pywikibot.output('Original title: ', newline=False)
+ self.ColorCodeWord(original + '\n', True)
+ for count, t in enumerate(candidates, 1):
if t in pagesDontExist:
- msg = u'missing'
+ msg = 'missing'
elif t in pagesRedir:
- msg = u'Redirect to ' + pagesRedir[t]
+ msg = 'Redirect to ' + pagesRedir[t]
else:
- msg = u'page exists'
- self.ColorCodeWord(u' %d: %s (%s)\n' % (count, t, msg), True)
- count += 1
+ msg = 'page exists'
+ self.ColorCodeWord(' {}: {} ({})\n'.format(count, t, msg),
+ True)
answers = [('skip', 's')] + [(str(i), i) for i in range(1,
count)]
- choice = pywikibot.input_choice(u'Which link to choose?', answers)
+ choice = pywikibot.input_choice('Which link to choose?', answers)
if choice != 's':
return candidates[int(choice) - 1]
@@ -699,45 +703,47 @@
else:
count = str(cl if cl > 0 else 'no backlinks')
- self.AppendLineToLog(self.nosuggestions, u'* %s (%s%s)'
- % (self.MakeLink(title), count, u', %d redirects'
- % redirs if redirs > 0 else u''))
+ self.AppendLineToLog(self.nosuggestions, '* {} ({}{})'
+ .format(self.MakeLink(title), count,
+ ', {} redirects'
+ .format(redirs if redirs > 0 else '')))
return False
def PutNewPage(self, pageObj, pageTxt, msg):
"""Save new page."""
title = pageObj.title(as_link=True, textlink=True)
- coloredMsg = u', '.join([self.ColorCodeWord(m) for m in msg])
+ coloredMsg = ', '.join([self.ColorCodeWord(m) for m in msg])
if pageObj.text == pageTxt:
- self.WikiLog(u"* Error: Text replacement failed in %s (%s)"
+ self.WikiLog('* Error: Text replacement failed in %s (%s)'
% (self.MakeLink(title, False), coloredMsg))
else:
- pywikibot.output(u'Case Replacements: %s' % u', '.join(msg))
+ pywikibot.output('Case Replacements: {}'.format(',
'.join(msg)))
pageObj.text = pageTxt
try:
pageObj.save(
- u'%s: %s'
- % (i18n.twtranslate(
- self.site,
- "casechecker-replacement-summary"),
-
self.site.mediawiki_message(u"comma-separator").join(msg)))
+ '{}: {}'.format(
+ i18n.twtranslate(
+ self.site, 'casechecker-replacement-summary'),
+ self.site.mediawiki_message(
+ 'comma-separator').join(msg)))
return True
except KeyboardInterrupt:
raise
except (pywikibot.LockedPage, pywikibot.PageNotSaved):
- self.WikiLog(u"* Error: Could not save updated page %s (%s)"
+ self.WikiLog('* Error: Could not save updated page %s (%s)'
% (self.MakeLink(title, False), coloredMsg))
return False
def MakeMoveSummary(self, fromTitle, toTitle):
"""Move summary from i18n."""
- return i18n.twtranslate(self.site, "casechecker-replacement-linklist",
+ return i18n.twtranslate(self.site, 'casechecker-replacement-linklist',
{'source': fromTitle, 'target':
toTitle})
def MakeLink(self, title, colorcode=True):
"""Create a colored link string."""
- prf = u'' if self.Page(title).namespace() == 0 else u':'
- cc = u'|««« %s »»»' % self.ColorCodeWord(title) if colorcode else
u''
+ prf = '' if self.Page(title).namespace() == 0 else ':'
+ cc = '|««« {} »»»'.format(
+ self.ColorCodeWord(title) if colorcode else '')
return u"[[%s%s%s]]" % (prf, title, cc)
def OpenLogFile(self, filename):
@@ -750,7 +756,7 @@
def AppendLineToLog(self, filename, text):
"""Write text to logfile."""
with self.OpenLogFile(filename) as f:
- f.write(text + u'\n')
+ f.write(text + '\n')
def Page(self, title):
"""Create Page object from title."""
@@ -764,13 +770,15 @@
for s in self.wordBreaker.split(newtxt)]
if len(frmParts) != len(toParts):
- raise ValueError(u'Splitting parts do not match counts')
+ raise ValueError('Splitting parts do not match counts')
for i in xrange(0, len(frmParts)):
if len(frmParts[i]) != len(toParts[i]):
- raise ValueError(u'Splitting parts do not match word length')
+ raise ValueError('Splitting parts do not match word length')
if len(frmParts[i]) > 0:
- text = text.replace(first_lower(frmParts[i]), first_lower(toParts[i]))
- text = text.replace(first_upper(frmParts[i]), first_upper(toParts[i]))
+ text = text.replace(first_lower(frmParts[i]),
+ first_lower(toParts[i]))
+ text = text.replace(first_upper(frmParts[i]),
+ first_upper(toParts[i]))
return text
--
To view, visit
https://gerrit.wikimedia.org/r/445856
To unsubscribe, or for help writing mail filters, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: I18d514a2f2707209220ddec9decc8421b28489f8
Gerrit-Change-Number: 445856
Gerrit-PatchSet: 3
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: Dalba <dalba.wiki(a)gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: Zoranzoki21 <zorandori4444(a)gmail.com>
Gerrit-Reviewer: jenkins-bot