dirstack: small refactoring

This commit is contained in:
adam j hartz 2015-03-21 21:13:11 -04:00
parent 8e3af41a53
commit 1f7024e284

View file

@ -38,7 +38,7 @@ def pushd(args, stdin=None):
if args.dir is None: if args.dir is None:
try: try:
new_pwd = DIRSTACK.pop(0) new_pwd = DIRSTACK.pop(0)
except: except IndexError:
e = 'pushd: Directory stack is empty\n' e = 'pushd: Directory stack is empty\n'
return None, e return None, e
elif os.path.isdir(args.dir): elif os.path.isdir(args.dir):
@ -46,10 +46,14 @@ def pushd(args, stdin=None):
else: else:
try: try:
num = int(args.dir[1:]) num = int(args.dir[1:])
assert num >= 0 except ValueError:
except:
e = 'Invalid argument to pushd: {0}\n' e = 'Invalid argument to pushd: {0}\n'
return None, e.format(args.dir) return None, e.format(args.dir)
if num < 0:
e = 'Invalid argument to pushd: {0}\n'
return None, e.format(args.dir)
if num > len(DIRSTACK): if num > len(DIRSTACK):
e = 'Too few elements in dirstack ({0} elements)\n' e = 'Too few elements in dirstack ({0} elements)\n'
return None, e.format(len(DIRSTACK)) return None, e.format(len(DIRSTACK))
@ -113,16 +117,20 @@ def popd(args, stdin=None):
if args.dir is None: if args.dir is None:
try: try:
new_pwd = DIRSTACK.pop(0) new_pwd = DIRSTACK.pop(0)
except: except IndexError:
e = 'popd: Directory stack is empty\n' e = 'popd: Directory stack is empty\n'
return None, e return None, e
else: else:
try: try:
num = int(args.dir[1:]) num = int(args.dir[1:])
assert num >= 0 except ValueError:
except:
e = 'Invalid argument to popd: {0}\n' e = 'Invalid argument to popd: {0}\n'
return None, e.format(args.dir) return None, e.format(args.dir)
if num < 0:
e = 'Invalid argument to popd: {0}\n'
return None, e.format(args.dir)
if num > len(DIRSTACK): if num > len(DIRSTACK):
e = 'Too few elements in dirstack ({0} elements)\n' e = 'Too few elements in dirstack ({0} elements)\n'
return None, e.format(len(DIRSTACK)) return None, e.format(len(DIRSTACK))
@ -207,13 +215,18 @@ def dirs(args, stdin=None):
if N is not None: if N is not None:
try: try:
num = int(N[1:]) num = int(N[1:])
assert num >= 0 except ValueError:
except:
e = 'Invalid argument to dirs: {0}\n' e = 'Invalid argument to dirs: {0}\n'
return None, e.format(N) return None, e.format(N)
if num < 0:
e = 'Invalid argument to dirs: {0}\n'
return None, e.format(len(o))
if num >= len(o): if num >= len(o):
e = 'Too few elements in dirstack ({0} elements)\n' e = 'Too few elements in dirstack ({0} elements)\n'
return None, e.format(len(o)) return None, e.format(len(o))
if N.startswith(BACKWARD): if N.startswith(BACKWARD):
idx = num idx = num
elif N.startswith(FORWARD): elif N.startswith(FORWARD):