Source code for cli_interview_kit.interaction

"""How a run may interact with its user: answers and consents.

Each run resolves one ``Interaction`` from ``--yes`` and whether stdin is a
terminal, and every question and consent follows it:

- **Answers** (what to create, which environment): asked in a terminal.
  When nobody can be asked, each takes its default, and one with no safe
  default fails the run, naming its flag.
- **Consents** (delete something, deploy to production, overwrite files):
  ``--yes`` gives them. Without it they are asked in a terminal; when nobody
  can be asked, a consent with a safe default takes it and any other fails
  the run, asking for ``--yes``. A piped stdin never consents by itself.
"""

from __future__ import annotations

import sys

from pydantic import Field

from cli_interview_kit._model import FrozenModel
from cli_interview_kit.console import Console
from cli_interview_kit.errors import CliError, DeclarationError


[docs] class Interaction(FrozenModel): """Whether a run may prompt, and whether it has blanket consent. Attributes ---------- can_prompt Stdin is a terminal and ``--yes`` was not given. consent ``--yes`` was given: every declared consent is granted. declared The keys of the consents the running command declares; only these can be asked for. Methods ------- resolve Return the interaction for a run, from ``--yes`` and the terminal. """ can_prompt: bool consent: bool declared: frozenset[str] = Field(default_factory=frozenset)
[docs] @classmethod def resolve(cls, yes: bool, declared: frozenset[str] = frozenset()) -> Interaction: """Return the interaction for a run, from ``--yes`` and the terminal. Parameters ---------- yes Whether ``--yes`` was given. declared The keys of the consents the running command declares. Returns ------- The interaction every question and consent of the run follows. """ return cls(can_prompt=sys.stdin.isatty() and not yes, consent=yes, declared=declared)