def render_context(context, output_folder_path): cookiecutter_dict = OrderedDict() # inject the output folder path at the beginning, so all variables can refer to it cookiecutter_dict['_output_folder_path'] = output_folder_path env = StrictEnvironment(context=context) for key, raw in iteritems(context['cookiecutter']): if key.startswith('_'): # unlike cookiecutter's prompt_for_config, we render internal variables cookiecutter_dict[key] = render_obj(env, raw, cookiecutter_dict) continue try: if isinstance(raw, list): # We are dealing with a choice variable val = prompt_choice_for_config(cookiecutter_dict, env, key, raw, no_input=True) else: # We are dealing with a regular variable val = render_variable(env, raw, cookiecutter_dict) except UndefinedError as err: msg = "Unable to render variable '{}'".format(key) raise UndefinedVariableInTemplate(msg, err, context) cookiecutter_dict[key] = val return {'cookiecutter': cookiecutter_dict}
def render_context(context, output_folder_path): cookiecutter_dict = OrderedDict() # inject the output folder path at the beginning, so all variables can refer to it cookiecutter_dict['_output_folder_path'] = output_folder_path; env = StrictEnvironment(context=context) for key, raw in iteritems(context['cookiecutter']): if key.startswith('_'): # unlike cookiecutter's prompt_for_config, we render internal variables cookiecutter_dict[key] = render_obj(env, raw, cookiecutter_dict) continue try: if isinstance(raw, list): # We are dealing with a choice variable val = prompt_choice_for_config( cookiecutter_dict, env, key, raw, no_input=True ) else: # We are dealing with a regular variable val = render_variable(env, raw, cookiecutter_dict) except UndefinedError as err: msg = "Unable to render variable '{}'".format(key) raise UndefinedVariableInTemplate(msg, err, context) cookiecutter_dict[key] = val return { 'cookiecutter' : cookiecutter_dict }
def prompt_for_config(context, no_input=False): """ Prompts the user to enter new config, using context as a source for the field names and sample values. :param no_input: Prompt the user at command line for manual configuration? """ cookiecutter_dict = OrderedDict([]) env = StrictEnvironment(context=context) # First pass: Handle simple and raw variables, plus choices. # These must be done first because the dictionaries keys and # values might refer to them. for key, raw in iteritems(context[u'cookiecutter']): if key.startswith(u'_'): cookiecutter_dict[key] = raw continue try: if isinstance(raw, list): if isinstance(raw[0], dict): val = _prompt_choice_and_subitems( cookiecutter_dict, env, key, raw, no_input ) cookiecutter_dict[key] = val else: # We are dealing with a choice variable val = prompt_choice_for_config( cookiecutter_dict, env, key, raw, no_input ) cookiecutter_dict[key] = val elif not isinstance(raw, dict): # We are dealing with a regular variable val = render_variable(env, raw, cookiecutter_dict) if not no_input: val = read_user_variable(key, val) cookiecutter_dict[key] = val except UndefinedError as err: msg = "Unable to render variable '{}'".format(key) raise UndefinedVariableInTemplate(msg, err, context) # Second pass; handle the dictionaries. for key, raw in iteritems(context[u'cookiecutter']): try: if isinstance(raw, dict): # We are dealing with a dict variable val = render_variable(env, raw, cookiecutter_dict) if not no_input: val = read_user_dict(key, val) cookiecutter_dict[key] = val except UndefinedError as err: msg = "Unable to render variable '{}'".format(key) raise UndefinedVariableInTemplate(msg, err, context) return cookiecutter_dict
def test_should_return_first_option_if_no_input(self, mocker, choices, context): read_choice = mocker.patch("cookiecutter.prompt.read_user_choice") expected_choice = choices[0] actual_choice = prompt.prompt_choice_for_config( context, Environment(), "orientation", choices, True # Suppress user input ) assert not read_choice.called assert expected_choice == actual_choice
def test_should_read_userchoice(self, mocker, choices, context): read_choice = mocker.patch("cookiecutter.prompt.read_user_choice") read_choice.return_value = "all" expected_choice = "all" actual_choice = prompt.prompt_choice_for_config( context, Environment(), "orientation", choices, False # Ask the user for input ) read_choice.assert_called_once_with("orientation", choices) assert expected_choice == actual_choice
def test_should_read_userchoice(self, mocker, choices, context): read_choice = mocker.patch('cookiecutter.prompt.read_user_choice') read_choice.return_value = 'all' expected_choice = 'all' actual_choice = prompt.prompt_choice_for_config( context, environment.StrictEnvironment(), 'orientation', choices, False # Ask the user for input ) read_choice.assert_called_once_with('orientation', choices) assert expected_choice == actual_choice
def test_should_return_first_option_if_no_input(self, mocker, choices, context): read_choice = mocker.patch('cookiecutter.prompt.read_user_choice') expected_choice = choices[0] actual_choice = prompt.prompt_choice_for_config( context, environment.StrictEnvironment(), 'orientation', choices, True # Suppress user input ) assert not read_choice.called assert expected_choice == actual_choice
def test_should_read_user_choice(self, mocker, choices, context): """Verify prompt_choice_for_config return user selection on no_input=False.""" read_user_choice = mocker.patch('cookiecutter.prompt.read_user_choice') read_user_choice.return_value = 'all' expected_choice = 'all' actual_choice = prompt.prompt_choice_for_config( cookiecutter_dict=context, env=environment.StrictEnvironment(), key='orientation', options=choices, no_input=False, # Ask the user for input ) read_user_choice.assert_called_once_with('orientation', choices) assert expected_choice == actual_choice
def test_should_return_first_option_if_no_input(self, mocker, choices, context): """Verify prompt_choice_for_config return first list option on no_input=True.""" read_user_choice = mocker.patch('cookiecutter.prompt.read_user_choice') expected_choice = choices[0] actual_choice = prompt.prompt_choice_for_config( cookiecutter_dict=context, env=environment.StrictEnvironment(), key='orientation', options=choices, no_input=True, # Suppress user input ) assert not read_user_choice.called assert expected_choice == actual_choice
def _prompt_choice_and_subitems(cookiecutter_dict, env, key, options, no_input): result = {} # first, get the selection rendered_options = [ render_variable(env, list(raw.keys())[0], cookiecutter_dict) for raw in options ] if no_input: selected = rendered_options[0] else: selected = read_user_choice(key, rendered_options) selected_item = [ list(c.values())[0] for c in options if list(c.keys())[0] == selected ][0] result[selected] = {} # then, fill in the sub values for that item if isinstance(selected_item, dict): for subkey, raw in selected_item.items(): # We are dealing with a regular variable val = render_variable(env, raw, cookiecutter_dict) if not no_input: val = read_user_variable(subkey, val) result[selected][subkey] = val elif isinstance(selected_item, list): val = prompt_choice_for_config(cookiecutter_dict, env, selected, selected_item, no_input) result[selected] = val elif isinstance(selected_item, str): result[selected] = selected_item return result