Files
ProgressQuest-cli/pqcli/__main__.py
T
Dash fd4a7d151d misc: replace Any with precise types
Curses screens, windows and pads are typed as curses.window, and the
widgets that may get no parent window take curses.window | None.

- EventHandler is generic over a ParamSpec, so each handler declares
  the arguments it passes, and MenuView is generic over the item its
  on_confirm hands out. Choice callbacks take no arguments.
- Scrollable is generic over its items, which DataTable and ListBox
  fix to tuple[str, str] and str.
- random.choice() and choice_low() are generic over the sequence, so
  the casts at their call sites are gone. BasicUserInterface.menu()
  and util.first() are generic too.
- Signal callbacks and their payloads are Callable[..., object] and
  object, since emit() only forwards them.
- JSON values in game_data are object and narrowed by isinstance,
  and data files are Traversable.
- Signal handlers take an int and a FrameType | None, and
  list_players() takes a TextIO.
- The focus_standout() context managers return
  Generator[None, None, None].

Any remains in three places: Player.__setstate__, whose pickled
__dict__ holds values of every type; Focusable.__init__, a mixin that
passes *args and **kwargs through to the next class; and the values
load_game_data() collects, whose element type depends on the field
each one fills.
2026-10-17 15:55:59 +02:00

154 lines
4.0 KiB
Python

import argparse
import sys
from pathlib import Path
from typing import TextIO
from xdg_base_dirs import xdg_config_home
from pqcli.game_data import (
DataError,
GameData,
dump_builtin_data,
load_game_data,
)
from pqcli.mechanic import Player
from pqcli.roster import Roster
from pqcli.ui.basic import BasicUserInterface
from pqcli.ui.curses import CursesUserInterface
SAVE_PATH = Path(xdg_config_home()) / "pqcli" / "save.dat"
DATA_PATH = Path(xdg_config_home()) / "pqcli" / "data"
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(prog="pqcli")
group = parser.add_mutually_exclusive_group()
group.set_defaults(ui=CursesUserInterface)
group.add_argument(
"--basic",
dest="ui",
action="store_const",
const=BasicUserInterface,
help="Use basic user interface (very crude, but uses least CPU)",
)
group.add_argument(
"--curses",
dest="ui",
action="store_const",
const=CursesUserInterface,
help="Use curses user interface (fast, but no colors output)",
)
parser.add_argument(
"--no-colors",
dest="colors",
action="store_false",
help="Disable color highlighting in curses interface",
)
parser.add_argument(
"--no-save",
dest="use_saves",
action="store_false",
help=argparse.SUPPRESS,
)
parser.add_argument("--cheats", action="store_true", help="???")
parser.add_argument(
"--list-saves",
action="store_true",
help="list saved characters and exit",
)
parser.add_argument(
"--load-save",
type=int,
metavar="NUM",
help="play chosen character",
)
parser.add_argument(
"--data-dir",
type=Path,
default=DATA_PATH,
metavar="DIR",
help=(
"directory with custom word lists; each NAME.json replaces "
f"the built-in list of that name (default: {DATA_PATH})"
),
)
parser.add_argument(
"--dump-data",
type=Path,
metavar="DIR",
help="copy the built-in word lists to DIR for editing and exit",
)
return parser.parse_args()
def list_players(roster: Roster, file: TextIO | None = None) -> None:
for i, player in enumerate(roster.players, start=1):
print(f"{i}. {player.name}", file=file)
def bootstrap_first_run(
roster: Roster, args: argparse.Namespace, data: GameData
) -> None:
if roster.players or args.list_saves or args.load_save:
return
print("No saved characters found. Starting first-run character creation.")
ui = BasicUserInterface(roster, None, args, data)
while not roster.players:
player = ui.create_player(auto_play=False)
if player:
return
if not ui.confirm("No character created. Do you want to try again?"):
print("A character is required for first run.", file=sys.stderr)
raise SystemExit(1)
def main() -> None:
args = parse_args()
if args.dump_data:
try:
paths = dump_builtin_data(args.dump_data)
except DataError as ex:
print(f"pqcli: {ex}", file=sys.stderr)
exit(1)
print(f"Wrote {len(paths)} files to {args.dump_data}")
exit(0)
try:
data = load_game_data(args.data_dir)
except DataError as ex:
print(f"pqcli: {ex}", file=sys.stderr)
exit(1)
roster = Roster.load(SAVE_PATH)
if args.list_saves:
list_players(roster)
exit(0)
player: Player | None = None
if args.load_save:
try:
player = list(roster.players)[args.load_save - 1]
except IndexError:
print("Invalid player. Available players:", file=sys.stderr)
list_players(roster, file=sys.stderr)
exit(1)
bootstrap_first_run(roster, args, data)
try:
ui = args.ui(roster, player, args, data)
ui.run()
finally:
if args.use_saves:
roster.save()
if __name__ == "__main__":
main()