<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
cookiecutter.prompt
---------------------

Functions for prompting the user for project info.
"""

from __future__ import unicode_literals
import sys

import click

from .compat import iteritems, is_string
from jinja2.environment import Environment


def read_user_variable(var_name, default_value):
    """Prompt the user for the given variable and return the entered value
    or the given default.

    :param str var_name: Variable of the context to query the user
    :param default_value: Value that will be returned if no input happens
    """
    return click.prompt(var_name, default=default_value)


def read_response(prompt=''):
    """Prompt the user and return the entered value or an empty string.

    :param str prompt: Text to display to the user
    """
    # Please see http://click.pocoo.org/4/api/#click.prompt
    # default: use an empty string if no input happens
    # prompt_suffix: do not add a suffix to the prompt
    # show_default: the prompt is expected to mention the default itself
    return click.prompt(
        prompt,
        default='',
        prompt_suffix='',
        show_default=False,
    )


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 = {}
    env = Environment()

    for key, raw in iteritems(context['cookiecutter']):
        if key.startswith('_'):
            cookiecutter_dict[key] = raw
            continue

        raw = raw if is_string(raw) else str(raw)
        val = env.from_string(raw).render(cookiecutter=cookiecutter_dict)

        if not no_input:
            prompt = '{0} (default is "{1}")? '.format(key, val)

            new_val = read_response(prompt).strip()

            if new_val != '':
                val = new_val

        cookiecutter_dict[key] = val
    return cookiecutter_dict


def query_yes_no(question, default='yes'):
    """
    Ask a yes/no question via `read_response()` and return their answer.

    :param question: A string that is presented to the user.
    :param default: The presumed answer if the user just hits <Enter>.
        It must be "yes" (the default), "no" or None (meaning
        an answer is required of the user).

    The "answer" return value is one of "yes" or "no".

    Adapted from
    http://stackoverflow.com/questions/3041986/python-command-line-yes-no-input<commit_msg>Use read_user_variable inside of prompt_for_config<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
cookiecutter.prompt
---------------------

Functions for prompting the user for project info.
"""

from __future__ import unicode_literals
import sys

import click

from .compat import iteritems, is_string
from jinja2.environment import Environment


def read_user_variable(var_name, default_value):
    """Prompt the user for the given variable and return the entered value
    or the given default.

    :param str var_name: Variable of the context to query the user
    :param default_value: Value that will be returned if no input happens
    """
    return click.prompt(var_name, default=default_value)


def read_response(prompt=''):
    """Prompt the user and return the entered value or an empty string.

    :param str prompt: Text to display to the user
    """
    # Please see http://click.pocoo.org/4/api/#click.prompt
    # default: use an empty string if no input happens
    # prompt_suffix: do not add a suffix to the prompt
    # show_default: the prompt is expected to mention the default itself
    return click.prompt(
        prompt,
        default='',
        prompt_suffix='',
        show_default=False,
    )


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 = {}
    env = Environment()

    for key, raw in iteritems(context['cookiecutter']):
        if key.startswith('_'):
            cookiecutter_dict[key] = raw
            continue

        raw = raw if is_string(raw) else str(raw)
        val = env.from_string(raw).render(cookiecutter=cookiecutter_dict)

        if not no_input:
            val = read_user_variable(key, val)

        cookiecutter_dict[key] = val
    return cookiecutter_dict


def query_yes_no(question, default='yes'):
    """
    Ask a yes/no question via `read_response()` and return their answer.

    :param question: A string that is presented to the user.
    :param default: The presumed answer if the user just hits <Enter>.
        It must be "yes" (the default), "no" or None (meaning
        an answer is required of the user).

    The "answer" return value is one of "yes" or "no".

    Adapted from
    http://stackoverflow.com/questions/3041986/python-command-line-yes-no-input
{'fraction_non_alphanumeric': 0.09446959259988191, 'fraction_numerical': 0.004329856327494588, 'mean_word_length': 2.8882938026013774, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3587639', 'n_tokens_mistral': 1486, 'n_tokens_neox': 1412, 'n_words': 537}