dnd/deadsands/site_tools/cli.py

137 lines
3.5 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
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
source_path = Path(build_system.CONFIG['data_path']).expanduser() / build_system.CONFIG['campaign_name'] / 'sources'
locations = dict((loc.name, loc) for loc in (source_path / 'stores').iterdir() if loc.is_dir())
Location = Enum("Location", ((name, name) for name in locations.keys()))
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(
location: Location = typer.Argument(..., help="Where to restock."),
frequency: 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"),
template_dir: str = typer.Argument(
build_system.CONFIG["templates_path"],
help="Override the default location for markdown templates.",
),
2023-05-13 13:24:43 -07:00
) -> None:
for store in locations[location.name].iterdir():
rt = RollTable([store.read_text()], frequency=frequency, die=int(die), hide_rolls=True)
store_attributes = rt.datasources[0].metadata["store"]
click.edit(
filename=create(
content_type="post",
title=store_attributes["title"],
template_dir=template_dir,
category="stores",
template="store",
extra_context=dict(inventory=rt.as_markdown(), **store_attributes),
)
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()