From ef75ea068ca5610c4017af65b7882bb77e5ab316 Mon Sep 17 00:00:00 2001 From: Pedro Algarvio Date: Sun, 12 Jul 2015 12:22:28 +0100 Subject: [PATCH 1/5] Lint fixes to `tests/modparser.py` --- tests/modparser.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/modparser.py b/tests/modparser.py index 0c299588d2..767f20e865 100644 --- a/tests/modparser.py +++ b/tests/modparser.py @@ -1,6 +1,12 @@ +# -*- coding: utf-8 -*- #!/usr/bin/env python2 -import argparse +from __future__ import absolute_import, print_function +try: + import argparse # pylint: disable=minimum-python-version + HAS_ARGPARSE = True +except ImportError: + HAS_ARGPARSE = False import os import sys import os @@ -43,7 +49,7 @@ def mod_data(opts, full): try: finder.load_file(full) except ImportError: - sys.stderr.write('ImportError - {}\n'.format(full)) + sys.stderr.write('ImportError - {0}\n'.format(full)) return ret for name, mod in finder.modules.items(): basemod = name.split('.')[0] @@ -82,6 +88,8 @@ def scan(opts): if __name__ == '__main__': + if not HAS_ARGPARSE: + print('The argparse python module is required') opts = parse() scand = scan(opts) if opts['format'] == 'yaml': From e6df8cbe240d880d89f142d559ca385580115113 Mon Sep 17 00:00:00 2001 From: Pedro Algarvio Date: Sun, 12 Jul 2015 12:27:26 +0100 Subject: [PATCH 2/5] Add allowed choices to output format. Since we're using the print function, we can specify the file handler to user and in this case we choose `sys.stderr` --- tests/modparser.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/modparser.py b/tests/modparser.py index 767f20e865..9ac0bb1f30 100644 --- a/tests/modparser.py +++ b/tests/modparser.py @@ -36,6 +36,7 @@ def parse(): '-f', '--format', dest='format', + choices=('pprint', 'yaml'), default='pprint') out = parser.parse_args() return out.__dict__ @@ -49,7 +50,7 @@ def mod_data(opts, full): try: finder.load_file(full) except ImportError: - sys.stderr.write('ImportError - {0}\n'.format(full)) + print('ImportError - {0}'.format(full), file=sys.stderr) return ret for name, mod in finder.modules.items(): basemod = name.split('.')[0] From 0add87e7b931bd6e206d8e677863b1cc7b34aa42 Mon Sep 17 00:00:00 2001 From: Pedro Algarvio Date: Sun, 12 Jul 2015 12:30:24 +0100 Subject: [PATCH 3/5] Display the failed import reason --- tests/modparser.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/modparser.py b/tests/modparser.py index 9ac0bb1f30..3225067cca 100644 --- a/tests/modparser.py +++ b/tests/modparser.py @@ -49,8 +49,8 @@ def mod_data(opts, full): finder = modulefinder.ModuleFinder() try: finder.load_file(full) - except ImportError: - print('ImportError - {0}'.format(full), file=sys.stderr) + except ImportError as exc: + print('ImportError - {0} (Reason: {1})'.format(full, exc), file=sys.stderr) return ret for name, mod in finder.modules.items(): basemod = name.split('.')[0] From 6294c67b129066b4ae99e0f301c3fef2d9a1aff1 Mon Sep 17 00:00:00 2001 From: Pedro Algarvio Date: Sun, 12 Jul 2015 12:37:56 +0100 Subject: [PATCH 4/5] Nicer handling of user keyboard interruption --- tests/modparser.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/tests/modparser.py b/tests/modparser.py index 3225067cca..d5fb60efd8 100644 --- a/tests/modparser.py +++ b/tests/modparser.py @@ -78,6 +78,7 @@ def mod_data(opts, full): def scan(opts): ''' + Scan the provided root for python source files ''' ret = {} for root, dirs, files in os.walk(opts['root']): @@ -92,10 +93,15 @@ if __name__ == '__main__': if not HAS_ARGPARSE: print('The argparse python module is required') opts = parse() - scand = scan(opts) - if opts['format'] == 'yaml': - print(yaml.dump(scand)) - if opts['format'] == 'json': - print(json.dumps(scand)) - else: - pprint.pprint(scand) + try: + scand = scan(opts) + if opts['format'] == 'yaml': + print(yaml.dump(scand)) + if opts['format'] == 'json': + print(json.dumps(scand)) + else: + pprint.pprint(scand) + exit(0) + except KeyboardInterrupt: + print('\nInterrupted on user request', file=sys.stderr) + exit(1) From 2b14375b30c13386427bc2e4441d9c62ff414290 Mon Sep 17 00:00:00 2001 From: Pedro Algarvio Date: Sun, 12 Jul 2015 19:26:33 +0100 Subject: [PATCH 5/5] No empty docstrings --- tests/modparser.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/modparser.py b/tests/modparser.py index d5fb60efd8..7e0aac21e4 100644 --- a/tests/modparser.py +++ b/tests/modparser.py @@ -44,6 +44,7 @@ def parse(): def mod_data(opts, full): ''' + Grab the module's data ''' ret = {} finder = modulefinder.ModuleFinder()