Files
Ziad ArafatandClaude Opus 5.5 b575ab6f75 doge_alignment: chart pictures, one command group, Discord-free core
Draw the chart with ImageMagick: one 3x3 picture, or nine tiles sent as a
Components V2 media gallery (3x3), with avatars cut into circles. Names and
avatars are looked up live; people who left are greyed out.

Move all logic into doge_alignment/core, which imports neither discord.py
nor Red: immutable Chart dataclasses with pure operations, Config/YAML
storage, a view model, pure layout geometry, a Wand painter and an avatar
fetcher. The cog and a new CLI (python -m doge_alignment) are front ends.

Commands are now one hybrid `alignment` group (text and slash): show, set,
remove, prune, clear, style, export, import, admin add/remove/list. Red's
admin/mod roles, Administrators and the server owner count as admins.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GUebqhE8dK19vyn4Ct5g1L
2026-11-05 11:34:36 -07:00

96 lines
3.3 KiB
Python

"""Command-line front end: render a YAML chart without Discord.
python -m doge_alignment text chart.yaml
python -m doge_alignment image chart.yaml -o chart.png
python -m doge_alignment tiles chart.yaml -o tiles/
The YAML is what ``/alignment export`` produces. Everyone in the file counts as
present, and avatars are downloaded from their saved URLs unless
``--no-avatars`` is given.
"""
import argparse
import asyncio
from collections.abc import Sequence
from pathlib import Path
import aiohttp
from .core.fetch import avatar_urls, by_user, fetch_images
from .core.storage import ChartFormatError, from_yaml
from .core.view import (
Cell,
cells,
everyone,
format_text,
tile_filename,
)
def _parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
prog="python -m doge_alignment",
description="Show an exported alignment chart.",
)
parser.add_argument(
"mode",
choices=("text", "image", "tiles"),
help="text, one PNG, or one PNG per alignment",
)
parser.add_argument("chart", type=Path, help="exported YAML chart")
parser.add_argument(
"-o",
"--output",
type=Path,
help="PNG file (image) or directory (tiles)",
)
parser.add_argument(
"--no-avatars",
action="store_true",
help="draw initials instead of downloading avatars",
)
return parser
async def _avatars(chart_cells: tuple[Cell, ...]) -> dict[str, bytes]:
people = everyone(chart_cells)
async with aiohttp.ClientSession() as session:
images = await fetch_images(session, avatar_urls(people))
return by_user(people, images)
def main(argv: Sequence[str] | None = None) -> int:
"""Run the command line; return the exit status."""
parser = _parser()
args = parser.parse_args(argv)
try:
chart = from_yaml(args.chart.read_text(encoding="utf-8"))
except (OSError, ChartFormatError) as error:
parser.error(f"{args.chart}: {error}")
chart_cells = cells(chart)
if args.mode == "text":
print(format_text(chart_cells) or "Nobody is on the chart.") # noqa: T201
return 0
# Rendering needs ImageMagick; text mode doesn't
from .core.render import render_chart, render_tiles # noqa: PLC0415
avatars = {} if args.no_avatars else asyncio.run(_avatars(chart_cells))
if args.mode == "image":
out = args.output or Path("alignment.png")
out.write_bytes(render_chart(chart_cells, avatars))
print(out) # noqa: T201
return 0
folder = args.output or Path("alignment-tiles")
folder.mkdir(parents=True, exist_ok=True)
for index, (cell, png) in enumerate(
zip(
chart_cells,
render_tiles(chart_cells, avatars),
strict=True,
),
start=1,
):
(folder / tile_filename(index, cell)).write_bytes(png)
print(folder) # noqa: T201
return 0