#!/usr/bin/env python
# vim:ts=4:sts=4:sw=4:noet
-from common import load_root_node
+from common import load_root_node, natural_sort
import sys
def choose(options, allow_multi):
- skeys = sorted(options.keys())
- for i, key in enumerate(skeys):
- print " %d) %s" % (i+1, key)
- print " 0) Back"
+ reverse_map = {}
+ for i, (key, value) in enumerate(options):
+ print "%3d) %s" % (i+1, key)
+ reverse_map[i+1] = value
+ print " 0) Back"
while True:
try:
values = map(int, raw_input("Choose> ").split())
continue
if 0 in values:
return
- values = [options[skeys[value-1]] for value in values]
+ values = [reverse_map[value] for value in values]
if allow_multi:
return values
else:
node = load_root_node()
while True:
- options = {}
+ options = []
will_download = True
for n in node.get_children():
- options[n.title] = n
+ options.append((n.title, n))
if not n.can_download:
will_download = False
+ if node.sort_children:
+ options = natural_sort(options, key=lambda x: x[0])
result = choose(options, allow_multi=will_download)
if result is None:
if node.parent is not None: