<s> """<STR_LIT>""" <EOL> from collections import OrderedDict <EOL> import click <EOL> from past . builtins import basestring <EOL> from future . utils import iteritems <EOL> from jinja2 . exceptions import UndefinedError <EOL> from . exceptions import UndefinedVariableInTemplate <EOL> from . environment import StrictEnvironment <EOL> def read_user_variable ( var_name , default_value ) : <EOL> """<STR_LIT>""" <EOL> return click . prompt ( var_name , default = default_value ) <EOL> def read_user_yes_no ( question , default_value ) : <EOL> """<STR_LIT>""" <EOL> return click . prompt ( <EOL> question , <EOL> default = default_value , <EOL> type = click . BOOL <EOL> ) <EOL> def read_user_choice ( var_name , options ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( options , list ) : <EOL> raise TypeError <EOL> if not options : <EOL> raise ValueError <EOL> choice_map = OrderedDict ( <EOL> ( u'<STR_LIT:{}>' . format ( i ) , value ) for i , value in enumerate ( options , <NUM_LIT:1> ) <EOL> ) <EOL> choices = choice_map . keys ( ) <EOL> default = u'<STR_LIT:1>' <EOL> choice_lines = [ u'<STR_LIT>' . format ( * c ) for c in choice_map . items ( ) ] <EOL> prompt = u'<STR_LIT:\n>' . join ( ( <EOL> u'<STR_LIT>' . format ( var_name ) , <EOL> u'<STR_LIT:\n>' . join ( choice_lines ) , <EOL> u'<STR_LIT>' . format ( u'<STR_LIT:U+002CU+0020>' . join ( choices ) ) <EOL> ) ) <EOL> user_choice = click . prompt ( <EOL> prompt , type = click . Choice ( choices ) , default = default <EOL> ) <EOL> return choice_map [ user_choice ] <EOL> def render_variable ( env , raw , cookiecutter_dict ) : <EOL> if raw is None : <EOL> return None <EOL> if not isinstance ( raw , basestring ) : <EOL> raw = str ( raw ) <EOL> template = env . from_string ( raw ) <EOL> rendered_template = template . render ( cookiecutter = cookiecutter_dict ) <EOL> return rendered_template <EOL> def prompt_choice_for_config ( cookiecutter_dict , env , key , options , no_input ) : <EOL> """<STR_LIT>""" <EOL> rendered_options = [ <EOL> render_variable ( env , raw , cookiecutter_dict ) for raw in options <EOL> ] <EOL> if no_input : <EOL> return rendered_options [ <NUM_LIT:0> ] <EOL> return read_user_choice ( key , rendered_options ) <EOL> def prompt_for_config ( context , no_input = False ) : <EOL> """<STR_LIT>""" <EOL> cookiecutter_dict = { } <EOL> env = StrictEnvironment ( context = context ) <EOL> for key , raw in iteritems ( context [ u'<STR_LIT>' ] ) : <EOL> if key . startswith ( u'<STR_LIT:_>' ) : <EOL> cookiecutter_dict [ key ] = raw <EOL> continue <EOL> try : <EOL> if isinstance ( raw , list ) : <EOL> val = prompt_choice_for_config ( <EOL> cookiecutter_dict , env , key , raw , no_input <EOL> ) <EOL> else : <EOL> val = render_variable ( env , raw , cookiecutter_dict ) <EOL> if not no_input : <EOL> val = read_user_variable ( key , val ) <EOL> except UndefinedError as err : <EOL> msg = "<STR_LIT>" . format ( key ) <EOL> raise UndefinedVariableInTemplate ( msg , err , context ) <EOL> cookiecutter_dict [ key ] = val <EOL> return cookiecutter_dict </s>
