Skip to content

Commit

Permalink
py26 test fix, corner cases fix
Browse files Browse the repository at this point in the history
  • Loading branch information
casperdcl committed Apr 22, 2016
1 parent d632b1a commit bdb749a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
24 changes: 14 additions & 10 deletions tqdm/_main.py
@@ -1,4 +1,3 @@
from __future__ import print_function
from ._tqdm import tqdm
from ._version import __version__ # NOQA
import sys
Expand All @@ -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')
Expand All @@ -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:
Expand All @@ -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
24 changes: 21 additions & 3 deletions tqdm/tests/tests_main.py
Expand Up @@ -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:
Expand Down

0 comments on commit bdb749a

Please sign in to comment.