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

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

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

from __future__ import unicode_literals
from __future__ import print_function

import sys
import re

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


def prompt_field(field, help_txt, value=None, regex=None,
                 default=None, required=False):
    """Prompt for value of one field

    """

    if default is not None:
        prompt = '{0} (default is "{1}") ? '.format(help_txt, default)
    elif not required:
        prompt = '{0} (optional) ? '.format(help_txt)
    else:
        prompt = '{0} ? '.format(help_txt)

    try:
        reg = re.compile('^%s$' % regex) if regex else None
    except re.error as e:
        print("Invalid regex for field %s: %s\n%s"
              % (field, regex, e.message))
        exit(1)

    while True:
        if value:
            ans = value
        else:
            ans = read_response(prompt).strip()
        if ans == '':
            if not required:
                return None
            if default is not None:
                ans = default
        if not reg:
            return ans
        if reg.search(ans):
            return ans

        print("Bad value for template field '%s'. Please try to match %r..."
              % (field, regex))
        value = None


def prompt_for_config(context, no_input=False, only_missing=None, values=None,
                      with_optional=True):
    """
    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?
    """
    if "cookiecutter" in context:
        context = context["cookiecutter"]

    if values is None:
        values = {}

    cookiecutter_dict = {}
    env = Environment()

    def render(s):
        return env.from_string(s).render(
            cookiecutter=cookiecutter_dict,
            cc=cookiecutter_dict)

    meta = context.get('__meta__', {})
    if only_missing is None:
        only_missing = True if meta else False

    keys = meta.keys() if meta else context.keys()
    for key in keys:
        dsc = meta.get(key, {})
        raw = values.get(key, None) or context.get(key, None)
        if key.startswith('_'):
            cookiecutter_dict[key] = raw
            continue
        if raw is not None:
            raw = raw if is_string(raw) else str(raw)
        else:
            raw = dsc.get("default", None)
        default = render(raw) if raw is not None else None

        optional = dsc and "default" in dsc and dsc["default"] is None

        if no_input or \<commit_msg>fix: repair template interpolation in default values<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

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

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

from __future__ import unicode_literals
from __future__ import print_function

import sys
import re

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


def prompt_field(field, help_txt, value=None, regex=None,
                 default=None, required=False):
    """Prompt for value of one field

    """

    if default is not None:
        prompt = '{0} (default is "{1}") ? '.format(help_txt, default)
    elif not required:
        prompt = '{0} (optional) ? '.format(help_txt)
    else:
        prompt = '{0} ? '.format(help_txt)

    try:
        reg = re.compile('^%s$' % regex) if regex else None
    except re.error as e:
        print("Invalid regex for field %s: %s\n%s"
              % (field, regex, e.message))
        exit(1)

    while True:
        if value:
            ans = value
        else:
            ans = read_response(prompt).strip()
        if ans == '':
            if not required:
                return None
            if default is not None:
                ans = default
        if not reg:
            return ans
        if reg.search(ans):
            return ans

        print("Bad value for template field '%s'. Please try to match %r..."
              % (field, regex))
        value = None


def prompt_for_config(context, no_input=False, only_missing=None, values=None,
                      with_optional=True):
    """
    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?
    """
    if "cookiecutter" in context:
        context = context["cookiecutter"]

    if values is None:
        values = {}

    cookiecutter_dict = {}
    env = Environment()

    def render(s):
        return env.from_string(s).render(**cookiecutter_dict)

    meta = context.get('__meta__', {})
    if only_missing is None:
        only_missing = True if meta else False

    keys = meta.keys() if meta else context.keys()
    for key in keys:
        dsc = meta.get(key, {})
        raw = values.get(key, None) or context.get(key, None)
        if key.startswith('_'):
            cookiecutter_dict[key] = raw
            continue
        if raw is not None:
            raw = raw if is_string(raw) else str(raw)
        else:
            raw = dsc.get("default", None)
        default = render(raw) if raw is not None else None

        optional = dsc and "default" in dsc and dsc["default"] is None

        if no_input or \
{'fraction_non_alphanumeric': 0.10790190735694823, 'fraction_numerical': 0.0025431425976385103, 'mean_word_length': 2.02029621503017, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '475785', 'n_tokens_mistral': 1596, 'n_tokens_neox': 1550, 'n_words': 538}