Skip to content

Commit

Permalink
add init command (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshKarpel authored May 15, 2021
1 parent 8d17f11 commit 4c243d1
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 7 deletions.
4 changes: 2 additions & 2 deletions spiel/demo/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -512,7 +512,7 @@ def notebooks():


@deck.slide(title="Options")
def notebooks():
def options_():
markup = dedent(
f"""\
## Options
Expand Down
79 changes: 79 additions & 0 deletions spiel/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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()

Expand Down
6 changes: 6 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@

import pytest
from rich.console import Console
from typer.testing import CliRunner

from spiel import Deck, Options
from spiel.constants import DECK
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")
Expand Down
5 changes: 0 additions & 5 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@
from spiel.modes import Mode


@pytest.fixture
def runner() -> CliRunner:
return CliRunner()


def test_help(runner: CliRunner) -> None:
result = runner.invoke(app, ["--help"])

Expand Down
36 changes: 36 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 4c243d1

Please sign in to comment.