<s> import colorama <EOL> def query_options ( menu_items , question = '<STR_LIT>' , default = None ) : <EOL> numbers = range ( <NUM_LIT:1> , len ( menu_items ) + <NUM_LIT:1> ) <EOL> selectors = [ '<STR_LIT:%s>' % n for n in numbers ] <EOL> def _unique_selector ( name ) : <EOL> for letter in name . lower ( ) : <EOL> if letter not in selectors : <EOL> selectors . append ( letter ) <EOL> coloring = { <EOL> '<STR_LIT>' : colorama . Style . BRIGHT , <EOL> '<STR_LIT:message>' : letter . upper ( ) , <EOL> '<STR_LIT>' : colorama . Style . RESET_ALL <EOL> } <EOL> c = '<STR_LIT>' % coloring <EOL> new_name = name . lower ( ) . replace ( letter , c , <NUM_LIT:1> ) <EOL> return letter , new_name <EOL> raise Exception ( "<STR_LIT>" ) <EOL> def _display_options ( ) : <EOL> for i in numbers : <EOL> key , new_name = _unique_selector ( menu_items [ i - <NUM_LIT:1> ] [ '<STR_LIT:name>' ] ) <EOL> menu_items [ i - <NUM_LIT:1> ] [ '<STR_LIT:key>' ] = key <EOL> menu_items [ i - <NUM_LIT:1> ] [ '<STR_LIT>' ] = '<STR_LIT:%s>' % ( i ) <EOL> description = menu_items [ i - <NUM_LIT:1> ] . get ( '<STR_LIT:description>' , '<STR_LIT>' ) <EOL> print "<STR_LIT>" % i + new_name + "<STR_LIT>" % description <EOL> _display_options ( ) <EOL> ids = dict ( [ ( item [ '<STR_LIT:id>' ] , item ) for item in menu_items ] ) <EOL> keys = dict ( [ ( item [ '<STR_LIT:key>' ] , item ) for item in menu_items ] ) <EOL> numbers = dict ( [ ( item [ '<STR_LIT>' ] , item ) for item in menu_items ] ) <EOL> names = dict ( [ ( item [ '<STR_LIT:name>' ] . lower ( ) , item ) for item in menu_items ] ) <EOL> tests = ( keys , numbers , names ) <EOL> def _selection ( ) : <EOL> if default : <EOL> choice = raw_input ( "<STR_LIT>" % ( question , ids [ default ] [ '<STR_LIT:name>' ] ) ) <EOL> else : <EOL> choice = raw_input ( "<STR_LIT>" % ( question ) ) <EOL> choice = choice . strip ( ) . lower ( ) <EOL> if not choice and default : <EOL> return ids [ default ] <EOL> if not choice : <EOL> print "<STR_LIT>" <EOL> _selection ( ) <EOL> for test in tests : <EOL> if choice in test : <EOL> print test [ choice ] [ '<STR_LIT:description>' ] <EOL> return test [ choice ] <EOL> print "<STR_LIT>" <EOL> return _selection ( ) <EOL> return _selection ( ) <EOL> def query_yes_no ( question , default = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> valid = { "<STR_LIT:yes>" : "<STR_LIT:yes>" , "<STR_LIT:y>" : "<STR_LIT:yes>" , "<STR_LIT>" : "<STR_LIT:yes>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:n>" : "<STR_LIT>" } <EOL> exact = False <EOL> if default == None : <EOL> exact = True <EOL> if exact : <EOL> prompt = ( "<STR_LIT:yes>" , "<STR_LIT>" ) <EOL> valid = { "<STR_LIT:yes>" : "<STR_LIT:yes>" , "<STR_LIT>" : "<STR_LIT>" } <EOL> elif not default : <EOL> prompt = ( '<STR_LIT:y>' , '<STR_LIT:n>' ) <EOL> elif default == "<STR_LIT:y>" : <EOL> prompt = ( '<STR_LIT:Y>' , '<STR_LIT:n>' ) <EOL> elif default == "<STR_LIT:n>" : <EOL> prompt = ( '<STR_LIT:y>' , '<STR_LIT:N>' ) <EOL> elif default == "<STR_LIT:yes>" : <EOL> prompt = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> elif default == "<STR_LIT>" : <EOL> prompt = ( '<STR_LIT:yes>' , '<STR_LIT>' ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % default ) <EOL> while <NUM_LIT:1> : <EOL> print question + "<STR_LIT>" % ( prompt [ <NUM_LIT:0> ] , prompt [ <NUM_LIT:1> ] ) <EOL> choice = raw_input ( ) . lower ( ) <EOL> if exact and choice in valid : <EOL> return choice <EOL> elif default and choice == '<STR_LIT>' : <EOL> return valid [ default ] <EOL> elif not exact and choice in valid . keys ( ) : <EOL> return valid [ choice ] <EOL> else : <EOL> print "<STR_LIT>" % ( prompt [ <NUM_LIT:0> ] . lower ( ) , prompt [ <NUM_LIT:1> ] . lower ( ) ) <EOL> def query_true_false ( question , default = '<STR_LIT>' ) : <EOL> if default == True : <EOL> default = '<STR_LIT:y>' <EOL> elif default == False : <EOL> default = '<STR_LIT:n>' <EOL> if query_yes_no ( question , default ) == '<STR_LIT:yes>' : <EOL> return True <EOL> return False </s>
