Skip to content

user_data.py

Functions used to load user data.

AnswersMap

Object that gathers answers from different sources.

Attributes:

Name Type Description
local AnyByStrDict

Local overrides to other answers.

user AnyByStrDict

Answers provided by the user, interactively.

init AnyByStrDict

Answers provided on init.

This will hold those answers that come from --data in CLI mode.

See data.

metadata AnyByStrDict

Data used to be able to reproduce the template.

It comes from copier.template.Template.metadata.

last AnyByStrDict

Data from the answers file.

default AnyByStrDict

Default data from the template.

See copier.template.Template.default_answers.

user_defaults AnyByStrDict

Default data from the user e.g. previously completed and restored data.

See copier.main.Worker.

Source code in copier/user_data.py
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
@dataclass
class AnswersMap:
    """Object that gathers answers from different sources.

    Attributes:
        local:
            Local overrides to other answers.

        user:
            Answers provided by the user, interactively.

        init:
            Answers provided on init.

            This will hold those answers that come from `--data` in
            CLI mode.

            See [data][].

        metadata:
            Data used to be able to reproduce the template.

            It comes from [copier.template.Template.metadata][].

        last:
            Data from [the answers file][the-copier-answersyml-file].

        default:
            Default data from the template.

            See [copier.template.Template.default_answers][].

        user_defaults:
            Default data from the user e.g. previously completed and restored data.

            See [copier.main.Worker][].
    """

    # Private
    local: AnyByStrDict = field(default_factory=dict, init=False)

    # Public
    user: AnyByStrDict = field(default_factory=dict)
    init: AnyByStrDict = field(default_factory=dict)
    metadata: AnyByStrDict = field(default_factory=dict)
    last: AnyByStrDict = field(default_factory=dict)
    user_defaults: AnyByStrDict = field(default_factory=dict)
    default: AnyByStrDict = field(default_factory=dict)

    @cached_property
    def combined(self) -> t_ChainMap[str, Any]:
        """Answers combined from different sources, sorted by priority."""
        return ChainMap(
            self.local,
            self.user,
            self.init,
            self.metadata,
            self.last,
            self.user_defaults,
            self.default,
            DEFAULT_DATA,
        )

    def old_commit(self) -> OptStr:
        return self.last.get("_commit")

combined()

Answers combined from different sources, sorted by priority.

Source code in copier/user_data.py
120
121
122
123
124
125
126
127
128
129
130
131
132
@cached_property
def combined(self) -> t_ChainMap[str, Any]:
    """Answers combined from different sources, sorted by priority."""
    return ChainMap(
        self.local,
        self.user,
        self.init,
        self.metadata,
        self.last,
        self.user_defaults,
        self.default,
        DEFAULT_DATA,
    )

Question

One question asked to the user.

All attributes are init kwargs.

Attributes:

Name Type Description
choices Union[Dict[Any, Any], List[Any]]

Selections available for the user if the question requires them. Can be templated.

default Any

Default value presented to the user to make it easier to respond. Can be templated.

help str

Additional text printed to the user, explaining the purpose of this question. Can be templated.

multiline Union[str, bool]

Indicates if the question should allow multiline input. Defaults to True for JSON and YAML questions, and to False otherwise. Only meaningful for str-based questions. Can be templated.

placeholder str

Text that appears if there's nothing written in the input field, but disappears as soon as the user writes anything. Can be templated.

secret bool

Indicates if the question should be removed from the answers file. If the question type is str, it will hide user input on the screen by displaying asterisks: ****.

type_name bool

The type of question. Affects the rendering, validation and filtering. Can be templated.

var_name str

Question name in the answers dict.

when Union[str, bool]

Condition that, if False, skips the question. Can be templated. If it is a boolean, it is used directly. If it is a str, it is converted to boolean using a parser similar to YAML, but only for boolean values.

Source code in copier/user_data.py
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
@dataclass(config=AllowArbitraryTypes)
class Question:
    """One question asked to the user.

    All attributes are init kwargs.

    Attributes:
        choices:
            Selections available for the user if the question requires them.
            Can be templated.

        default:
            Default value presented to the user to make it easier to respond.
            Can be templated.

        help:
            Additional text printed to the user, explaining the purpose of
            this question. Can be templated.

        multiline:
            Indicates if the question should allow multiline input. Defaults
            to `True` for JSON and YAML questions, and to `False` otherwise.
            Only meaningful for str-based questions. Can be templated.

        placeholder:
            Text that appears if there's nothing written in the input field,
            but disappears as soon as the user writes anything. Can be templated.

        secret:
            Indicates if the question should be removed from the answers file.
            If the question type is str, it will hide user input on the screen
            by displaying asterisks: `****`.

        type_name:
            The type of question. Affects the rendering, validation and filtering.
            Can be templated.

        var_name:
            Question name in the answers dict.

        when:
            Condition that, if `False`, skips the question. Can be templated.
            If it is a boolean, it is used directly. If it is a str, it is
            converted to boolean using a parser similar to YAML, but only for
            boolean values.
    """

    var_name: str
    answers: AnswersMap
    jinja_env: SandboxedEnvironment
    choices: Union[Dict[Any, Any], List[Any]] = field(default_factory=list)
    default: Any = None
    help: str = ""
    ask_user: bool = False
    multiline: Union[str, bool] = False
    placeholder: str = ""
    secret: bool = False
    type: str = ""
    when: Union[str, bool] = True

    @validator("var_name")
    def _check_var_name(cls, v):
        if v in DEFAULT_DATA:
            raise ValueError("Invalid question name")
        return v

    @validator("type", always=True)
    def _check_type(cls, v, values):
        if v == "":
            default_type_name = type(values.get("default")).__name__
            v = default_type_name if default_type_name in CAST_STR_TO_NATIVE else "yaml"
        return v

    def get_default(self) -> Any:
        """Get the default value for this question, casted to its expected type."""
        cast_fn = self.get_cast_fn()
        try:
            result = self.answers.init[self.var_name]
        except KeyError:
            try:
                result = self.answers.last[self.var_name]
            except KeyError:
                try:
                    result = self.answers.user_defaults[self.var_name]
                except KeyError:
                    result = self.render_value(self.default)
        result = cast_answer_type(result, cast_fn)
        return result

    def get_default_rendered(self) -> Union[bool, str, Choice, None]:
        """Get default answer rendered for the questionary lib.

        The questionary lib expects some specific data types, and returns
        it when the user answers. Sometimes you need to compare the response
        to the rendered one, or viceversa.

        This helper allows such usages.
        """
        default = self.get_default()
        # If there are choices, return the one that matches the expressed default
        if self.choices:
            for choice in self._formatted_choices:
                if choice.value == default:
                    return choice
            return None
        # Yes/No questions expect and return bools
        if isinstance(default, bool) and self.get_type_name() == "bool":
            return default
        # Emptiness is expressed as an empty str
        if default is None:
            return ""
        # JSON and YAML dumped depending on mutliline setting
        if self.get_type_name() == "json":
            return json.dumps(default, indent=2 if self.get_multiline() else None)
        if self.get_type_name() == "yaml":
            return yaml.safe_dump(
                default, default_flow_style=not self.get_multiline(), width=2147483647
            ).strip()
        # All other data has to be str
        return str(default)

    @cached_property
    def _formatted_choices(self) -> List[Choice]:
        """Obtain choices rendered and properly formatted."""
        result = []
        choices = self.choices
        if isinstance(self.choices, dict):
            choices = list(self.choices.items())
        for choice in choices:
            # If a choice is a dict, it can be used raw
            if isinstance(choice, dict):
                name = choice["name"]
                value = choice["value"]
            # ... or a value pair
            elif isinstance(choice, (tuple, list)):
                name, value = choice
            # However, a choice can also be a single value...
            else:
                name = value = choice
            # The name must always be a str
            name = str(self.render_value(name))
            # The value can be templated
            value = self.render_value(value)
            result.append(Choice(name, value))
        return result

    def filter_answer(self, answer) -> Any:
        """Cast the answer to the desired type."""
        if answer == self.get_default_rendered():
            return self.get_default()
        return cast_answer_type(answer, self.get_cast_fn())

    def get_message(self) -> str:
        """Get the message that will be printed to the user."""
        if self.help:
            rendered_help = self.render_value(self.help)
            if rendered_help:
                return force_str_end(rendered_help) + "  "
        # Otherwise, there's no help message defined.
        message = self.var_name
        answer_type = self.get_type_name()
        if answer_type != "str":
            message += f" ({answer_type})"
        return message + "\n  "

    def get_placeholder(self) -> str:
        """Render and obtain the placeholder."""
        return self.render_value(self.placeholder)

    def get_questionary_structure(self) -> AnyByStrDict:
        """Get the question in a format that the questionary lib understands."""
        lexer = None
        result: AnyByStrDict = {
            "default": self.get_default_rendered(),
            "filter": self.filter_answer,
            "message": self.get_message(),
            "mouse_support": True,
            "name": self.var_name,
            "qmark": "🕵️" if self.secret else "🎤",
            "when": self.get_when,
        }
        questionary_type = "input"
        type_name = self.get_type_name()
        if type_name == "bool":
            questionary_type = "confirm"
        if self.choices:
            questionary_type = "select"
            result["choices"] = self._formatted_choices
        if questionary_type == "input":
            if self.secret:
                questionary_type = "password"
            elif type_name == "yaml":
                lexer = PygmentsLexer(YamlLexer)
            elif type_name == "json":
                lexer = PygmentsLexer(JsonLexer)
            if lexer:
                result["lexer"] = lexer
            result["multiline"] = self.get_multiline()
            placeholder = self.get_placeholder()
            if placeholder:
                result["placeholder"] = placeholder
            result["validate"] = self.validate_answer
        result.update({"type": questionary_type})
        return result

    def get_cast_fn(self) -> Callable:
        """Obtain function to cast user answer to desired type."""
        type_name = self.get_type_name()
        if type_name not in CAST_STR_TO_NATIVE:
            raise InvalidTypeError("Invalid question type")
        return CAST_STR_TO_NATIVE.get(type_name, parse_yaml_string)

    def get_type_name(self) -> str:
        """Render the type name and return it."""
        return self.render_value(self.type)

    def get_multiline(self) -> bool:
        """Get the value for multiline."""
        multiline = self.render_value(self.multiline)
        multiline = cast_answer_type(multiline, cast_str_to_bool)
        return bool(multiline)

    def validate_answer(self, answer) -> bool:
        """Validate user answer."""
        cast_fn = self.get_cast_fn()
        try:
            cast_fn(answer)
            return True
        except Exception:
            return False

    def get_when(self, answers) -> bool:
        """Get skip condition for question."""
        if (
            # Skip on --defaults
            not self.ask_user
            # Skip on --data=this_question=some_answer
            or self.var_name in self.answers.init
        ):
            return False
        when = self.when
        when = self.render_value(when)
        when = cast_answer_type(when, cast_str_to_bool)
        return bool(when)

    def render_value(self, value: Any) -> str:
        """Render a single templated value using Jinja.

        If the value cannot be used as a template, it will be returned as is.
        """
        try:
            template = self.jinja_env.from_string(value)
        except TypeError:
            # value was not a string
            return value
        try:
            return template.render(**self.answers.combined)
        except UndefinedError as error:
            raise UserMessageError(str(error)) from error

filter_answer(answer)

Cast the answer to the desired type.

Source code in copier/user_data.py
284
285
286
287
288
def filter_answer(self, answer) -> Any:
    """Cast the answer to the desired type."""
    if answer == self.get_default_rendered():
        return self.get_default()
    return cast_answer_type(answer, self.get_cast_fn())

get_cast_fn()

Obtain function to cast user answer to desired type.

Source code in copier/user_data.py
343
344
345
346
347
348
def get_cast_fn(self) -> Callable:
    """Obtain function to cast user answer to desired type."""
    type_name = self.get_type_name()
    if type_name not in CAST_STR_TO_NATIVE:
        raise InvalidTypeError("Invalid question type")
    return CAST_STR_TO_NATIVE.get(type_name, parse_yaml_string)

get_default()

Get the default value for this question, casted to its expected type.

Source code in copier/user_data.py
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
def get_default(self) -> Any:
    """Get the default value for this question, casted to its expected type."""
    cast_fn = self.get_cast_fn()
    try:
        result = self.answers.init[self.var_name]
    except KeyError:
        try:
            result = self.answers.last[self.var_name]
        except KeyError:
            try:
                result = self.answers.user_defaults[self.var_name]
            except KeyError:
                result = self.render_value(self.default)
    result = cast_answer_type(result, cast_fn)
    return result

get_default_rendered()

Get default answer rendered for the questionary lib.

The questionary lib expects some specific data types, and returns it when the user answers. Sometimes you need to compare the response to the rendered one, or viceversa.

This helper allows such usages.

Source code in copier/user_data.py
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
def get_default_rendered(self) -> Union[bool, str, Choice, None]:
    """Get default answer rendered for the questionary lib.

    The questionary lib expects some specific data types, and returns
    it when the user answers. Sometimes you need to compare the response
    to the rendered one, or viceversa.

    This helper allows such usages.
    """
    default = self.get_default()
    # If there are choices, return the one that matches the expressed default
    if self.choices:
        for choice in self._formatted_choices:
            if choice.value == default:
                return choice
        return None
    # Yes/No questions expect and return bools
    if isinstance(default, bool) and self.get_type_name() == "bool":
        return default
    # Emptiness is expressed as an empty str
    if default is None:
        return ""
    # JSON and YAML dumped depending on mutliline setting
    if self.get_type_name() == "json":
        return json.dumps(default, indent=2 if self.get_multiline() else None)
    if self.get_type_name() == "yaml":
        return yaml.safe_dump(
            default, default_flow_style=not self.get_multiline(), width=2147483647
        ).strip()
    # All other data has to be str
    return str(default)

get_message()

Get the message that will be printed to the user.

Source code in copier/user_data.py
290
291
292
293
294
295
296
297
298
299
300
301
def get_message(self) -> str:
    """Get the message that will be printed to the user."""
    if self.help:
        rendered_help = self.render_value(self.help)
        if rendered_help:
            return force_str_end(rendered_help) + "  "
    # Otherwise, there's no help message defined.
    message = self.var_name
    answer_type = self.get_type_name()
    if answer_type != "str":
        message += f" ({answer_type})"
    return message + "\n  "

get_multiline()

Get the value for multiline.

Source code in copier/user_data.py
354
355
356
357
358
def get_multiline(self) -> bool:
    """Get the value for multiline."""
    multiline = self.render_value(self.multiline)
    multiline = cast_answer_type(multiline, cast_str_to_bool)
    return bool(multiline)

get_placeholder()

Render and obtain the placeholder.

Source code in copier/user_data.py
303
304
305
def get_placeholder(self) -> str:
    """Render and obtain the placeholder."""
    return self.render_value(self.placeholder)

get_questionary_structure()

Get the question in a format that the questionary lib understands.

Source code in copier/user_data.py
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
def get_questionary_structure(self) -> AnyByStrDict:
    """Get the question in a format that the questionary lib understands."""
    lexer = None
    result: AnyByStrDict = {
        "default": self.get_default_rendered(),
        "filter": self.filter_answer,
        "message": self.get_message(),
        "mouse_support": True,
        "name": self.var_name,
        "qmark": "🕵️" if self.secret else "🎤",
        "when": self.get_when,
    }
    questionary_type = "input"
    type_name = self.get_type_name()
    if type_name == "bool":
        questionary_type = "confirm"
    if self.choices:
        questionary_type = "select"
        result["choices"] = self._formatted_choices
    if questionary_type == "input":
        if self.secret:
            questionary_type = "password"
        elif type_name == "yaml":
            lexer = PygmentsLexer(YamlLexer)
        elif type_name == "json":
            lexer = PygmentsLexer(JsonLexer)
        if lexer:
            result["lexer"] = lexer
        result["multiline"] = self.get_multiline()
        placeholder = self.get_placeholder()
        if placeholder:
            result["placeholder"] = placeholder
        result["validate"] = self.validate_answer
    result.update({"type": questionary_type})
    return result

get_type_name()

Render the type name and return it.

Source code in copier/user_data.py
350
351
352
def get_type_name(self) -> str:
    """Render the type name and return it."""
    return self.render_value(self.type)

get_when(answers)

Get skip condition for question.

Source code in copier/user_data.py
369
370
371
372
373
374
375
376
377
378
379
380
381
def get_when(self, answers) -> bool:
    """Get skip condition for question."""
    if (
        # Skip on --defaults
        not self.ask_user
        # Skip on --data=this_question=some_answer
        or self.var_name in self.answers.init
    ):
        return False
    when = self.when
    when = self.render_value(when)
    when = cast_answer_type(when, cast_str_to_bool)
    return bool(when)

render_value(value)

Render a single templated value using Jinja.

If the value cannot be used as a template, it will be returned as is.

Source code in copier/user_data.py
383
384
385
386
387
388
389
390
391
392
393
394
395
396
def render_value(self, value: Any) -> str:
    """Render a single templated value using Jinja.

    If the value cannot be used as a template, it will be returned as is.
    """
    try:
        template = self.jinja_env.from_string(value)
    except TypeError:
        # value was not a string
        return value
    try:
        return template.render(**self.answers.combined)
    except UndefinedError as error:
        raise UserMessageError(str(error)) from error

validate_answer(answer)

Validate user answer.

Source code in copier/user_data.py
360
361
362
363
364
365
366
367
def validate_answer(self, answer) -> bool:
    """Validate user answer."""
    cast_fn = self.get_cast_fn()
    try:
        cast_fn(answer)
        return True
    except Exception:
        return False

cast_answer_type(answer, type_fn)

Cast answer to expected type.

Source code in copier/user_data.py
423
424
425
426
427
428
429
430
431
432
def cast_answer_type(answer: Any, type_fn: Callable) -> Any:
    """Cast answer to expected type."""
    # Skip casting None into "None"
    if type_fn is str and answer is None:
        return answer
    try:
        return type_fn(answer)
    except (TypeError, AttributeError):
        # JSON or YAML failed because it wasn't a string; no need to convert
        return answer

load_answersfile_data(dst_path, answers_file=None)

Load answers data from a $dst_path/$answers_file file if it exists.

Source code in copier/user_data.py
411
412
413
414
415
416
417
418
419
420
def load_answersfile_data(
    dst_path: StrOrPath,
    answers_file: OptStrOrPath = None,
) -> AnyByStrDict:
    """Load answers data from a `$dst_path/$answers_file` file if it exists."""
    try:
        with open(Path(dst_path) / (answers_file or ".copier-answers.yml")) as fd:
            return yaml.safe_load(fd)
    except FileNotFoundError:
        return {}

parse_yaml_string(string)

Parse a YAML string and raise a ValueError if parsing failed.

This method is needed because :meth:prompt requires a ValueError to repeat failed questions.

Source code in copier/user_data.py
399
400
401
402
403
404
405
406
407
408
def parse_yaml_string(string: str) -> Any:
    """Parse a YAML string and raise a ValueError if parsing failed.

    This method is needed because :meth:`prompt` requires a ``ValueError``
    to repeat failed questions.
    """
    try:
        return yaml.safe_load(string)
    except yaml.error.YAMLError as error:
        raise ValueError(str(error))