<s> from __future__ import print_function <EOL> from six . moves import urllib <EOL> from six . moves import input <EOL> class InvalidInput ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , reason = "<STR_LIT>" ) : <EOL> self . reason = reason <EOL> def prompt ( q , coerce_fn = None , error_msg = "<STR_LIT>" , prompt_fn = input ) : <EOL> """<STR_LIT>""" <EOL> if coerce_fn is None : <EOL> coerce_fn = lambda x : x <EOL> while <NUM_LIT:1> : <EOL> r = prompt_fn ( q + "<STR_LIT>" ) <EOL> try : <EOL> return coerce_fn ( r ) <EOL> except InvalidInput as e : <EOL> print ( e . reason or error_msg ) <EOL> _yes_no_mapping = { "<STR_LIT:y>" : True , "<STR_LIT:n>" : False } <EOL> def _yes_no_coerce_fn ( r ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return _yes_no_mapping [ r . lower ( ) ] <EOL> except KeyError : <EOL> raise InvalidInput ( "<STR_LIT>" ) <EOL> def prompt_yes_no ( q ) : <EOL> """<STR_LIT>""" <EOL> return prompt ( q + "<STR_LIT>" , _yes_no_coerce_fn ) <EOL> def _normalized_coerce_fn ( r ) : <EOL> """<STR_LIT>""" <EOL> return r . lower ( ) . strip ( ) <EOL> def prompt_normalized ( q ) : <EOL> """<STR_LIT>""" <EOL> return prompt ( q , _normalized_coerce_fn ) <EOL> def _url_coerce_fn ( r ) : <EOL> """<STR_LIT>""" <EOL> p = urllib . parse . urlparse ( r ) <EOL> if not p . scheme : <EOL> raise InvalidInput ( "<STR_LIT>" ) <EOL> if not p . netloc : <EOL> raise InvalidInput ( "<STR_LIT>" ) <EOL> if p . path and p . path != "<STR_LIT:/>" : <EOL> raise InvalidInput ( "<STR_LIT>" ) <EOL> if p . params or p . query or p . fragment : <EOL> raise InvalidInput ( "<STR_LIT>" ) <EOL> if not p . path : <EOL> r += "<STR_LIT:/>" <EOL> r = r . lower ( ) <EOL> return r <EOL> def prompt_url ( q ) : <EOL> """<STR_LIT>""" <EOL> return prompt ( q , _url_coerce_fn ) </s>
