Skip to content

Commit

Permalink
Load stories from text files (#14)
Browse files Browse the repository at this point in the history
* Fix typo

Signed-off-by: GitHub <[email protected]>

* Install as editable by default

Signed-off-by: GitHub <[email protected]>

* Move stories to text files

Signed-off-by: GitHub <[email protected]>

* Add entrypoint, pin dependencies

Signed-off-by: GitHub <[email protected]>

* Update name

Signed-off-by: GitHub <[email protected]>

* Bump version

Signed-off-by: GitHub <[email protected]>

---------

Signed-off-by: GitHub <[email protected]>
  • Loading branch information
shenanigansd authored Oct 25, 2024
1 parent 38198ed commit 8b4b0a8
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 83 deletions.
2 changes: 1 addition & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
@nox.session
def install(session: nox.Session) -> None:
"""Install the project."""
session.run("python", "-m", "pip", "install", ".[dev,test]")
session.run("python", "-m", "pip", "install", "--editable", ".[dev,tests]")


@nox.session
Expand Down
42 changes: 32 additions & 10 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,24 +1,42 @@
[project]
name = "crazylibs"
version = "0.1.2"
version = "0.2.0"
description = "crazylibs"
readme = "README.md"
authors = [{ name = "Bradley Reynolds", email = "[email protected]" }]
license = { text = "MIT" }
requires-python = ">=3.10"
dependencies = ["typer"]

[project.optional-dependencies]
dev = ["nox", "pre-commit", "ruff", "mypy"]
docs = ["sphinx", "furo", "sphinx-autoapi", "releases"]
tests = ["pytest", "pytest-randomly"]
dependencies = [
"typer==0.12.5"
]

[project.urls]
repository = "https://github.com/letsbuilda/crazylibs"
documentation = "https://docs.letsbuilda.dev/crazylibs/"

[project.scripts]
crazylibs = "crazylibs.cli:app"

[project.optional-dependencies]
dev = [
"nox==2024.10.9",
"pre-commit==4.0.1",
"ruff==0.7.1",
"mypy==1.13.0",
]
tests = [
"pytest==8.3.3",
"pytest-randomly==3.15.0",
]
docs = [
"sphinx==8.1.3",
"furo==2024.8.6",
"sphinx-autoapi==3.3.2",
"releases==2.1.1",
]

[build-system]
requires = ["setuptools", "wheel"]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"

[tool.setuptools.package-data]
Expand All @@ -29,7 +47,9 @@ target-version = "py310"
line-length = 120

[tool.ruff.lint]
select = ["ALL"]
select = [
"ALL",
]
ignore = [
"CPY001", # (Missing copyright notice at top of file) - No license
]
Expand All @@ -47,4 +67,6 @@ ignore = [
convention = "numpy"

[tool.coverage.run]
source = ["crazylibs"]
source = [
"crazylibs",
]
55 changes: 29 additions & 26 deletions src/crazylibs/cli.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
"""CLI app definition."""

from random import choice
import random
from pathlib import Path
from typing import Annotated

import typer

app = typer.Typer()


@app.command()
def run_story() -> None:
"""Fun!""" # noqa: D400
from crazylibs.stories import stories

story = choice(stories) # noqa: S311
from .stories import run_story

typer.echo(f'Welcome to "{story.title}"')
typer.echo("")
typer.echo("Please fill in the following words:")

context = {}
for index, question in story.questions.items():
context[index] = typer.prompt(question)

text = story.template
for index, item in context.items():
text = text.replace(f"({index})", item)
app = typer.Typer()

typer.echo("")
typer.echo("Wow, what a good selection of words!")
typer.echo("Here is your story:")
typer.echo("")

typer.echo(text)
@app.command(help="A variation of the 'Mad Libs' games.")
def run_story_command(
story_file_name: Annotated[
str | None,
typer.Argument(
help="The name of the story to run, without the .txt extension",
),
] = None,
) -> None:
"""Run a story."""
stories_folder = Path("stories")
if not stories_folder.exists():
typer.echo("No stories folder found. Exiting.")
raise typer.Exit(code=1)

if story_file_name:
story_path = stories_folder.joinpath(f"{story_file_name}.txt")
if not story_path.exists():
typer.echo(f"Story file {story_file_name} not found. Exiting.")
raise typer.Exit(code=1)
else:
story_path = random.choice(list(stories_folder.glob("*.txt"))) # noqa: S311

run_story(story_path)
38 changes: 38 additions & 0 deletions src/crazylibs/stories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""Story functions."""

from pathlib import Path

import typer


def run_story(story_file: Path) -> None:
"""Run a story."""
story_text = Path(story_file).read_text()
raw_title, questions_block, template = story_text.split("---")

title = raw_title.strip()

question_rows = questions_block.strip().split("\n")
questions = {}
for row in question_rows:
index, question = row.split(":")
questions[index] = question.strip()

typer.echo(f'Welcome to "{title}"')
typer.echo("")
typer.echo("Please fill in the following words:")

context = {}
for index, question in questions.items():
context[index] = typer.prompt(question)

text = template
for index, item in context.items():
text = text.replace(f"({index})", item)

typer.echo("")
typer.echo("Wow, what a good selection of words!")
typer.echo("Here is your story:")
typer.echo("")

typer.echo(text)
5 changes: 0 additions & 5 deletions src/crazylibs/stories/__init__.py

This file was deleted.

10 changes: 0 additions & 10 deletions src/crazylibs/stories/_story.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
"""A Day at the Zoo."""

from ._story import Story

questions = {
1: "adjective",
2: "animal",
3: "verb ending in -ing",
4: "place",
5: "adjective",
6: "animal",
7: "verb ending in -ing",
8: "adjective",
9: "food",
10: "adjective",
11: "animal",
12: "adjective",
13: "animal",
14: "verb ending in -ing",
15: "verb ending in -ing",
16: "adjective",
17: "adjective",
18: "noun",
19: "adjective",
}

template = """
A Day at the Zoo
---
1: adjective
2: animal
3: verb ending in -ing
4: place
5: adjective
6: animal
7: verb ending in -ing
8: adjective
9: food
10: adjective
11: animal
12: adjective
13: animal
14: verb ending in -ing
15: verb ending in -ing
16: adjective
17: adjective
18: noun
19: adjective
---
One day, my friend and I decided to visit the zoo. We were very (1) to see all the animals.
First, we saw the (2), which was (3) in its (4).
Next, we went to the (5) exhibit where we saw a (6) that was (7). It was so (8)!
Expand All @@ -36,6 +31,3 @@

Finally, we visited the gift shop and bought a (17) (18) as a souvenir.
It was a (19) day at the zoo, and we couldn't wait to come back again!
"""

a_day_at_the_zoo = Story(title="A Day at the Zoo", template=template, questions=questions)
3 changes: 2 additions & 1 deletion tests/test_app.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Testing the app."""

from crazylibs.cli import app
from typer.testing import CliRunner

from crazylibs.cli import app

runner = CliRunner()


Expand Down

0 comments on commit 8b4b0a8

Please sign in to comment.