User settings model.
| 76 | |
| 77 | |
| 78 | class SettingsModel(BaseModel): |
| 79 | """User settings model.""" |
| 80 | |
| 81 | defaults: dict[str, Any] = Field( |
| 82 | default_factory=dict, description="Default values for questions" |
| 83 | ) |
| 84 | trust: set[str] = Field( |
| 85 | default_factory=set, description="List of trusted repositories or prefixes" |
| 86 | ) |
| 87 | |
| 88 | @staticmethod |
| 89 | def _default_settings_path() -> Path: |
| 90 | return _default_settings_path() |
| 91 | |
| 92 | @classmethod |
| 93 | def from_file(cls, settings_path: Path | None = None) -> SettingsModel: |
| 94 | """Load settings from a file.""" |
| 95 | env_path = os.getenv(_ENV_VAR) |
| 96 | if settings_path is None: |
| 97 | if env_path: |
| 98 | settings_path = Path(env_path) |
| 99 | else: |
| 100 | settings_path = cls._default_settings_path() |
| 101 | |
| 102 | # NOTE: Remove after a sufficiently long deprecation period. |
| 103 | if OS == "windows": |
| 104 | old_settings_path = user_config_path("copier") / "settings.yml" |
| 105 | if old_settings_path.is_file(): |
| 106 | warnings.warn( |
| 107 | f"Settings path {old_settings_path} is deprecated. " |
| 108 | f"Please migrate to {settings_path}.", |
| 109 | DeprecationWarning, |
| 110 | stacklevel=2, |
| 111 | ) |
| 112 | settings_path = old_settings_path |
| 113 | if settings_path.is_file(): |
| 114 | data = yaml.safe_load(settings_path.read_bytes()) |
| 115 | return cls.model_validate(data) |
| 116 | elif env_path: |
| 117 | warnings.warn( |
| 118 | f"Settings file not found at {env_path}", MissingSettingsWarning |
| 119 | ) |
| 120 | return cls() |
| 121 | |
| 122 | def is_trusted(self, repository: str) -> bool: |
| 123 | """Check if a repository is trusted.""" |
| 124 | return _is_trusted(self, repository) |
| 125 | |
| 126 | def normalize(self, url: str) -> str: |
| 127 | """Normalize an URL using user settings.""" |
| 128 | return _normalize(url) |
| 129 | |
| 130 | |
| 131 | def _default_settings_path() -> Path: |
no outgoing calls