Skip to content

Commit

Permalink
loading standard templates from a file
Browse files Browse the repository at this point in the history
  • Loading branch information
tschm committed Jan 19, 2025
1 parent 2b55263 commit 5f1d44b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
24 changes: 15 additions & 9 deletions src/cvx/cradle/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@
from .utils.shell import run_shell_command


def load_templates(yaml_path: Path) -> dict[str, str]:
"""
Load templates from YAML file and return a dictionary mapping display names to URLs.
"""
with open(yaml_path) as f:
config = yaml.safe_load(f)

return {details["display_name"]: details["url"] for template_name, details in config["templates"].items()}


def append_to_yaml_file(new_data, file_path):
# Check if the file exists
if os.path.exists(file_path):
Expand Down Expand Up @@ -68,21 +78,17 @@ def cli(template: str = None, dst_path: str = None, vcs_ref: str | None = None,
logger.info("cradle will ask a group of questions to create a repository for you")

if template is None:
# which template you want to pick?
templates = {
"(Marimo) Experiments": "[email protected]:tschm/experiments.git",
"A package (complete with a release process)": "[email protected]:tschm/package.git",
"A paper": "[email protected]:tschm/paper.git",
}

# result is the value related to the key you pick
# Load templates from YAML file
yaml_path = Path(__file__).parent / "templates.yaml" # Adjust path as needed
templates = load_templates(yaml_path)

# Let user select from the display names
result = questionary.select(
"What kind of project do you want to create?",
choices=list(templates.keys()),
).ask()

template = templates[result]

remove_path = False
# Create a random path
if not dst_path:
Expand Down
10 changes: 10 additions & 0 deletions src/cvx/cradle/templates.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
templates:
marimo_experiments:
url: "[email protected]:tschm/experiments.git"
display_name: "(Marimo) Experiments"
package:
url: "[email protected]:tschm/package.git"
display_name: "A python package"
paper:
url: "[email protected]:tschm/paper.git"
display_name: "A LaTeX document"
4 changes: 2 additions & 2 deletions src/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ def ask(self):


def test_no_template(mock_context, mocker, tmp_path, mock_run_shell_command):
mocker.patch("cvx.cradle.cli.questionary.select", return_value=Answer("A paper"))
mocker.patch("cvx.cradle.cli.questionary.select", return_value=Answer("A LaTeX document"))
mocker.patch("cvx.cradle.cli.ask", return_value=mock_context)
mocker.patch("cvx.cradle.cli.copier.run_copy", return_value=None)
cli(dst_path=str(tmp_path))
assert mock_run_shell_command.call_count == 6


def test_runtime_error(mock_context, mocker, tmp_path):
mocker.patch("cvx.cradle.cli.questionary.select", return_value=Answer("A paper"))
mocker.patch("cvx.cradle.cli.questionary.select", return_value=Answer("A LaTeX document"))
mocker.patch("cvx.cradle.cli.ask", return_value=mock_context)
mocker.patch("cvx.cradle.cli.copier.run_copy", return_value=None)
mocker.patch("cvx.cradle.cli.run_shell_command", side_effect=RuntimeError("An error occurred"))
Expand Down

0 comments on commit 5f1d44b

Please sign in to comment.