add init command (#35)

pull/36/head v0.2.0
Josh Karpel 3 years ago committed by GitHub
parent 8d17f11ccb
commit 4c243d13fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -458,7 +458,7 @@ def _(example, triggers):
@deck.slide(title="Live Coding with the REPL")
def notebooks():
def repl():
markup = dedent(
f"""\
## Live Coding: REPL
@ -512,7 +512,7 @@ def notebooks():
@deck.slide(title="Options")
def notebooks():
def options_():
markup = dedent(
f"""\
## Options

@ -93,6 +93,81 @@ def _present(path: Path, mode: Mode, slide: int, watch: bool, poll: bool) -> Non
state.console.print(Control.move_to(0, 0))
@app.command()
def init(
path: Path = Argument(
...,
writable=True,
resolve_path=True,
help="The path to create a new deck script at.",
)
) -> None:
"""
Create a new deck script at the given path from a basic template.
This is a good starting point if you already know what you want to do.
If you're not so sure, consider taking a look at the demo deck to see what's possible:
$ spiel demo --help
"""
console = Console()
if path.exists():
console.print(
Text(f"Error: {path} already exists, refusing to overwrite.", style=Style(color="red"))
)
raise Exit(code=1)
name = path.stem.replace("_", " ").title()
try:
path.parent.mkdir(parents=True, exist_ok=True)
except Exception as e:
console.print(
Text(
f"Error: was not able to ensure that the parent directory {path.parent} exists due to: {e}.",
style=Style(color="red"),
)
)
raise Exit(code=1)
try:
path.write_text(
dedent(
f"""\
from textwrap import dedent
from spiel import Deck, Options
deck = Deck(name="{name}")
options = Options()
@deck.slide(title="Title")
def title():
markup = dedent(
\"""\\
# {name}
This is your title slide!
\"""
)
return Markdown(markup, justify="center")
"""
)
)
except Exception as e:
console.print(
Text(
f"Error: was not able to write template to {path} due to: {e}",
style=Style(color="red"),
)
)
raise Exit(code=1)
console.print(Text(f"Wrote deck template to {path}", style=Style(color="green")))
@app.command()
def version(
plain: bool = Option(
@ -153,6 +228,10 @@ def copy(
) -> None:
"""
Copy the demo deck source code and assets to a new directory.
If you're looking for a more stripped-down starting point, try the init command:
$ spiel init --help
"""
console = Console()

@ -4,6 +4,7 @@ from textwrap import dedent
import pytest
from rich.console import Console
from typer.testing import CliRunner
from spiel import Deck, Options
from spiel.constants import DECK
@ -11,6 +12,11 @@ from spiel.slide import Slide
from spiel.state import State
@pytest.fixture
def runner() -> CliRunner:
return CliRunner()
@pytest.fixture
def three_slide_deck() -> Deck:
deck = Deck(name="three-slides")

@ -12,11 +12,6 @@ from spiel.main import DEMO_SOURCE, app
from spiel.modes import Mode
@pytest.fixture
def runner() -> CliRunner:
return CliRunner()
def test_help(runner: CliRunner) -> None:
result = runner.invoke(app, ["--help"])

@ -0,0 +1,36 @@
from pathlib import Path
import pytest
from typer.testing import CliRunner
from spiel import Options
from spiel.load import load_deck_and_options
from spiel.main import app
def test_init_cli_command_fails_if_file_exists(runner: CliRunner, tmp_path: Path) -> None:
target = tmp_path / "foo_bar.py"
target.touch()
result = runner.invoke(app, ["init", str(target)])
assert result.exit_code == 1
@pytest.fixture
def init_file(runner: CliRunner, tmp_path: Path) -> Path:
target = tmp_path / "foo_bar.py"
runner.invoke(app, ["init", str(target)])
return target
def test_title_slide_header_injection(init_file: Path) -> None:
assert "# Foo Bar" in init_file.read_text()
def test_can_load_init_file(init_file: Path) -> None:
deck, options = load_deck_and_options(init_file)
assert deck.name == "Foo Bar"
assert options == Options()
Loading…
Cancel
Save