jenkins-bot merged this change.

View Change

Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
[IMPR] Reduce code complexity of pwb.main for further improvements

Introduce a new function find_filename to look for the given filename
and return its path. The code was moved from main().

This change will be used for further improvements.

Change-Id: Idf0af67ae2ca0513cdba1acfb60ef956dcc85b4e
---
M pwb.py
1 file changed, 50 insertions(+), 38 deletions(-)

diff --git a/pwb.py b/pwb.py
index a07690a..36d2b7b 100755
--- a/pwb.py
+++ b/pwb.py
@@ -168,6 +168,53 @@
sys.exit(1)


+def find_filename(filename):
+ """Search for the filename in the given script paths."""
+ from pywikibot import config
+
+ script_paths = ['scripts',
+ 'scripts.maintenance',
+ 'scripts.userscripts']
+
+ if config.user_script_paths:
+ if isinstance(config.user_script_paths, (tuple, list)):
+ script_paths = config.user_script_paths + script_paths
+ else:
+ warn("'user_script_paths' must be a list or tuple,\n"
+ 'found: {0}. Ignoring this setting.'
+ .format(type(config.user_script_paths)))
+
+ for file_package in script_paths:
+ paths = file_package.split('.') + [filename]
+ testpath = os.path.join(_pwb_dir, *paths)
+ if os.path.exists(testpath):
+ 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
+ return filename
+
+
def main():
"""Command line entry point."""
global filename
@@ -176,45 +223,10 @@

file_package = None
argvu = pwb.argvu[1:]
+
if not os.path.exists(filename):
- script_paths = ['scripts',
- 'scripts.maintenance',
- 'scripts.userscripts']
- from pywikibot import config
- if config.user_script_paths:
- if isinstance(config.user_script_paths, (tuple, list)):
- script_paths = config.user_script_paths + script_paths
- else:
- warn("'user_script_paths' must be a list or tuple,\n"
- 'found: {0}. Ignoring this setting.'
- ''.format(type(config.user_script_paths)))
- for file_package in script_paths:
- paths = file_package.split('.') + [filename]
- testpath = os.path.join(_pwb_dir, *paths)
- if os.path.exists(testpath):
- 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')
+ filename = find_filename(filename)
+ if filename is None:
return True

# When both pwb.py and the filename to run are within the current

To view, visit change 510077. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: Idf0af67ae2ca0513cdba1acfb60ef956dcc85b4e
Gerrit-Change-Number: 510077
Gerrit-PatchSet: 2
Gerrit-Owner: Xqt <info@gno.de>
Gerrit-Reviewer: John Vandenberg <jayvdb@gmail.com>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: jenkins-bot (75)
Gerrit-CC: Dvorapa <dvorapa@seznam.cz>