Xqt has uploaded a new change for review.
https://gerrit.wikimedia.org/r/92015
Change subject: pep8 changes
......................................................................
pep8 changes
Change-Id: Id82074241828db3a96e25659ea45e368de7edff8
---
M category.py
1 file changed, 136 insertions(+), 89 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/pywikibot/compat
refs/changes/15/92015/1
diff --git a/category.py b/category.py
index b9471a1..d03f15d 100644
--- a/category.py
+++ b/category.py
@@ -97,9 +97,14 @@
# Distributed under the terms of the MIT license.
#
-import os, re, pickle, bz2
+import os
+import re
+import pickle
+import bz2
import wikipedia as pywikibot
-import catlib, config, pagegenerators
+import catlib
+import config
+import pagegenerators
from pywikibot import i18n
# This is required for the text that is shown when you run this script
@@ -109,14 +114,15 @@
}
cfd_templates = {
- 'wikipedia' : {
- 'en':[u'cfd', u'cfr', u'cfru', u'cfr-speedy', u'cfm', u'cfdu'],
- 'fi':[u'roskaa', u'poistettava', u'korjattava/nimi',
u'yhdistettäväLuokka'],
- 'he':[u'הצבעת מחיקה', u'למחוק'],
- 'nl':[u'categorieweg', u'catweg', u'wegcat', u'weg2']
+ 'wikipedia': {
+ 'en': [u'cfd', u'cfr', u'cfru', u'cfr-speedy', u'cfm', u'cfdu'],
+ 'fi': [u'roskaa', u'poistettava', u'korjattava/nimi',
+ u'yhdistettäväLuokka'],
+ 'he': [u'הצבעת מחיקה', u'למחוק'],
+ 'nl': [u'categorieweg', u'catweg', u'wegcat', u'weg2']
},
- 'commons' : {
- 'commons':[u'cfd', u'move']
+ 'commons': {
+ 'commons': [u'cfd', u'move']
}
}
@@ -127,7 +133,7 @@
over and over again
'''
- def __init__(self, rebuild = False, filename = 'category.dump.bz2'):
+ def __init__(self, rebuild=False, filename='category.dump.bz2'):
if rebuild:
self.rebuild()
else:
@@ -149,8 +155,8 @@
self.rebuild()
def rebuild(self):
- self.catContentDB={}
- self.superclassDB={}
+ self.catContentDB = {}
+ self.superclassDB = {}
def getSubcats(self, supercat):
'''For a given supercategory, return a list of Categorys for all its
@@ -194,7 +200,7 @@
self.superclassDB[subcat] = supercatlist
return supercatlist
- def dump(self, filename = 'category.dump.bz2'):
+ def dump(self, filename='category.dump.bz2'):
'''Saves the contents of the dictionaries superclassDB and catContentDB
to disk.
@@ -264,8 +270,7 @@
# pull last part of the name to the beginning, and append the
# rest after a comma; e.g., "John von Neumann" becomes
# "Neumann, John von"
- sorted_key = split_string[-1] + ', ' + \
- ' '.join(split_string[:-1])
+ sorted_key = split_string[-1] + ', ' + ' '.join(split_string[:-1])
# give explicit sort key
return pywikibot.Page(site, catlink.title() + '|' + sorted_key)
else:
@@ -275,8 +280,8 @@
self.newcatTitle = pywikibot.input(
u'Category to add (do not give namespace):')
if not self.site.nocapitalize:
- self.newcatTitle = self.newcatTitle[:1].upper() + \
- self.newcatTitle[1:]
+ self.newcatTitle = (self.newcatTitle[:1].upper() +
+ self.newcatTitle[1:])
if not self.editSummary:
self.editSummary = i18n.twtranslate(self.site, 'category-adding',
{'newcat': self.newcatTitle})
@@ -315,7 +320,7 @@
if text != page.get():
# show what was changed
pywikibot.showDiff(page.get(), text)
- pywikibot.output(u'Comment: %s' %comment)
+ pywikibot.output(u'Comment: %s' % comment)
if not self.dry:
if not self.always:
confirm = 'y'
@@ -330,7 +335,8 @@
if confirm == 'y':
self.always = True
break
- else: break
+ else:
+ break
if self.always or choice == 'y':
try:
# Save the page
@@ -406,21 +412,21 @@
if self.useSummaryForDeletion and self.editSummary:
reason = self.editSummary
else:
- reason = i18n.twtranslate(site, 'category-was-moved') \
- % {'newcat': self.newCatTitle, 'title': self.newCatTitle}
-
+ reason = i18n.twtranslate(site, 'category-was-moved',
+ {'newcat': self.newCatTitle,
+ 'title': self.newCatTitle})
+
if not self.editSummary:
- self.editSummary = i18n.twtranslate(site, 'category-changing') \
- % {'oldcat':self.oldCat.title(),
- 'newcat':newCat.title()}
+ self.editSummary = i18n.twtranslate(site, 'category-changing',
+ {'oldcat': self.oldCat.title(),
+ 'newcat': newCat.title()})
# Copy the category contents to the new category page
copied = False
oldMovedTalk = None
if self.oldCat.exists() and self.moveCatPage:
copied = self.oldCat.copyAndKeep(
- self.newCatTitle,
- pywikibot.translate(site, cfd_templates))
+ self.newCatTitle, pywikibot.translate(site, cfd_templates))
# Also move the talk page
if copied:
oldTalk = self.oldCat.toggleTalkPage()
@@ -486,12 +492,12 @@
inPlace=self.inPlace)
# Delete the old category and its moved talk page
- if copied and self.deleteEmptySourceCat == True:
+ if copied and self.deleteEmptySourceCat:
if self.oldCat.isEmptyCategory():
confirm = not self.batchMode
- self.oldCat.delete(reason, confirm, mark = True)
+ self.oldCat.delete(reason, confirm, mark=True)
if oldMovedTalk is not None:
- oldMovedTalk.delete(reason, confirm, mark = True)
+ oldMovedTalk.delete(reason, confirm, mark=True)
else:
pywikibot.output('Couldn\'t delete %s - not empty.'
% self.oldCat.title())
@@ -499,7 +505,9 @@
class CategoryListifyRobot:
'''Creates a list containing all of the members in a category.'''
- def __init__(self, catTitle, listTitle, editSummary, overwrite = False,
showImages = False, subCats = False, talkPages = False, recurse = False):
+ def __init__(self, catTitle, listTitle, editSummary, overwrite=False,
+ showImages=False, subCats=False, talkPages=False,
+ recurse=False):
self.editSummary = editSummary
self.overwrite = overwrite
self.showImages = showImages
@@ -511,7 +519,7 @@
self.recurse = recurse
def run(self):
- listOfArticles = self.cat.articlesList(recurse = self.recurse)
+ listOfArticles = self.cat.articlesList(recurse=self.recurse)
if self.subCats:
listOfArticles += self.cat.subcategoriesList()
if not self.editSummary:
@@ -522,18 +530,24 @@
listString = ""
for article in listOfArticles:
- if (not article.isImage() or self.showImages) and not
article.isCategory():
+ if (not article.isImage() or
+ self.showImages) and not article.isCategory():
if self.talkPages and not article.isTalkPage():
- listString = listString + "*[[%s]] -- [[%s|talk]]\n" %
(article.title(), article.toggleTalkPage().title())
+ listString += "*[[%s]] -- [[%s|talk]]\n" \
+ % (article.title(),
+ article.toggleTalkPage().title())
else:
- listString = listString + "*[[%s]]\n" % article.title()
+ listString += "*[[%s]]\n" % article.title()
else:
if self.talkPages and not article.isTalkPage():
- listString = listString + "*[[:%s]] -- [[%s|talk]]\n" %
(article.title(), article.toggleTalkPage().title())
+ listString += "*[[:%s]] -- [[%s|talk]]\n" \
+ % (article.title(),
+ article.toggleTalkPage().title())
else:
- listString = listString + "*[[:%s]]\n" % article.title()
+ listString += "*[[:%s]]\n" % article.title()
if self.list.exists() and not self.overwrite:
- pywikibot.output(u'Page %s already exists, aborting.' %
self.list.title())
+ pywikibot.output(u'Page %s already exists, aborting.'
+ % self.list.title())
else:
self.list.put(listString, comment=self.editSummary)
@@ -552,7 +566,7 @@
pagesonly=False):
self.editSummary = editSummary
self.site = pywikibot.getSite()
- self.cat = catlib.Category(self.site, 'Category:'+ catTitle)
+ self.cat = catlib.Category(self.site, 'Category:' + catTitle)
# get edit summary message
self.useSummaryForDeletion = useSummaryForDeletion
self.batchMode = batchMode
@@ -564,23 +578,30 @@
{'oldcat': self.cat.title()})
def run(self):
- articles = self.cat.articlesList(recurse = 0)
+ articles = self.cat.articlesList(recurse=0)
if len(articles) == 0:
- pywikibot.output(u'There are no articles in category %s' %
self.cat.title())
+ pywikibot.output(u'There are no articles in category %s'
+ % self.cat.title())
else:
for article in articles:
- if not self.titleRegex or
re.search(self.titleRegex,article.title()):
- catlib.change_category(article, self.cat, None, comment =
self.editSummary, inPlace = self.inPlace)
+ if not self.titleRegex or re.search(self.titleRegex,
+ article.title()):
+ catlib.change_category(article, self.cat, None,
+ comment=self.editSummary,
+ inPlace=self.inPlace)
if self.pagesonly:
return
# Also removes the category tag from subcategories' pages
- subcategories = self.cat.subcategoriesList(recurse = 0)
+ subcategories = self.cat.subcategoriesList(recurse=0)
if len(subcategories) == 0:
- pywikibot.output(u'There are no subcategories in category %s' %
self.cat.title())
+ pywikibot.output(u'There are no subcategories in category %s'
+ % self.cat.title())
else:
for subcategory in subcategories:
- catlib.change_category(subcategory, self.cat, None, comment =
self.editSummary, inPlace = self.inPlace)
+ catlib.change_category(subcategory, self.cat, None,
+ comment=self.editSummary,
+ inPlace=self.inPlace)
# Deletes the category page
if self.cat.exists() and self.cat.isEmptyCategory():
if self.useSummaryForDeletion and self.editSummary:
@@ -591,7 +612,9 @@
try:
self.cat.delete(reason, not self.batchMode)
except pywikibot.NoUsername:
- pywikibot.output(u'You\'re not setup sysop info, category will
not delete.' % self.cat.site())
+ pywikibot.output(
+ u'You\'re not setup sysop info, category will not delete.'
+ % self.cat.site())
return
if (talkPage.exists()):
talkPage.delete(reason=reason, prompt=not self.batchMode)
@@ -626,7 +649,7 @@
self.catDB = catDB
self.site = pywikibot.getSite()
self.editSummary = i18n.twtranslate(self.site, 'category-changing')\
- % {'oldcat':self.catTitle, 'newcat':u''}
+ % {'oldcat': self.catTitle, 'newcat': u''}
def move_to_category(self, article, original_cat, current_cat):
'''
@@ -639,21 +662,24 @@
pywikibot.output(u'')
# Show the title of the page where the link was found.
# Highlight the title in purple.
- pywikibot.output(u'Treating page \03{lightpurple}%s\03{default},
currently in \03{lightpurple}%s\03{default}' % (article.title(),
current_cat.title()))
+ pywikibot.output(
+ u'Treating page \03{lightpurple}%s\03{default}, '
+ u'currently in \03{lightpurple}%s\03{default}'
+ % (article.title(), current_cat.title()))
# Determine a reasonable amount of context to print
try:
- full_text = article.get(get_redirect = True)
+ full_text = article.get(get_redirect=True)
except pywikibot.NoPage:
pywikibot.output(u'Page %s not found.' % article.title())
return
try:
contextLength = full_text.index('\n\n')
- except ValueError: # substring not found
+ except ValueError: # substring not found
contextLength = 500
- if full_text.startswith(u'[['): # probably an image
+ if full_text.startswith(u'[['): # probably an image
# Add extra paragraph.
- contextLength = full_text.find('\n\n', contextLength+2)
+ contextLength = full_text.find('\n\n', contextLength + 2)
if contextLength > 1000 or contextLength < 0:
contextLength = 500
print
@@ -670,10 +696,12 @@
# show subcategories as possible choices (with numbers)
for i in range(len(supercatlist)):
# layout: we don't expect a cat to have more than 10 supercats
- alternatives += (u"u%d - Move up to %s\n" % (i,
supercatlist[i].title()))
+ alternatives += (u"u%d - Move up to %s\n"
+ % (i, supercatlist[i].title()))
for i in range(len(subcatlist)):
# layout: we don't expect a cat to have more than 100 subcats
- alternatives += (u"%2d - Move down to %s\n" % (i,
subcatlist[i].title()))
+ alternatives += (u"%2d - Move down to %s\n"
+ % (i, subcatlist[i].title()))
alternatives += u" j - Jump to another category\n"
alternatives += u" s - Skip this article\n"
alternatives += u" r - Remove this category tag\n"
@@ -688,7 +716,9 @@
pywikibot.output(alternatives)
choice = pywikibot.input(u"Option:")
else:
- choice = pywikibot.input(u"Option (#, [j]ump, [s]kip,
[r]emove, [l]ist, [m]ore context, [RETURN]):")
+ choice = pywikibot.input(
+ u"Option (#, [j]ump, [s]kip, [r]emove, [l]ist, "
+ u"[m]ore context, [RETURN]):")
if choice in ['s', 'S']:
flag = True
elif choice == '':
@@ -697,30 +727,42 @@
print 'No changes necessary.'
else:
newcat = u'[[:%s|%s]]' %
(current_cat.title(savetitle=True),
-
current_cat.title(withNamespace=False))
- editsum = i18n.twtranslate(pywikibot.getSite(),
- 'category-replacing',
- {'oldcat':
original_cat.title(withNamespace=False),
- 'newcat': newcat})
+ current_cat.title(
+ withNamespace=False))
+ editsum = i18n.twtranslate(
+ pywikibot.getSite(), 'category-replacing',
+ {'oldcat': original_cat.title(withNamespace=False),
+ 'newcat': newcat})
if pywikibot.getSite().family.name == "commons":
- if
original_cat.title(withNamespace=False).startswith("Media needing categories as
of"):
+ if original_cat.title(withNamespace=False).startswith(
+ "Media needing categories as of"):
parts = original_cat.title().split()
- catstring =
u"{{Uncategorized|year=%s|month=%s|day=%s}}"%(parts[-1], parts[-2], parts[-3])
+ catstring =
u"{{Uncategorized|year=%s|month=%s|day=%s}}" \
+ % (parts[-1], parts[-2], parts[-3])
if catstring in article.get():
- article.put(article.get().replace(catstring,
u"[[%s]]"%current_cat.title(savetitle=True)), comment = editsum)
+ article.put(
+ article.get().replace(catstring,
+ u"[[%s]]"
+ % current_cat.title(
+ savetitle=True)),
+ comment=editsum)
flag = True
if not flag:
- catlib.change_category(article, original_cat,
current_cat, comment = editsum)
+ catlib.change_category(article, original_cat,
+ current_cat, comment=editsum)
flag = True
elif choice in ['j', 'J']:
- newCatTitle = pywikibot.input(u'Please enter the category the
article should be moved to:')
- newCat = catlib.Category(pywikibot.getSite(), 'Category:' +
newCatTitle)
+ newCatTitle = pywikibot.input(
+ u'Please enter the category the article should be moved
to:')
+ newCat = catlib.Category(pywikibot.getSite(),
+ 'Category:' + newCatTitle)
# recurse into chosen category
self.move_to_category(article, original_cat, newCat)
flag = True
elif choice in ['r', 'R']:
# remove the category tag
- catlib.change_category(article, original_cat, None, comment =
self.editSummary)
+ catlib.change_category(article, original_cat, None,
+ comment=self.editSummary)
flag = True
elif choice in ['l', 'L']:
longchoice = True
@@ -739,15 +781,16 @@
pywikibot.output(u'* %s' % cat.title())
elif choice[0] == 'u':
try:
- choice=int(choice[1:])
+ choice = int(choice[1:])
except ValueError:
# user pressed an unknown command. Prompt him again.
continue
- self.move_to_category(article, original_cat,
supercatlist[choice])
+ self.move_to_category(article, original_cat,
+ supercatlist[choice])
flag = True
else:
try:
- choice=int(choice)
+ choice = int(choice)
except ValueError:
# user pressed an unknown command. Prompt him again.
continue
@@ -758,7 +801,7 @@
def run(self):
cat = catlib.Category(self.site, 'Category:' + self.catTitle)
- articles = cat.articlesList(recurse = False)
+ articles = cat.articlesList(recurse=False)
if len(articles) == 0:
pywikibot.output(u'There are no articles in category ' + catTitle)
else:
@@ -783,7 +826,7 @@
the tree to stdout.
'''
- def __init__(self, catTitle, catDB, filename = None, maxDepth = 10):
+ def __init__(self, catTitle, catDB, filename=None, maxDepth=10):
self.catTitle = catTitle
self.catDB = catDB
if filename and not os.path.isabs(filename):
@@ -793,7 +836,7 @@
self.maxDepth = maxDepth
self.site = pywikibot.getSite()
- def treeview(self, cat, currentDepth = 0, parent = None):
+ def treeview(self, cat, currentDepth=0, parent=None):
'''
Returns a multi-line string which contains a tree view of all
subcategories
of cat, up to level maxDepth. Recursively calls itself.
@@ -807,8 +850,8 @@
result = u'#' * currentDepth
result += '[[:%s|%s]]' % (cat.title(), cat.title().split(':', 1)[1])
result += ' (%d)' % len(self.catDB.getArticles(cat))
- # We will remove an element of this array, but will need the original
array
- # later, so we create a shallow copy with [:]
+ # We will remove an element of this array, but will need the original
+ # array later, so we create a shallow copy with [:]
supercats = self.catDB.getSupercats(cat)[:]
# Find out which other cats are supercats of the current cat
try:
@@ -819,15 +862,19 @@
supercat_names = []
for i in range(len(supercats)):
# create a list of wiki links to the supercategories
- supercat_names.append('[[:%s|%s]]' % (supercats[i].title(),
supercats[i].title().split(':', 1)[1]))
- # print this list, separated with commas, using translations
given in also_in_cats
+ supercat_names.append('[[:%s|%s]]'
+ % (supercats[i].title(),
+ supercats[i].title().split(':',
1)[1]))
+ # print this list, separated with commas, using translations
+ # given in also_in_cats
result += ' ' + i18n.twtranslate(self.site, 'category-also-in',
- {'alsocat': ',
'.join(supercat_names)})
+ {'alsocat': ', '.join(
+ supercat_names)})
result += '\n'
if currentDepth < self.maxDepth:
for subcat in self.catDB.getSubcats(cat):
# recurse into subdirectories
- result += self.treeview(subcat, currentDepth + 1, parent = cat)
+ result += self.treeview(subcat, currentDepth + 1, parent=cat)
else:
if self.catDB.getSubcats(cat) != []:
# show that there are more categories beyond the depth limit
@@ -852,7 +899,7 @@
f.write(tree)
f.close()
else:
- pywikibot.output(tree, toStdout = True)
+ pywikibot.output(tree, toStdout=True)
def main(*args):
@@ -900,7 +947,7 @@
action = 'tree'
elif arg == 'listify':
action = 'listify'
- elif arg =='-nodelete':
+ elif arg == '-nodelete':
deleteEmptySourceCat = False
elif arg == '-person':
sort_by_last_name = True
@@ -960,7 +1007,7 @@
bot = AddCategory(gen, sort_by_last_name, create_pages, editSummary)
bot.run()
elif action == 'remove':
- if (fromGiven == False):
+ if not fromGiven:
oldCatTitle = pywikibot.input(
u'Please enter the name of the category that should be removed:')
bot = CategoryRemoveRobot(oldCatTitle, batchMode, editSummary,
@@ -968,10 +1015,10 @@
pagesonly=pagesonly)
bot.run()
elif action == 'move':
- if (fromGiven == False):
+ if not fromGiven:
oldCatTitle = pywikibot.input(
u'Please enter the old name of the category:')
- if (toGiven == False):
+ if not toGiven:
newCatTitle = pywikibot.input(
u'Please enter the new name of the category:')
bot = CategoryMoveRobot(oldCatTitle, newCatTitle, batchMode,
@@ -987,15 +1034,15 @@
catTitle = pywikibot.input(
u'For which category do you want to create a tree view?')
filename = pywikibot.input(
- u'Please enter the name of the file where the tree should be
saved,\n'
- u'or press enter to simply show the tree:')
+ u'Please enter the name of the file where the tree should be
saved,'
+ u'\nor press enter to simply show the tree:')
bot = CategoryTreeRobot(catTitle, catDB, filename)
bot.run()
elif action == 'listify':
- if (fromGiven == False):
+ if not fromGiven:
oldCatTitle = pywikibot.input(
u'Please enter the name of the category to listify:')
- if (toGiven == False):
+ if not toGiven:
newCatTitle = pywikibot.input(
u'Please enter the name of the list to create:')
bot = CategoryListifyRobot(oldCatTitle, newCatTitle, editSummary,
--
To view, visit https://gerrit.wikimedia.org/r/92015
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Id82074241828db3a96e25659ea45e368de7edff8
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/compat
Gerrit-Branch: master
Gerrit-Owner: Xqt <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits