dnd/deadsands/site_tools/cli.py

136 lines
3.2 KiB
Python
Raw Normal View History

2022-08-02 18:20:45 -07:00
import sys
from enum import Enum
2023-07-04 10:59:51 -07:00
from pathlib import Path
import click
import typer
2024-01-06 12:23:05 -08:00
from rolltable.types import RollTable
2023-07-03 14:14:03 -07:00
from typing_extensions import Annotated
2022-09-17 16:48:16 -07:00
2022-08-02 22:40:49 -07:00
from site_tools.content_manager import create
2023-07-04 12:35:04 -07:00
from site_tools import build_system
2022-08-02 22:49:01 -07:00
2022-08-02 18:20:45 -07:00
class ContentType(str, Enum):
2023-07-04 10:59:51 -07:00
post = "post"
lore = "lore"
monster = "monster"
region = "region"
location = "location"
page = "page"
2022-08-02 18:20:45 -07:00
2023-07-04 12:35:04 -07:00
2023-07-03 14:14:03 -07:00
class Die(str, Enum):
2023-07-04 10:59:51 -07:00
d100 = "100"
d20 = "20"
d12 = "12"
d10 = "10"
d6 = "6"
d4 = "4"
2023-07-03 14:14:03 -07:00
2023-07-04 12:35:04 -07:00
# 'site' ENTRY POINT
site_app = typer.Typer()
# BUILD SYSTEM SUBCOMMANDS
@site_app.command()
def clean() -> None:
2023-07-04 12:35:04 -07:00
build_system.clean()
2023-07-04 12:35:04 -07:00
@site_app.command()
def build() -> None:
2023-07-04 12:35:04 -07:00
build_system.build()
2023-07-04 12:35:04 -07:00
@site_app.command()
2022-09-17 16:48:16 -07:00
def watch() -> None:
2023-07-04 12:35:04 -07:00
build_system.watch()
@site_app.command()
def serve() -> None:
2023-07-04 12:35:04 -07:00
build_system.serve()
@site_app.command()
2022-08-02 22:40:49 -07:00
def publish() -> None:
2023-07-04 12:35:04 -07:00
build_system.publish()
# CONTENT MANAGEMENT COMMANDS
2023-07-04 12:35:04 -07:00
@site_app.command()
2023-07-04 10:59:51 -07:00
def restock(
source: str = typer.Argument(..., help="The source file for the store."),
frequency: str = Annotated[str, typer.Option("default", help="use the specified frequency from the source file")],
die: Die = typer.Option(20, help="The size of the die for which to create a table"),
2023-07-03 14:14:03 -07:00
template_dir: str = Annotated[
str,
typer.Argument(
2023-07-04 12:35:04 -07:00
build_system.CONFIG["templates_path"],
2023-07-03 14:14:03 -07:00
help="Override the default location for markdown templates.",
2023-07-04 10:59:51 -07:00
),
2023-07-03 14:14:03 -07:00
],
2023-05-13 13:24:43 -07:00
) -> None:
2023-07-04 10:59:51 -07:00
rt = RollTable([Path(source).read_text()], frequency=frequency, die=die, hide_rolls=True)
store = rt.datasources[0].metadata["store"]
click.edit(
filename=create(
content_type="post",
title=store["title"],
template_dir=template_dir,
category="stores",
template="store",
extra_context=dict(inventory=rt.as_markdown, **store),
2023-05-13 13:24:43 -07:00
)
2023-07-04 10:59:51 -07:00
)
2023-05-13 13:24:43 -07:00
2023-07-04 12:35:04 -07:00
@site_app.command()
2022-08-02 18:20:45 -07:00
def new(
content_type: ContentType = typer.Argument(
...,
help="The type of content to create.",
),
title: str = typer.Argument(
...,
help="The title of the content.",
),
category: str = typer.Argument(
None,
help='Override the default category; required for "post" content.',
),
template: str = typer.Argument(
None,
help="Override the default template for the content_type.",
),
template_dir: str = typer.Argument(
2023-07-04 12:35:04 -07:00
build_system.CONFIG["templates_path"],
2022-08-02 18:20:45 -07:00
help="Override the default location for markdown templates.",
2023-07-04 10:59:51 -07:00
),
2022-08-02 18:20:45 -07:00
) -> None:
if not category:
match content_type:
2023-07-04 10:59:51 -07:00
case "post":
2022-08-02 18:20:45 -07:00
print("You must specify a category for 'post' content.")
sys.exit()
2023-07-04 10:59:51 -07:00
case "monster":
category = "bestiary"
case "region":
category = "locations"
case "location":
category = "locations"
case "page":
category = "pages"
2022-08-02 18:20:45 -07:00
case _:
2022-08-04 21:34:47 -07:00
category = content_type.value
2023-07-04 10:59:51 -07:00
click.edit(filename=create(content_type.value, title, template_dir, category, template or content_type.value))
2022-08-02 18:20:45 -07:00
2023-07-04 12:35:04 -07:00
2023-07-04 10:59:51 -07:00
if __name__ == "__main__":
2023-07-04 12:35:04 -07:00
site_app()