Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_convert_to_str(mocker, raw_var, rendered_var):
env = environment.StrictEnvironment()
from_string = mocker.patch(
'cookiecutter.prompt.StrictEnvironment.from_string',
wraps=env.from_string
)
context = {'project': 'foobar'}
result = prompt.render_variable(env, raw_var, context)
assert result == rendered_var
# Make sure that non None non str variables are converted beforehand
if raw_var is not None:
if not isinstance(raw_var, basestring):
raw_var = str(raw_var)
from_string.assert_called_once_with(raw_var)
else:
assert not from_string.called
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]
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
for subkey, raw in selected_item.items():
# We are dealing with a regular variable
val = render_variable(env, raw, cookiecutter_dict)
def render_obj(env, o, cookiecutter_dict):
if isinstance(o, list):
return render_list(env, o, cookiecutter_dict)
elif isinstance(o, dict):
return render_dict(env, o, cookiecutter_dict)
else:
return render_variable(env, o, cookiecutter_dict)
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 }
render_variable(env, list(raw.keys())[0], cookiecutter_dict) for raw in options
]
if no_input:
selected = rendered_options[0]
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
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
return result
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)