jenkins-bot has submitted this change and it was merged.
Change subject: [FEAT/FIX] Use input_choice or new input_yn
......................................................................
[FEAT/FIX] Use input_choice or new input_yn
As inputChoice has been deprecated this updates all references to the
replacement (input_choice).
Also added input_yn which is like input_choice but returns a boolean and
automatically sets the answers to [y]es and [n]o.
Change-Id: Ib68e66a60e5bc5a78602253123312a380109657d
---
M generate_user_files.py
M pywikibot/__init__.py
M pywikibot/bot.py
M pywikibot/diff.py
M pywikibot/page.py
M scripts/add_text.py
M scripts/basic.py
M scripts/blockpageschecker.py
M scripts/blockreview.py
M scripts/capitalize_redirects.py
M scripts/casechecker.py
M scripts/cosmetic_changes.py
M scripts/interwiki.py
M scripts/maintenance/compat2core.py
M scripts/movepages.py
M scripts/nowcommons.py
M scripts/protect.py
M scripts/selflink.py
M scripts/solve_disambiguation.py
M scripts/spamremove.py
M scripts/template.py
M scripts/templatecount.py
M scripts/unlink.py
M scripts/upload.py
M scripts/welcome.py
25 files changed, 199 insertions(+), 208 deletions(-)
Approvals:
John Vandenberg: Looks good to me, approved
jenkins-bot: Verified
diff --git a/generate_user_files.py b/generate_user_files.py
index 8a99806..ea6915a 100644
--- a/generate_user_files.py
+++ b/generate_user_files.py
@@ -98,7 +98,7 @@
set environment variables.""" % locals(), width=76)
for line in msg:
pywikibot.output(line)
- if pywikibot.inputChoice("Is this OK?", ["yes", "no"],
["y", "n"], "n") == "y":
+ if pywikibot.input_yn('Is this OK?', default=False, automatic_quit=False):
return new_base
pywikibot.output("Aborting changes.")
return False
@@ -170,10 +170,10 @@
while not mylang:
mylang = pywikibot.input(message) or default_lang
if known_langs and mylang and mylang not in known_langs:
- if pywikibot.inputChoice("The language code {0} is not in the "
- "list of known languages. Do you want to
"
- "continue?".format(mylang),
- ["yes", "no"], ["y",
"n"], "n") == "n":
+ if not pywikibot.input_yn("The language code {0} is not in the "
+ "list of known languages. Do you want "
+ "to continue?".format(mylang),
+ default=False, automatic_quit=False):
mylang = None
username = None
@@ -250,8 +250,8 @@
main_family, main_lang, main_username = get_site_and_lang()
usernames = [(main_family, main_lang, main_username)]
- while pywikibot.inputChoice("Do you want to add any other projects?",
- ["yes", "no"], ["y",
"n"], "n") == "y":
+ while pywikibot.input_yn("Do you want to add any other projects?",
+ default=False, automatic_quit=False):
usernames += [get_site_and_lang(main_family, main_lang,
main_username)]
@@ -259,9 +259,9 @@
u"usernames['{0}']['{1}'] =
u'{2}'".format(*username)
for username in usernames)
- extended = pywikibot.inputChoice(
- "Which variant of user_config.py?",
- ["small", "extended (with further information)"],
["s", "e"]) == "e"
+ extended = pywikibot.input_yn("Would you like the extended version of
"
+ "user-config.py, with explanations "
+ "included?", automatic_quit=False)
if extended:
# config2.py will be in the pywikibot/ directory relative to this
@@ -321,8 +321,8 @@
if __name__ == "__main__":
while True:
pywikibot.output(u'\nYour default user directory is "%s"' %
base_dir)
- if pywikibot.inputChoice("How to proceed?", ["keep",
"change"],
- ["k", "c"], "k") ==
"c":
+ if pywikibot.input_yn("Do you want to use that directory?",
+ default=False, automatic_quit=False):
new_base = change_base_dir()
if new_base:
base_dir = new_base
@@ -335,9 +335,10 @@
while True:
if os.path.exists(os.path.join(base_dir, "user-config.py")):
break
- if pywikibot.inputChoice(
+ if pywikibot.input_yn(
"Do you want to copy user files from an existing Pywikibot "
- "installation?", ["yes", "no"],
["y", "n"]) == "y":
+ "installation?",
+ automatic_quit=False):
oldpath = pywikibot.input("Path to existing user-config.py?")
if not os.path.exists(oldpath):
pywikibot.error("Not a valid path")
@@ -361,16 +362,16 @@
else:
break
if not os.path.isfile(os.path.join(base_dir, "user-config.py")):
- if pywikibot.inputChoice("Create user-config.py file? Required for "
- "running bots.",
- ["yes", "no"], ["y",
"n"], "n") == "y":
+ if pywikibot.input_yn('Create user-config.py file? Required for '
+ 'running bots.',
+ default=False, automatic_quit=False):
create_user_config()
elif not copied_config:
pywikibot.output("user-config.py already exists in the directory")
if not os.path.isfile(os.path.join(base_dir, "user-fixes.py")):
- if pywikibot.inputChoice("Create user-fixes.py file? Optional and for
"
- "advanced users.",
- ["yes", "no"], ["y",
"n"], "n") == "y":
+ if pywikibot.input_yn('Create user-fixes.py file? Optional and for '
+ 'advanced users.',
+ default=False, automatic_quit=False):
create_user_fixes()
elif not copied_fixes:
pywikibot.output("user-fixes.py already exists in the directory")
diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py
index ad0c475..a007c9a 100644
--- a/pywikibot/__init__.py
+++ b/pywikibot/__init__.py
@@ -29,7 +29,7 @@
from pywikibot import config2 as config
from pywikibot.bot import (
output, warning, error, critical, debug, stdout, exception,
- input, input_choice, inputChoice, handle_args, showHelp, ui, log,
+ input, input_choice, input_yn, inputChoice, handle_args, showHelp, ui, log,
calledModuleName, Bot, WikidataBot, QuitKeyboardInterrupt,
# the following are flagged as deprecated on usage
handleArgs,
@@ -68,7 +68,7 @@
'ItemPage', 'PropertyPage', 'Claim',
'TimeStripper',
'html2unicode', 'url2unicode', 'unicode2html',
'stdout', 'output', 'warning', 'error',
'critical', 'debug',
- 'exception', 'input_choice', 'input',
'inputChoice',
+ 'exception', 'input_choice', 'input',
'input_yn', 'inputChoice',
'handle_args', 'handleArgs', 'showHelp', 'ui',
'log',
'calledModuleName', 'Bot', 'WikidataBot',
'Error', 'InvalidTitle', 'BadTitle', 'NoPage',
'SectionError',
@@ -699,11 +699,9 @@
try:
_putthread.join(1)
except KeyboardInterrupt:
- answer = inputChoice(u"""\
-There are %i pages remaining in the queue. Estimated time remaining: %s
-Really exit?""" % remaining(),
- ['yes', 'no'], ['y', 'N'],
'N')
- if answer == 'y':
+ if input_yn('There are %i pages remaining in the queue. '
+ 'Estimated time remaining: %s\nReally exit?'
+ % remaining(), default=False, automatic_quit=False):
return
# only need one drop() call because all throttles use the same global pid
diff --git a/pywikibot/bot.py b/pywikibot/bot.py
index 89ed75d..caafaca 100644
--- a/pywikibot/bot.py
+++ b/pywikibot/bot.py
@@ -516,6 +516,8 @@
"""
Ask the user the question and return one of the valid answers.
+ @param question: The question asked without trailing spaces.
+ @type answers: basestring
@param answers: The valid answers each containing a full length answer and
a shortcut. Each value must be unique.
@type answers: Iterable containing an iterable of length two
@@ -527,7 +529,7 @@
returned.
@type return_shortcut: bool
@param automatic_quit: Adds the option 'Quit' ('q') and throw a
- L{QuitKeyboardInterrupt} if selected (default).
+ L{QuitKeyboardInterrupt} if selected.
@type automatic_quit: bool
@return: The selected answer shortcut or index. Is -1 if the default is
selected, it does not return the shortcut and the default is not a
@@ -542,6 +544,34 @@
automatic_quit)
+def input_yn(question, default=None, automatic_quit=True):
+ """
+ Ask the user a yes/no question and returns the answer as a bool.
+
+ @param question: The question asked without trailing spaces.
+ @type answers: basestring
+ @param default: The result if no answer was entered. It must be a bool or
+ 'y' or 'n' and can be disabled by setting it to None.
+ @type default: basestring or bool
+ @param automatic_quit: Adds the option 'Quit' ('q') and throw a
+ L{QuitKeyboardInterrupt} if selected.
+ @type automatic_quit: bool
+ @return: Return True if the user selected yes and False if the user
+ selected no. If the default is not None it'll return True if default
+ is True or 'y' and False if default is False or 'n'.
+ @rtype: bool
+ """
+ if default not in ['y', 'Y', 'n', 'N']:
+ if default:
+ default = 'y'
+ elif default is not None:
+ default = 'n'
+ assert default in ['y', 'Y', 'n', 'N', None]
+
+ return input_choice(question, [('Yes', 'y'), ('No',
'n')], default,
+ automatic_quit=automatic_quit) == 'y'
+
+
@deprecated('input_choice')
def inputChoice(question, answers, hotkeys, default=None):
"""Ask the user a question with several options, return the user's
choice.
diff --git a/pywikibot/diff.py b/pywikibot/diff.py
index 346fcba..7ac30dc 100644
--- a/pywikibot/diff.py
+++ b/pywikibot/diff.py
@@ -245,8 +245,8 @@
]
question = 'Accept this hunk?'
- answers = ['yes', 'no', 'stop', 'all',
'review', 'help']
- hotkeys = ['y', 'n', 's', 'a', 'r',
'h']
+ answers = [('yes', 'y'), ('no', 'n'),
('stop', 's'), ('all', 'a'),
+ ('review', 'r'), ('help', 'h')]
actions = {'y': Hunk.APPR,
'n': Hunk.NOT_APPR,
's': Hunk.NOT_APPR,
@@ -261,7 +261,8 @@
hunk = pending.pop(0)
pywikibot.output(hunk.header + hunk.diff_text)
- choice = pywikibot.inputChoice(question, answers, hotkeys,
default='r')
+ choice = pywikibot.input_choice(question, answers, default='r',
+ automatic_quit=False)
if choice in actions.keys():
hunk.reviewed = actions[choice]
diff --git a/pywikibot/page.py b/pywikibot/page.py
index 2c3db36..e3d0755 100644
--- a/pywikibot/page.py
+++ b/pywikibot/page.py
@@ -1476,12 +1476,11 @@
if self.site.username(sysop=True):
answer = u'y'
if prompt and not hasattr(self.site, '_noDeletePrompt'):
- answer = pywikibot.inputChoice(
+ answer = pywikibot.input_choice(
u'Do you want to delete %s?' % self.title(
asLink=True, forceInterwiki=True),
- ['Yes', 'No', 'All'],
- ['y', 'n', 'a'],
- 'n')
+ [('Yes', 'y'), ('No', 'n'),
('All', 'a')],
+ 'n', automatic_quit=False)
if answer == 'a':
answer = 'y'
self.site._noDeletePrompt = True
@@ -1491,13 +1490,12 @@
if mark or hasattr(self.site, '_noMarkDeletePrompt'):
answer = 'y'
else:
- answer = pywikibot.inputChoice(
+ answer = pywikibot.input_choice(
u"Can't delete %s; do you want to mark it "
"for deletion instead?" % self.title(asLink=True,
forceInterwiki=True),
- ['Yes', 'No', 'All'],
- ['y', 'n', 'a'],
- 'n')
+ [('Yes', 'y'), ('No', 'n'),
('All', 'a')],
+ 'n', automatic_quit=False)
if answer == 'a':
answer = 'y'
self.site._noMarkDeletePrompt = True
@@ -1651,12 +1649,11 @@
pywikibot.bot.warning(u'"prompt" argument of protect() is
'
'deprecated')
if prompt and not hasattr(self.site, '_noProtectPrompt'):
- answer = pywikibot.inputChoice(
+ answer = pywikibot.input_choice(
u'Do you want to change the protection level of %s?'
% self.title(asLink=True, forceInterwiki=True),
- ['Yes', 'No', 'All'],
- ['y', 'N', 'a'],
- 'N')
+ [('Yes', 'y'), ('No', 'n'),
('All', 'a')],
+ 'n', automatic_quit=False)
if answer == 'a':
answer = 'y'
self.site._noProtectPrompt = True
diff --git a/scripts/add_text.py b/scripts/add_text.py
index 8979dba..d90e0cd 100644
--- a/scripts/add_text.py
+++ b/scripts/add_text.py
@@ -222,10 +222,10 @@
# text in the page
if putText:
if not always:
- choice = pywikibot.inputChoice(
+ choice = pywikibot.input_choice(
u'Do you want to accept these changes?',
- ['Yes', 'No', 'All', 'open in
Browser'],
- ['y', 'n', 'a', 'b'], 'n')
+ [('Yes', 'y'), ('No', 'n'),
('All', 'a'),
+ ('open in Browser', 'b')], 'n',
automatic_quit=False)
if choice == 'a':
always = True
elif choice == 'n':
diff --git a/scripts/basic.py b/scripts/basic.py
index c1af240..9c315ea 100755
--- a/scripts/basic.py
+++ b/scripts/basic.py
@@ -111,10 +111,9 @@
pywikibot.showDiff(page.get(), text)
pywikibot.output(u'Comment: %s' % comment)
if not self.dry:
- choice = pywikibot.inputChoice(
- u'Do you want to accept these changes?',
- ['Yes', 'No'], ['y', 'N'],
'N')
- if choice == 'y':
+ if pywikibot.input_yn(
+ u'Do you want to accept these changes?',
+ default=False, automatic_quit=False):
try:
page.text = text
# Save the page
diff --git a/scripts/blockpageschecker.py b/scripts/blockpageschecker.py
index fd548af..52fd2fb 100755
--- a/scripts/blockpageschecker.py
+++ b/scripts/blockpageschecker.py
@@ -189,9 +189,10 @@
def showQuest(page):
- quest = pywikibot.inputChoice(u'Do you want to open the page?',
- ['with browser', 'with gui',
'no'],
- ['b', 'g', 'n'], 'n')
+ quest = pywikibot.input_choice(
+ u'Do you want to open the page?',
+ [('with browser', 'b'), ('with gui', 'g'),
('no', 'n')], 'n',
+ automatic_quit=False)
site = page.site
url = '%s://%s%s?redirect=no' % (site.protocol(),
site.hostname(),
@@ -452,10 +453,10 @@
% page.title())
pywikibot.showDiff(oldtext, text)
if not always:
- choice = pywikibot.inputChoice(u'Do you want to accept these '
- u'changes?',
- ['Yes', 'No',
'All'],
- ['y', 'N', 'a'],
'N')
+ choice = pywikibot.input_choice(u'Do you want to accept these '
+ u'changes?',
+ [('Yes', 'y'),
('No', 'n'),
+ ('All', 'a')],
'n')
if choice == 'a':
always = True
if always or choice == 'y':
diff --git a/scripts/blockreview.py b/scripts/blockreview.py
index f3f9c98..9abbe12 100644
--- a/scripts/blockreview.py
+++ b/scripts/blockreview.py
@@ -279,10 +279,9 @@
pywikibot.showDiff(page.get(), text)
pywikibot.output(u'Comment: %s' % comment)
if not self.dry:
- choice = pywikibot.inputChoice(
- u'Do you want to accept these changes?',
- ['Yes', 'No'], ['y', 'N'],
'N')
- if choice == 'y':
+ if pywikibot.input_yn(
+ u'Do you want to accept these changes?',
+ default=False, automatic_quit=False):
page.text = text
try:
# Save the page
diff --git a/scripts/capitalize_redirects.py b/scripts/capitalize_redirects.py
index d646b38..d74921c 100644
--- a/scripts/capitalize_redirects.py
+++ b/scripts/capitalize_redirects.py
@@ -66,13 +66,11 @@
pywikibot.output(u'%s doesn\'t exist'
% page_cap.title(asLink=True))
if not self.getOption('always'):
- choice = pywikibot.inputChoice(
+ choice = pywikibot.input_choice(
u'Do you want to create a redirect?',
- ['Yes', 'No', 'All', 'Quit'],
['y', 'N', 'a', 'q'], 'N')
+ [('Yes', 'y'), ('No', 'n'),
('All', 'a')], 'n')
if choice == 'a':
self.options['always'] = True
- elif choice == 'q':
- self.quit()
if self.getOption('always') or choice == 'y':
comment = i18n.twtranslate(
page.site,
diff --git a/scripts/casechecker.py b/scripts/casechecker.py
index 7d0c4da..8bc2b23 100644
--- a/scripts/casechecker.py
+++ b/scripts/casechecker.py
@@ -380,10 +380,9 @@
if self.replace:
if len(err[1]) == 1:
newTitle = err[1][0]
-## choice = pywikibot.inputChoice(u'Move %s to %s?'
-## % (title, newTitle),
-## ['Yes',
'No'],
-## ['y', 'n'])
+## choice = pywikibot.input_yn(u'Move %s to %s?'
+## % (title, newTitle),
+## automatic_quit=False)
editSummary = i18n.twtranslate(
self.site, "casechecker-rename")
dst = self.Page(newTitle)
@@ -680,12 +679,10 @@
msg = u'page exists'
self.ColorCodeWord(u' %d: %s (%s)\n' % (count, t, msg), True)
count += 1
- answers = [str(i) for i in xrange(0, count)]
- choice = int(pywikibot.inputChoice(
- u'Which link to choose? (0 to skip)',
- answers, [a[0] for a in answers]))
- if choice > 0:
- return candidates[choice - 1]
+ answers = [('skip', 's')] + [(str(i), i) for i in range(1,
count)]
+ choice = pywikibot.input_choice(u'Which link to choose?', answers)
+ if choice != 's':
+ return candidates[int(choice) - 1]
def ColorCodeWord(self, word, toScreen=False):
if not toScreen:
diff --git a/scripts/cosmetic_changes.py b/scripts/cosmetic_changes.py
index e02e7ca..f0c892d 100755
--- a/scripts/cosmetic_changes.py
+++ b/scripts/cosmetic_changes.py
@@ -938,7 +938,6 @@
@param args: command line arguments
@type args: list of unicode
"""
- answer = 'y'
options = {}
# Process global args and prepare generator args parser
@@ -972,11 +971,9 @@
gen = genFactory.getCombinedGenerator()
if gen:
- if not options.get('always'):
- answer = pywikibot.inputChoice(
+ if options.get('always') or pywikibot.input_yn(
warning + '\nDo you really want to continue?',
- ['yes', 'no'], ['y', 'n'], 'n')
- if answer == 'y':
+ default=False, automatic_quit=False):
site.login()
preloadingGen = pagegenerators.PreloadingGenerator(gen)
bot = CosmeticChangesBot(preloadingGen, **options)
diff --git a/scripts/interwiki.py b/scripts/interwiki.py
index 61bc6b0..89a04a6 100755
--- a/scripts/interwiki.py
+++ b/scripts/interwiki.py
@@ -1058,12 +1058,13 @@
linkedPage.namespace(), preferredPage))
return True
else:
- choice = pywikibot.inputChoice(
+ choice = pywikibot.input_choice(
u'WARNING: %s is in namespace %i, but %s is in namespace %i. Follow it anyway?'
% (self.originPage, self.originPage.namespace(),
linkedPage, linkedPage.namespace()),
- ['Yes', 'No', 'Add an alternative',
'give up'],
- ['y', 'n', 'a', 'g'])
+ [('Yes', 'y'), ('No', 'n'),
+ ('Add an alternative', 'a'), ('give up',
'g')],
+ automatic_quit=False)
if choice != 'y':
# Fill up foundIn, so that we will not ask again
self.foundIn[linkedPage] = [linkingPage]
@@ -1146,12 +1147,13 @@
% (page, self.originPage, disambig))
return (True, None)
else:
- choice = pywikibot.inputChoice(
+ choice = pywikibot.input_choice(
u"WARNING: %s is a disambiguation page, but %s doesn't
"
u"seem to be one. Follow it anyway?"
% (self.originPage, page),
- ['Yes', 'No', 'Add an alternative',
'Give up'],
- ['y', 'n', 'a', 'g'])
+ [('Yes', 'y'), ('No', 'n'),
+ ('Add an alternative', 'a'), ('give up',
'g')],
+ automatic_quit=False)
elif not self.originPage.isDisambig() and page.isDisambig():
nondisambig = self.getFoundNonDisambig(page.site)
if nondisambig:
@@ -1161,12 +1163,13 @@
% (page, self.originPage, nondisambig))
return (True, None)
else:
- choice = pywikibot.inputChoice(
+ choice = pywikibot.input_choice(
u'WARNING: %s doesn\'t seem to be a disambiguation '
u'page, but %s is one. Follow it anyway?'
% (self.originPage, page),
- ['Yes', 'No', 'Add an alternative',
'Give up'],
- ['y', 'n', 'a', 'g'])
+ [('Yes', 'y'), ('No', 'n'),
+ ('Add an alternative', 'a'), ('give up',
'g')],
+ automatic_quit=False)
if choice == 'n':
return (True, None)
elif choice == 'a':
@@ -1581,7 +1584,11 @@
answer = 'a'
else:
# TODO: allow answer to repeat previous or go back after a
mistake
- answer = pywikibot.inputChoice(u'What should be done?',
['accept', 'reject', 'give up', 'accept all'],
['a', 'r', 'g', 'l'], 'a')
+ answer = pywikibot.input_choice(
+ u'What should be done?',
+ [('accept', 'a'), ('reject',
'r'),
+ ('give up', 'g'), ('accept all',
'l')], 'a',
+ automatic_quit=False)
if answer == 'l': # accept all
acceptall = True
answer = 'a'
@@ -1952,10 +1959,12 @@
# If we cannot ask, deny permission
answer = 'n'
else:
- answer = pywikibot.inputChoice(u'Submit?',
- ['Yes', 'No', 'open in
Browser',
- 'Give up', 'Always'],
- ['y', 'n', 'b',
'g', 'a'])
+ answer = pywikibot.input_choice(u'Submit?',
+ [('Yes', 'y'),
('No', 'n'),
+ ('open in Browser',
'b'),
+ ('Give up', 'g'),
+ ('Always', 'a')],
+ automatic_quit=False)
if answer == 'b':
webbrowser.open("http://%s%s" % (
page.site.hostname(),
diff --git a/scripts/maintenance/compat2core.py b/scripts/maintenance/compat2core.py
index bc74cf3..90b318f 100644
--- a/scripts/maintenance/compat2core.py
+++ b/scripts/maintenance/compat2core.py
@@ -144,9 +144,9 @@
def get_dest(self):
self.dest = u'%s-core.%s' % tuple(self.source.rsplit(u'.', 1))
- if not self.warnonly and pywikibot.inputChoice(
+ if not self.warnonly and not pywikibot.input_yn(
u'Destination file is %s.' % self.dest,
- ['Yes', 'No'], ['y', 'n'], 'y')
== 'n':
+ default=True, automatic_quit=False):
pywikibot.output('Quitting...')
exit()
diff --git a/scripts/movepages.py b/scripts/movepages.py
index d5fc6ec..59edd26 100644
--- a/scripts/movepages.py
+++ b/scripts/movepages.py
@@ -104,16 +104,14 @@
newPageTitle = (u'%s%s' % (self.getOption('prefix'),
pagetitle))
if self.getOption('prefix') or self.appendAll or self.regexAll:
if not self.getOption('always'):
- choice2 = pywikibot.inputChoice(
+ choice2 = pywikibot.input_choice(
u'Change the page title to "%s"?' % newPageTitle,
- ['yes', 'no', 'all', 'quit'],
['y', 'n', 'a', 'q'])
+ [('yes', 'y'), ('no', 'n'),
('all', 'a')])
if choice2 == 'y':
self.moveOne(page, newPageTitle)
elif choice2 == 'a':
self.options['always'] = True
self.moveOne(page, newPageTitle)
- elif choice2 == 'q':
- self.quit()
elif choice2 == 'n':
pass
else:
@@ -121,12 +119,11 @@
else:
self.moveOne(page, newPageTitle)
else:
- choice = pywikibot.inputChoice(u'What do you want to do?',
- ['change page name',
- 'append to page name',
- 'use a regular expression',
- 'next page', 'quit'],
- ['c', 'a', 'r',
'n', 'q'])
+ choice = pywikibot.input_choice(u'What do you want to do?',
+ [('change page name', 'c'),
+ ('append to page name',
'a'),
+ ('use a regular expression',
'r'),
+ ('next page', 'n')])
if choice == 'c':
newPageTitle = pywikibot.input(u'New page name:')
self.moveOne(page, newPageTitle)
@@ -136,24 +133,20 @@
newPageTitle = (u'%s%s%s'
% (self.pagestart, pagetitle, self.pageend))
if namesp:
- choice2 = pywikibot.inputChoice(
- u'Do you want to remove the namespace prefix
"%s:"?'
- % namesp, ['yes', 'no'], ['y',
'n'])
- if choice2 == 'y':
+ if pywikibot.input_yn(u'Do you want to remove the '
+ 'namespace prefix "%s:"?' %
namesp,
+ automatic_quit=False):
self.noNamespace = True
else:
newPageTitle = (u'%s:%s' % (namesp, newPageTitle))
- choice2 = pywikibot.inputChoice(
+ choice2 = pywikibot.input_choice(
u'Change the page title to "%s"?'
- % newPageTitle, ['yes', 'no', 'all',
'quit'],
- ['y', 'n', 'a', 'q'])
+ % newPageTitle, [('yes', 'y'), ('no',
'n'), ('all', 'a')])
if choice2 == 'y':
self.moveOne(page, newPageTitle)
elif choice2 == 'a':
self.appendAll = True
self.moveOne(page, newPageTitle)
- elif choice2 == 'q':
- self.quit()
elif choice2 == 'n':
pass
else:
@@ -167,35 +160,29 @@
newPageTitle = self.regex.sub(self.replacePattern,
page.title())
else:
- choice2 = pywikibot.inputChoice(
- u'Do you want to remove the namespace prefix
"%s:"?'
- % namesp, ['yes', 'no'], ['y',
'n'])
- if choice2 == 'y':
+ if pywikibot.input_yn(u'Do you want to remove the '
+ 'namespace prefix "%s:"?' %
namesp,
+ automatic_quit=False):
newPageTitle = self.regex.sub(
self.replacePattern, page.title(withNamespace=False))
self.noNamespace = True
else:
newPageTitle = self.regex.sub(self.replacePattern,
page.title())
- choice2 = pywikibot.inputChoice(
+ choice2 = pywikibot.input_choice(
u'Change the page title to "%s"?'
- % newPageTitle, ['yes', 'no', 'all',
'quit'],
- ['y', 'n', 'a', 'q'])
+ % newPageTitle, [('yes', 'y'), ('no',
'n'), ('all', 'a')])
if choice2 == 'y':
self.moveOne(page, newPageTitle)
elif choice2 == 'a':
self.regexAll = True
self.moveOne(page, newPageTitle)
- elif choice2 == 'q':
- self.quit()
elif choice2 == 'n':
pass
else:
self.treat(page)
elif choice == 'n':
pass
- elif choice == 'q':
- self.quit()
else:
self.treat(page)
diff --git a/scripts/nowcommons.py b/scripts/nowcommons.py
index b3494e4..7d27c71 100644
--- a/scripts/nowcommons.py
+++ b/scripts/nowcommons.py
@@ -246,14 +246,14 @@
webbrowser.open(url_local, 0, 1)
webbrowser.open(url_commons, 0, 1)
if image_local.split('Image:')[1] == image_commons:
- choice = pywikibot.inputChoice(
- u'The local and the commons images have the same name,
continue?',
- ['Yes', 'No'], ['y', 'N'],
'N')
+ choice = pywikibot.input_yn(
+ u'The local and the commons images have the same name, '
+ 'continue?', default=False, automatic_quit=False)
else:
- choice = pywikibot.inputChoice(
+ choice = pywikibot.input_yn(
u'Are the two images equal?',
- ['Yes', 'No'], ['y', 'N'],
'N')
- if choice == 'y':
+ default=False, automatic_quit=False)
+ if choice:
yield [image_local, image_commons]
else:
continue
@@ -413,11 +413,11 @@
u'\n\n>>>> Description on
\03{lightpurple}%s\03{default} <<<<\n'
% commonsImagePage.title())
pywikibot.output(commonsText)
- choice = pywikibot.inputChoice(u'Does the description \
- on Commons contain all
required source and license\n'
- u'information?',
- ['yes', 'no'],
['y', 'N'], 'N')
- if choice == 'y':
+ if pywikibot.input_yn(
+ u'Does the description on Commons contain '
+ 'all required source and license\n'
+ 'information?',
+ default=False, automatic_quit=False):
localImagePage.delete(
'%s [[:commons:Image:%s]]'
% (comment, filenameOnCommons), prompt=False)
diff --git a/scripts/protect.py b/scripts/protect.py
index 0a4279f..09144fb 100644
--- a/scripts/protect.py
+++ b/scripts/protect.py
@@ -91,12 +91,11 @@
for page in self.generator:
self.current_page = page
if not self.getOption('always'):
- choice = pywikibot.inputChoice(
+ choice = pywikibot.input_choice(
u'Do you want to change the protection level of %s?'
% page.title(asLink=True, forceInterwiki=True),
- ['yes', 'No', 'all'],
- ['y', 'N', 'a'],
- 'n')
+ [('yes', 'y'), ('No', 'n'),
('all', 'a')],
+ 'n', automatic_quit=False)
if choice == 'n':
continue
elif choice == 'a':
@@ -128,9 +127,9 @@
num += 1
if level == default:
default_char = first_char[-1]
- choice = pywikibot.inputChoice('Choice a protection level to %s:'
- % operation, levels, first_char,
- default=default_char)
+ choice = pywikibot.input_choice('Choice a protection level to %s:'
+ % operation, zip(levels, first_char),
+ default=default_char)
return levels[first_char.index(choice)]
else:
diff --git a/scripts/selflink.py b/scripts/selflink.py
index cb21cc9..1687c97 100644
--- a/scripts/selflink.py
+++ b/scripts/selflink.py
@@ -55,7 +55,6 @@
r'(?P<section>#[^\]\|]*)?'
'(\|(?P<label>[^\]]*))?\]\]'
r'(?P<linktrail>' + linktrail + ')')
- self.done = False
def handleNextLink(self, page, match, context=100):
"""Process the next link on a page, offering the user choices.
@@ -97,11 +96,10 @@
matchText = match.group(0)
pywikibot.output(
pre + '\03{lightred}' + matchText + '\03{default}' +
post)
- choice = pywikibot.inputChoice(
+ choice = pywikibot.input_choice(
u'\nWhat shall be done with this selflink?\n',
- ['unlink', 'make bold', 'skip', 'edit',
'more context',
- 'unlink all', 'quit'],
- ['U', 'b', 's', 'e', 'm',
'a', 'q'], 'u')
+ [('unlink', 'u'), ('make bold', 'b'),
('skip', 's'),
+ ('edit', 'e'), ('more context', 'm'),
('unlink all')], 'u')
pywikibot.output(u'')
if choice == 's':
@@ -121,9 +119,6 @@
return self.handleNextLink(page, match, context=context + 100)
elif choice == 'a':
self.always = True
- elif choice == 'q':
- self.done = True
- return False
# choice was 'U', 'b', or 'a'
new = match.group('label') or match.group('title')
@@ -151,7 +146,7 @@
% page.title(asLink=True))
return
curpos = 0
- while curpos < len(page.text) or self.done:
+ while curpos < len(page.text):
match = self.linkR.search(page.text, pos=curpos)
if not match:
break
@@ -176,12 +171,6 @@
% page.title(asLink=True))
except pywikibot.LockedPage:
pywikibot.output(u"Page %s is locked." % page.title(asLink=True))
-
- def run(self):
- for page in self.generator:
- if self.done:
- break
- self.treat(page)
def main(*args):
diff --git a/scripts/solve_disambiguation.py b/scripts/solve_disambiguation.py
index e79eab8..c3edae1 100644
--- a/scripts/solve_disambiguation.py
+++ b/scripts/solve_disambiguation.py
@@ -570,10 +570,9 @@
% (refPage.title(), disambPage.title()))
if disambPage.isRedirectPage():
target = self.alternatives[0]
- choice = pywikibot.inputChoice(
- u'Do you want to make redirect %s point to %s?'
- % (refPage.title(), target), ['yes', 'no'],
['y', 'N'], 'N')
- if choice == 'y':
+ if pywikibot.input_yn(u'Do you want to make redirect %s point '
+ 'to %s?' % (refPage.title(), target),
+ default=False, automatic_quit=False):
redir_text = '#%s [[%s]]' \
% (self.mysite.redirect(default=True), target)
try:
@@ -581,10 +580,11 @@
except pywikibot.PageNotSaved as error:
pywikibot.output(u'Page not saved: %s' % error.args)
else:
- choice = pywikibot.inputChoice(
+ choice = pywikibot.input_choice(
u'Do you want to work on pages linking to %s?'
- % refPage.title(), ['yes', 'no', 'change
redirect'],
- ['y', 'N', 'c'],
'N')
+ % refPage.title(),
+ [('yes', 'y'), ('no', 'n'),
('change redirect', 'c')], 'n',
+ automatic_quit=False)
if choice == 'y':
gen = ReferringPageGeneratorWithIgnore(refPage,
self.primary)
@@ -1065,10 +1065,10 @@
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')
- if answer == 'y':
+ if pywikibot.input_yn(
+ u'Possibility %s does not actually exist. Use it '
+ 'anyway?' % page.title(),
+ default=False, automatic_quit=False):
alternatives.append(page.title())
else:
alternatives.append(arg[5:])
diff --git a/scripts/spamremove.py b/scripts/spamremove.py
index 17cb25f..ec92df4 100755
--- a/scripts/spamremove.py
+++ b/scripts/spamremove.py
@@ -100,9 +100,10 @@
if always:
answer = "y"
else:
- answer = pywikibot.inputChoice(u'\nDelete the red lines?',
- ['yes', 'no',
'edit'],
- ['y', 'N', 'e'],
'n')
+ answer = pywikibot.input_choice(
+ u'\nDelete the red lines?',
+ [('yes', 'y'), ('no', 'n'),
('edit', 'e')],
+ 'n', automatic_quit=False)
if answer == "n":
continue
elif answer == "e":
diff --git a/scripts/template.py b/scripts/template.py
index b6389d8..5eaa837 100755
--- a/scripts/template.py
+++ b/scripts/template.py
@@ -277,10 +277,8 @@
template = pywikibot.Page(site, new, ns=10)
if not template.exists():
pywikibot.warning(u'Template "%s" does not exist.'
% new)
- choice = pywikibot.inputChoice(
- u'Do you want to proceed anyway?',
- ['Yes', 'No'], ['y', 'N'],
'N')
- if choice == 'n':
+ if not pywikibot.input_yn('Do you want to proceed anyway?',
+ default=False, automatic_quit=False):
continue
replacements.append((templateRegex,
'{{%s\g<parameters>}}' % new))
diff --git a/scripts/templatecount.py b/scripts/templatecount.py
index fd67c21..ab70b97 100644
--- a/scripts/templatecount.py
+++ b/scripts/templatecount.py
@@ -145,8 +145,10 @@
if 'reflist' in argsList:
pywikibot.output(
u'NOTE: it will take a long time to count "reflist".')
- choice = pywikibot.inputChoice(
- u'Proceed anyway?', ['yes', 'no', 'skip'],
['y', 'n', 's'], 'y')
+ choice = pywikibot.input_choice(
+ u'Proceed anyway?',
+ [('yes', 'y'), ('no', 'n'), ('skip',
's')], 'y',
+ automatic_quit=False)
if choice == 's':
argsList.remove('reflist')
elif choice == 'n':
diff --git a/scripts/unlink.py b/scripts/unlink.py
index a94ff9f..ea24f7c 100755
--- a/scripts/unlink.py
+++ b/scripts/unlink.py
@@ -104,11 +104,10 @@
text[max(0, match.start() - context):match.start()]
+ '\03{lightred}' + text[match.start():match.end()]
+ '\03{default}' + text[match.end():match.end() + context])
- choice = pywikibot.inputChoice(
+ choice = pywikibot.input_choice(
u'\nWhat shall be done with this link?\n',
- ['unlink', 'skip', 'edit', 'more
context',
- 'unlink all', 'quit'],
- ['U', 's', 'e', 'm', 'a',
'q'], 'u')
+ [('unlink', 'u'), ('skip', 's'),
('edit', 'e'),
+ ('more context', 'm'), ('unlink all',
'a')], 'u')
pywikibot.output(u'')
if choice == 's':
@@ -128,8 +127,6 @@
context=context + 100)
elif choice == 'a':
self.options['always'] = True
- elif choice == 'q':
- self.quit()
new = match.group('label') or match.group('title')
new += match.group('linktrail')
return text[:match.start()] + new + text[match.end():], False
diff --git a/scripts/upload.py b/scripts/upload.py
index 69127b0..90cb164 100755
--- a/scripts/upload.py
+++ b/scripts/upload.py
@@ -48,7 +48,6 @@
import pywikibot
import pywikibot.data.api
from pywikibot import config
-from pywikibot.bot import QuitKeyboardInterrupt
if sys.version_info[0] > 2:
from urllib.parse import urlparse
@@ -190,11 +189,10 @@
'Invalid character(s): %s. Please try again' % c)
continue
if ext not in allowed_formats:
- choice = pywikibot.inputChoice(
- u"File format is not one of [%s], but %s. Continue?"
- % (u' '.join(allowed_formats), ext),
- ['yes', 'no'], ['y', 'N'],
'N')
- if choice == 'n':
+ if not pywikibot.input_yn(
+ u"File format is not one of [%s], but %s.
Continue?"
+ % (u' '.join(allowed_formats), ext),
+ default=False, automatic_quit=False):
continue
break
if newfn != '':
@@ -215,21 +213,16 @@
'without a summary/description.\03{default}')
# if no description, default is 'yes'
- default = 'y' if not self.description else 'n'
- choice = pywikibot.inputChoice(
- u'Do you want to change this description?',
- ['Yes', 'No', 'Quit'], ['y', 'n',
'q'], default)
- if choice == 'y':
+ if pywikibot.input_yn(
+ u'Do you want to change this description?',
+ default=not self.description):
from pywikibot import editor as editarticle
editor = editarticle.TextEditor()
newDescription = editor.edit(self.description)
# if user saved / didn't press Cancel
if newDescription:
self.description = newDescription
- if choice == 'q':
- raise QuitKeyboardInterrupt
- else:
- self.verifyDescription = False
+ self.verifyDescription = False
return filename
@@ -272,12 +265,9 @@
except pywikibot.data.api.UploadWarning as warn:
pywikibot.output(
u'We got a warning message: {0}'.format(warn.message))
- if self.abort_on_warn(warn.code):
- answer = "N"
- else:
- answer = pywikibot.inputChoice(u"Do you want to ignore?",
- ['Yes', 'No'],
['y', 'N'], 'N')
- if answer == "y":
+ if (not self.abort_on_warn(warn.code) and
+ pywikibot.input_yn(u"Do you want to ignore?",
+ default=False, automatic_quit=False)):
self.ignoreWarning = True
self.keepFilename = True
return self.upload_image(debug)
diff --git a/scripts/welcome.py b/scripts/welcome.py
index 63a9764..c37a582 100644
--- a/scripts/welcome.py
+++ b/scripts/welcome.py
@@ -563,10 +563,11 @@
#Queue process
if name:
if globalvar.confirm:
- answer = pywikibot.inputChoice(
+ answer = pywikibot.input_choice(
u'%s may have an unwanted username, do you want to report '
u'this user?' % name,
- ['Yes', 'No', 'All'], ['y',
'N', 'a'], 'N')
+ [('Yes', 'y'), ('No', 'n'),
('All', 'a')], 'n',
+ automatic_quit=False)
if answer in ['a', 'all']:
answer = 'y'
globalvar.confirm = False
--
To view, visit
https://gerrit.wikimedia.org/r/168225
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Ib68e66a60e5bc5a78602253123312a380109657d
Gerrit-PatchSet: 7
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: XZise <CommodoreFabianus(a)gmx.de>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Ladsgroup <ladsgroup(a)gmail.com>
Gerrit-Reviewer: Merlijn van Deen <valhallasw(a)arctus.nl>
Gerrit-Reviewer: XZise <CommodoreFabianus(a)gmx.de>
Gerrit-Reviewer: jenkins-bot <>