ui: import and export .pqw files from the menus

The basic interface gets two main menu entries that ask for a path.
The curses roster gets [I] Import and [E] Export, with a new text input
view for the path, prefilled with NAME.pqw on export, and a message
view for the outcome and any warnings. The README explains how to move
characters to and from the web version.

Resolves #21
This commit is contained in:
Dash committed 2026-10-17 15:56:03 +02:00
1 parent 612b223a59
commit 804b0d8b38
7 files changed
+266 -3

No files matched your search

+45 -2
View File
@@ -6,10 +6,11 @@ import sys
import time
from collections.abc import Sequence
from datetime import datetime, timedelta
from pathlib import Path
from types import FrameType
from typing import TypeVar
from pqcli import lingo
from pqcli import lingo, pqw
from pqcli.config import PRIME_STATS
from pqcli.game_data import GameData
from pqcli.mechanic import Player, Simulation, StatsBuilder, create_player
@@ -34,7 +35,9 @@ class MainMenu(enum.IntEnum):
play = 2
info = 3
delete = 4
quit = 5
import_ = 5
export = 6
quit = 7
class BasicUserInterface(BaseUserInterface):
@@ -79,6 +82,8 @@ class BasicUserInterface(BaseUserInterface):
(MainMenu.play, "Play as character"),
(MainMenu.info, "Query character info"),
(MainMenu.delete, "Delete a character"),
(MainMenu.import_, "Import a character (.pqw)"),
(MainMenu.export, "Export a character (.pqw)"),
(MainMenu.quit, "Quit"),
]
)
@@ -104,6 +109,15 @@ class BasicUserInterface(BaseUserInterface):
continue
self.delete_player(player)
elif choice == MainMenu.import_:
self.import_player()
elif choice == MainMenu.export:
player = self.choose_player()
if not player:
continue
self.export_player(player)
elif choice == MainMenu.quit:
self.quit()
break
@@ -201,6 +215,35 @@ class BasicUserInterface(BaseUserInterface):
if self.args.use_saves:
self.roster.save()
def import_player(self) -> None:
path = input("Import from .pqw file: ").strip()
if not path:
print("Cancelled.")
return
try:
player, warnings = pqw.read_file(
Path(path).expanduser(), self.data
)
except pqw.PqwError as ex:
print(f"Cannot import: {ex}")
return
for warning in warnings:
print(f"Warning: {warning}")
self.roster.players.append(player)
if self.args.use_saves:
self.roster.save()
print(f"Imported {player.name}.")
def export_player(self, player: Player) -> None:
default = pqw.default_file_name(player)
path = input(f"Export to .pqw file [{default}]: ").strip() or default
try:
pqw.write_file(player, Path(path).expanduser(), self.data)
except pqw.PqwError as ex:
print(f"Cannot export: {ex}")
return
print(f"Exported {player.name} to {path}.")
def choose_player(self) -> Player | None:
if not self.roster.players:
print("No characters to choose from!")