<s> import sys <EOL> import os <EOL> try : import readline <EOL> except : pass <EOL> __doc__ = "<STR_LIT>" <EOL> def apply_check ( check , input ) : <EOL> result = check ( input ) <EOL> if result is None : <EOL> return True <EOL> elif result is True : <EOL> print "<STR_LIT>" <EOL> print <EOL> else : <EOL> print "<STR_LIT>" % result <EOL> print <EOL> return False <EOL> def yes_or_no ( prompt , default = None ) : <EOL> prompt = "<STR_LIT>" % ( prompt , "<STR_LIT:Y>" if default is True else "<STR_LIT:y>" , "<STR_LIT:N>" if default is False else "<STR_LIT:n>" ) <EOL> while True : <EOL> try : input = raw_input ( prompt ) <EOL> except KeyboardInterrupt : <EOL> print <EOL> raise <EOL> if input . lower ( ) in ( "<STR_LIT:y>" , "<STR_LIT:yes>" ) : <EOL> return True <EOL> elif input . lower ( ) in ( "<STR_LIT:n>" , "<STR_LIT>" ) : <EOL> return False <EOL> elif input or default is None : <EOL> print "<STR_LIT>" <EOL> print <EOL> else : <EOL> return default <EOL> def string ( prompt , default = None , check = None ) : <EOL> prompt = "<STR_LIT>" % ( prompt , ( "<STR_LIT>" % default ) if default is not None else "<STR_LIT>" ) <EOL> while True : <EOL> try : input = raw_input ( prompt ) <EOL> except KeyboardInterrupt : <EOL> print <EOL> raise <EOL> if default and not input : <EOL> input = default <EOL> if check : <EOL> if apply_check ( check , input ) : <EOL> return input <EOL> elif not input : <EOL> print "<STR_LIT>" <EOL> else : <EOL> return input <EOL> def password ( prompt , default = None , twice = True ) : <EOL> import termios <EOL> prompt = "<STR_LIT>" % ( prompt , "<STR_LIT>" if default is not None else "<STR_LIT>" ) <EOL> def internal ( prompt ) : <EOL> if os . isatty ( sys . stdin . fileno ( ) ) : <EOL> old = termios . tcgetattr ( sys . stdin ) <EOL> new = old [ : ] <EOL> new [ <NUM_LIT:3> ] = new [ <NUM_LIT:3> ] & ~ termios . ECHO <EOL> try : <EOL> termios . tcsetattr ( sys . stdin , termios . TCSADRAIN , new ) <EOL> try : password = raw_input ( prompt ) <EOL> except KeyboardInterrupt : <EOL> print <EOL> raise <EOL> finally : <EOL> termios . tcsetattr ( sys . stdin , termios . TCSADRAIN , old ) <EOL> else : <EOL> password = sys . stdin . readline ( ) . rstrip ( "<STR_LIT:\n>" ) <EOL> print <EOL> if default and not password : return default <EOL> else : return password <EOL> while True : <EOL> password = internal ( prompt ) <EOL> if twice : <EOL> andagain = internal ( "<STR_LIT>" ) <EOL> if password == andagain : <EOL> return password <EOL> else : <EOL> print <EOL> print "<STR_LIT>" <EOL> print <EOL> else : <EOL> return password </s>
