From bdb749aefe0b05824a885e39a4414fe4d5321f64 Mon Sep 17 00:00:00 2001 From: Casper da Costa-Luis Date: Fri, 22 Apr 2016 15:23:55 +0100 Subject: [PATCH] py26 test fix, corner cases fix --- tqdm/_main.py | 24 ++++++++++++++---------- tqdm/tests/tests_main.py | 24 +++++++++++++++++++++--- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/tqdm/_main.py b/tqdm/_main.py index 3634c8ad9..aab61ef9a 100644 --- a/tqdm/_main.py +++ b/tqdm/_main.py @@ -1,4 +1,3 @@ -from __future__ import print_function from ._tqdm import tqdm from ._version import __version__ # NOQA import sys @@ -8,14 +7,19 @@ def cast(val, typ): if typ == 'bool': - return (str(val) == 'True') or not str(val) - return eval(typ + '("' + str(val) + '")') + # sys.stderr.write('\ndebug | `val:type`: `' + val + ':' + typ + '`.\n') + if (val == 'True') or (val == ''): + return True + elif val == 'False': + return False + else: + raise ValueError(val + ' : ' + typ) + + return eval(typ + '("' + val + '")') -# Don't have to worry about Python 2.6 not supporting re flags -# since it does not support executing modules either. # RE_OPTS = re.compile(r' {8}(\S+)\s{2,}:\s*(str|int|float|bool)', flags=re.M) -RE_OPTS = re.compile(r' {8}(\S+)\s{2,}:\s*([^\s,]+)', flags=re.M) +RE_OPTS = re.compile(r'\n {8}(\S+)\s{2,}:\s*([^\s,]+)') # TODO: add custom support for some of the following? UNSUPPORTED_OPTS = ('iterable', 'gui', 'out', 'file') @@ -32,7 +36,7 @@ def main(): # d = RE_OPTS.sub(r' --\1=<\1> : \2', d) split = RE_OPTS.split(d) opt_types_desc = zip(split[1::3], split[2::3], split[3::3]) - d = ''.join(' --{0}=<{0}> : {1}{2}'.format(*otd) + d = ''.join('\n --{0}=<{0}> : {1}{2}'.format(*otd) for otd in opt_types_desc if otd[0] not in UNSUPPORTED_OPTS) __doc__ = """Usage: @@ -52,18 +56,18 @@ def main(): sys.stdout.write(__doc__ + '\n') sys.exit(0) - argv = re.split('(--\S+)[=\s]*', ' '.join(sys.argv[1:])) + argv = re.split('\s*(--\S+)[=\s]*', ' '.join(sys.argv[1:])) opts = dict(zip(argv[1::2], argv[2::2])) tqdm_args = {} try: for (o, v) in opts.items(): tqdm_args[o[2:]] = cast(v, opt_types[o[2:]]) - # print('debug |', tqdm_args) + # sys.stderr.write('\ndebug | args: ' + str(tqdm_args) + '\n') for i in tqdm(sys.stdin, **tqdm_args): sys.stdout.write(i) except: # pragma: no cover + sys.stderr.write('\nError:\nUsage:\n tqdm [--help | options]\n') for i in sys.stdin: sys.stdout.write(i) - sys.stderr.write('\nUsage:\n tqdm [--help | options]\n') raise diff --git a/tqdm/tests/tests_main.py b/tqdm/tests/tests_main.py index e7daf86bf..cba21e679 100644 --- a/tqdm/tests/tests_main.py +++ b/tqdm/tests/tests_main.py @@ -32,18 +32,36 @@ def test_main(): pass sys.stdin = map(str, _range(int(1e3))) - sys.argv = ['', '--desc', 'Test command line pipes', + sys.argv = ['', '--desc', 'Test CLI pipes', '--ascii', 'True', '--unit_scale', 'True'] import tqdm.__main__ # NOQA - sys.argv = ['', '--bad_arg_u_ment', 'foo', - '--ascii', 'True', '--unit_scale', 'True'] + sys.argv = ['', '--ascii', '--unit_scale', 'False', + '--desc', 'Test CLI errors'] + main() + + sys.argv = ['', '--bad_arg_u_ment', 'foo', '--ascii', '--unit_scale'] try: main() except KeyError as e: if 'bad_arg_u_ment' not in str(e): raise + sys.argv = ['', '--ascii', '--unit_scale', 'invalid_bool_value'] + try: + main() + except ValueError as e: + if 'invalid_bool_value' not in str(e): + raise + + + sys.argv = ['', '--ascii', '--total', 'invalid_int_value'] + try: + main() + except ValueError as e: + if 'invalid_int_value' not in str(e): + raise + for i in ('-h', '--help', '-v', '--version'): sys.argv = ['', i] try: