jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/496056 )
Change subject: [IMPR] Let the operator choose misspelled script ......................................................................
[IMPR] Let the operator choose misspelled script
- Move code part of finding alternatives of misspelled script to new functions find_alternates - Create a dictionary of all scripts found in all script paths with script name as a key and the path as its value - use ShowingListOption class to show the list of matching scripts and let the operator have the choice to choose one of them or to quit the script
Note: Idf0af67ae must be merged first
Bug: T217195 Change-Id: I0fb163eef3ea3b0ab18782470d4cba4fb0cb6f6e --- M pwb.py 1 file changed, 37 insertions(+), 21 deletions(-)
Approvals: Dvorapa: Looks good to me, approved jenkins-bot: Verified
diff --git a/pwb.py b/pwb.py index 36d2b7b..0fefb5a 100755 --- a/pwb.py +++ b/pwb.py @@ -168,6 +168,42 @@ sys.exit(1)
+def find_alternates(filename, script_paths): + """Search for similar filenames in the given script paths.""" + from pywikibot import input_choice + from pywikibot.bot import ShowingListOption, QuitKeyboardInterrupt + + print('ERROR: {} not found! Misspelling?'.format(filename), + file=sys.stderr) + + scripts = {} + for file_package in script_paths: + path = file_package.split('.') + for script_name in os.listdir(os.path.join(*path)): + # remove .py for better matching + name, _, suffix = script_name.rpartition('.') + if suffix == 'py' and not name.startswith('__'): + scripts[name] = os.path.join(*(path + [script_name])) + + filename = filename[:-3] + similar_scripts = get_close_matches(filename, scripts, n=10, cutoff=0.7) + if not similar_scripts: + return None + + msg = '\nThe most similar script{}:'.format( + ' is' if len(similar_scripts) == 1 else 's are') + alternatives = ShowingListOption(similar_scripts, pre=msg, post='') + + try: + prefix, script = input_choice('Which script to be run:', + alternatives, default='1') + except QuitKeyboardInterrupt: + return None + + print() + return scripts[script[0]] + + def find_filename(filename): """Search for the filename in the given script paths.""" from pywikibot import config @@ -191,27 +227,7 @@ filename = testpath break else: - print('ERROR: {} not found! Misspelling?'.format(filename), - file=sys.stderr) - - scripts = {} - for file_package in script_paths: - path = file_package.split('.') - for script_name in os.listdir(os.path.join(*path)): - if (script_name.endswith('.py') - and not script_name.startswith('__')): - # remove .py for better matching - scripts[script_name[:-3]] = os.path.join( - *(path + [script_name])) - - similar_scripts = get_close_matches(filename[:-3], scripts, - n=10, cutoff=0.7) - if similar_scripts: - print('\nThe most similar script{}:' - .format(' is' if len(similar_scripts) == 1 - else 's are')) - print('\t' + '.py\n\t'.join(similar_scripts) + '.py') - return None + filename = find_alternates(filename, script_paths) return filename