Skip to content

Commit 08db834

Browse files
committed
refactor: use common answers file loader
1 parent e444514 commit 08db834

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

copier/main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,9 @@ def _external_data(self) -> _LazyDict:
293293
return _LazyDict(
294294
**{
295295
name: lambda path=path: load_answersfile_data(
296-
self.dst_path, self._render_string(path)
296+
self.dst_path,
297+
self._render_string(path),
298+
warn_on_missing=True,
297299
)
298300
for name, path in self.template.external_data.items()
299301
}

copier/subproject.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
from pathlib import Path
1111
from typing import Callable
1212

13-
import yaml
1413
from plumbum.machines import local
1514
from pydantic.dataclasses import dataclass
1615

1716
from .template import Template
1817
from .types import AbsolutePath, AnyByStrDict, VCSTypes
18+
from .user_data import load_answersfile_data
1919
from .vcs import get_git, is_in_git_repo
2020

2121

@@ -55,9 +55,7 @@ def _cleanup(self) -> None:
5555
def _raw_answers(self) -> AnyByStrDict:
5656
"""Get last answers, loaded raw as yaml."""
5757
try:
58-
return yaml.safe_load(
59-
(self.local_abspath / self.answers_relpath).read_text("utf-8")
60-
)
58+
return load_answersfile_data(self.local_abspath, self.answers_relpath)
6159
except OSError:
6260
return {}
6361

copier/user_data.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,15 +504,19 @@ def parse_yaml_string(string: str) -> Any:
504504
def load_answersfile_data(
505505
dst_path: StrOrPath,
506506
answers_file: StrOrPath = ".copier-answers.yml",
507+
*,
508+
warn_on_missing: bool = False,
507509
) -> AnyByStrDict:
508510
"""Load answers data from a `$dst_path/$answers_file` file if it exists."""
509511
try:
510512
with Path(dst_path, answers_file).open(encoding="utf-8") as fd:
511513
return yaml.safe_load(fd)
512514
except (FileNotFoundError, IsADirectoryError):
513-
warnings.warn(
514-
f"File not found; returning empty dict: {answers_file}", MissingFileWarning
515-
)
515+
if warn_on_missing:
516+
warnings.warn(
517+
f"File not found; returning empty dict: {answers_file}",
518+
MissingFileWarning,
519+
)
516520
return {}
517521

518522

0 commit comments

Comments
 (0)