feat(board): own piece sets from a folder, keep the saved choice
public
feat(board): own piece sets from a folder, keep the saved choice Each subfolder of the user piece folder with the twelve SVGs appears in the preferences as "(eigener Satz)" - e.g. for sets whose license only allows private use, like Staunty. A missing set now only falls back on screen; the saved setting is no longer replaced by cburnett, so a folder that is briefly unavailable doesn't lose the choice. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| Changeset | 8f9a5dfd351b469530e948c7a34fa8589783e042 |
|---|---|
| Author (claimed) | Timo Stolz <timo.stolz@nullachtvierzehn.de>, |
| Parents | a7f81012ae72 |
| Children | fd6b1dcbd9f6 |
| Pushlog | by timo via ssh by timo via ssh |
modified README.md +3 −1
| @@ -80,7 +80,9 @@ Schlichtes GNOME-Schachprogramm (Python, | ||
| 80 | 80 | Zwischenablage – Bilder als Bild, HTML als formatierter Text. |
| 81 | 81 | - **Einstellungen** (Strg+,): Standardfarbe, Streuung von Maia, Brettfarben (Holz, |
| 82 | 82 | Schiefer, Grün), Notation (deutsch Sf3 oder englisch Nf3 – gilt für Zugliste und |
| 83 | - Erklärungen; PGN-Dateien bleiben international), 20 mitgelieferte Figurensätze (von Lichess und Maurizio Monge, `schach/figuren/HERKUNFT.md`), Lichess-Konto. | |
| 83 | + Erklärungen; PGN-Dateien bleiben international), 20 mitgelieferte Figurensätze (von Lichess und Maurizio Monge, `schach/figuren/HERKUNFT.md`) | |
| 84 | + plus eigene im Figurenordner (`~/.local/share/schach/figuren/<name>/wk.svg` … `bp.svg`) – etwa | |
| 85 | + Sätze, deren Lizenz nur private Nutzung erlaubt –, Lichess-Konto. | |
| 84 | 86 | |
| 85 | 87 | ## Starten |
| 86 | 88 | |
modified schach/app.py +0 −2
| @@ -88,8 +88,6 @@ def load_settings(): | ||
| 88 | 88 | # früher: Index der Lichess-Bedenkzeit; jetzt eigener Schlüssel, "time_control" = lokale Uhr |
| 89 | 89 | settings["lichess_time_control"] = settings["time_control"] |
| 90 | 90 | settings["time_control"] = DEFAULTS["time_control"] |
| 91 | - if settings.get("theme") not in {name for name, _label in available_themes()}: | |
| 92 | - settings["theme"] = DEFAULTS["theme"] # früher auch PyChess-Sätze und „python-chess“ | |
| 93 | 91 | return settings |
| 94 | 92 | |
| 95 | 93 | |
modified schach/diagram.py +28 −11
| @@ -15,13 +15,16 @@ import chess.svg | ||
| 15 | 15 | import gi |
| 16 | 16 | |
| 17 | 17 | gi.require_version("Rsvg", "2.0") |
| 18 | -from gi.repository import Rsvg # noqa: E402 | |
| 18 | +from gi.repository import GLib, Rsvg # noqa: E402 | |
| 19 | 19 | |
| 20 | 20 | STRIP = 0.5 # Höhe der Streifen für geschlagene Figuren, in Feldgrößen |
| 21 | 21 | BAR = 0.32 # Breite des Gewinnchance-Balkens neben dem Brett, in Feldgrößen |
| 22 | 22 | VALUE = {chess.PAWN: 1, chess.KNIGHT: 3, chess.BISHOP: 3, chess.ROOK: 5, chess.QUEEN: 9, chess.KING: 0} |
| 23 | 23 | |
| 24 | 24 | PIECES_DIR = os.path.join(os.path.dirname(__file__), "figuren") # mitgeliefert, siehe figuren/HERKUNFT.md |
| 25 | +# Eigene Sätze (z. B. mit Lizenzen, die nur private Nutzung erlauben): je Unterordner zwölf SVGs | |
| 26 | +USER_PIECES_DIR = os.path.join(GLib.get_user_data_dir(), "schach", "figuren") | |
| 27 | +PIECE_FILES = [c + p + ".svg" for c in "wb" for p in "kqrbnp"] | |
| 25 | 28 | BUILTIN_THEME = "cburnett" # Standard; fehlt eine Datei, zeichnet python-chess dieselben Figuren |
| 26 | 29 | # (Name, Bezeichnung in den Einstellungen) – in dieser Reihenfolge |
| 27 | 30 | PIECE_SETS = [ |
| @@ -86,19 +89,33 @@ def hex_color(rgb): | ||
| 86 | 89 | return "#" + "".join(f"{round(c * 255):02x}" for c in rgb[:3]) |
| 87 | 90 | |
| 88 | 91 | |
| 89 | -def available_themes(): | |
| 90 | - """[(Name, Bezeichnung)] der mitgelieferten Figurensätze.""" | |
| 91 | - return [(name, label) for name, label in PIECE_SETS | |
| 92 | - if os.path.isfile(os.path.join(PIECES_DIR, name, "wk.svg"))] | |
| 92 | +def available_themes(user_dir=None): | |
| 93 | + """[(Name, Bezeichnung)]: die mitgelieferten Sätze, dann eigene aus dem Figurenordner | |
| 94 | + (nur vollständige; ein eigener Satz mit dem Namen eines mitgelieferten zählt nicht).""" | |
| 95 | + builtin = [(name, label) for name, label in PIECE_SETS | |
| 96 | + if os.path.isfile(os.path.join(PIECES_DIR, name, "wk.svg"))] | |
| 97 | + user_dir = USER_PIECES_DIR if user_dir is None else user_dir | |
| 98 | + own = [] | |
| 99 | + if os.path.isdir(user_dir): | |
| 100 | + taken = {name for name, _label in PIECE_SETS} | |
| 101 | + for name in sorted(os.listdir(user_dir), key=str.lower): | |
| 102 | + folder = os.path.join(user_dir, name) | |
| 103 | + if name not in taken and all(os.path.isfile(os.path.join(folder, f)) for f in PIECE_FILES): | |
| 104 | + own.append((name, f"{name.replace('_', ' ').replace('-', ' ').title()} (eigener Satz)")) | |
| 105 | + return builtin + own | |
| 93 | 106 | |
| 94 | 107 | |
| 95 | -def piece_svg(theme, symbol): | |
| 96 | - """SVG-Quelltext einer Figur (``symbol`` wie in FEN: K, q …) aus dem Figurensatz.""" | |
| 108 | +def piece_svg(theme, symbol, user_dir=None): | |
| 109 | + """SVG-Quelltext einer Figur (``symbol`` wie in FEN: K, q …) aus dem Figurensatz. Fehlt der | |
| 110 | + Satz (z. B. eigener Ordner gerade nicht da), zeichnet python-chess – die gespeicherte Wahl | |
| 111 | + bleibt davon unberührt.""" | |
| 97 | 112 | piece = chess.Piece.from_symbol(symbol) |
| 98 | - path = os.path.join(PIECES_DIR, theme, ("w" if piece.color else "b") + symbol.lower() + ".svg") | |
| 99 | - if os.path.isfile(path): | |
| 100 | - with open(path, "rb") as fh: | |
| 101 | - return fh.read() | |
| 113 | + filename = ("w" if piece.color else "b") + symbol.lower() + ".svg" | |
| 114 | + for base in (PIECES_DIR, USER_PIECES_DIR if user_dir is None else user_dir): | |
| 115 | + path = os.path.join(base, theme, filename) | |
| 116 | + if theme and os.path.isfile(path): | |
| 117 | + with open(path, "rb") as fh: | |
| 118 | + return fh.read() | |
| 102 | 119 | return chess.svg.piece(piece).encode() |
| 103 | 120 | |
| 104 | 121 | |
modified schach/prefs.py +16 −0
| @@ -10,6 +10,7 @@ from gi.repository import Adw, Gio, GLib | ||
| 10 | 10 | |
| 11 | 11 | from . import notation, opening # noqa: E402 |
| 12 | 12 | from .board import DEFAULT_SCHEME, SCHEMES, scheme_colors # noqa: E402 |
| 13 | +from .diagram import USER_PIECES_DIR # noqa: E402 | |
| 13 | 14 | |
| 14 | 15 | COLORS = [("white", "Weiß"), ("black", "Schwarz"), ("random", "Zufällig")] |
| 15 | 16 | |
| @@ -152,6 +153,10 @@ class Preferences(Adw.PreferencesDialog) | ||
| 152 | 153 | if path not in chosen: |
| 153 | 154 | self._set_books(chosen + [path]) |
| 154 | 155 | |
| 156 | + def _on_open_pieces_dir(self, _row): | |
| 157 | + os.makedirs(USER_PIECES_DIR, exist_ok=True) | |
| 158 | + Gtk.FileLauncher.new(Gio.File.new_for_path(USER_PIECES_DIR)).launch(self.win, None, None, None) | |
| 159 | + | |
| 155 | 160 | def _on_open_book_dir(self, _row): |
| 156 | 161 | os.makedirs(opening.BOOK_DIR, exist_ok=True) |
| 157 | 162 | Gtk.FileLauncher.new(Gio.File.new_for_path(opening.BOOK_DIR)).launch(self.win, None, None, None) |
| @@ -170,8 +175,19 @@ class Preferences(Adw.PreferencesDialog) | ||
| 170 | 175 | current = self.win.settings.get("theme") |
| 171 | 176 | if current in names: |
| 172 | 177 | row.set_selected(names.index(current)) |
| 178 | + else: | |
| 179 | + row.set_subtitle("Gewählter Satz fehlt gerade – es wird Cburnett gezeigt") | |
| 173 | 180 | row.connect("notify::selected", lambda r, _p: self._set("theme", names[r.get_selected()])) |
| 174 | 181 | pieces.add(row) |
| 182 | + folder = Adw.ActionRow( | |
| 183 | + title="Eigene Figurensätze", activatable=True, | |
| 184 | + subtitle="Je Satz ein Unterordner mit wk.svg, wq.svg … bp.svg – erscheint nach dem " | |
| 185 | + "nächsten Öffnen der Einstellungen in der Liste", | |
| 186 | + tooltip_text=GLib.markup_escape_text(USER_PIECES_DIR), | |
| 187 | + ) | |
| 188 | + folder.add_suffix(Gtk.Image(icon_name="folder-open-symbolic")) | |
| 189 | + folder.connect("activated", self._on_open_pieces_dir) | |
| 190 | + pieces.add(folder) | |
| 175 | 191 | page.add(pieces) |
| 176 | 192 | |
| 177 | 193 | moves = Adw.PreferencesGroup( |
modified tests/test_pieces.py +15 −1
| @@ -12,7 +12,7 @@ class PieceSetsTest(unittest.TestCase): | ||
| 12 | 12 | import gi |
| 13 | 13 | gi.require_version("Rsvg", "2.0") |
| 14 | 14 | from gi.repository import Rsvg |
| 15 | - names = [name for name, _label in diagram.available_themes()] | |
| 15 | + names = [name for name, _label in diagram.available_themes(user_dir="/gibt/es/nicht")] | |
| 16 | 16 | self.assertEqual(len(names), len(diagram.PIECE_SETS)) |
| 17 | 17 | self.assertEqual(names[0], diagram.BUILTIN_THEME) |
| 18 | 18 | for name in names: |
| @@ -33,6 +33,20 @@ class PieceSetsTest(unittest.TestCase): | ||
| 33 | 33 | for name, _label in diagram.PIECE_SETS: |
| 34 | 34 | self.assertIn(f"| {name} |", credits, name) |
| 35 | 35 | |
| 36 | + def test_user_sets(self): | |
| 37 | + import shutil | |
| 38 | + import tempfile | |
| 39 | + with tempfile.TemporaryDirectory() as tmp: | |
| 40 | + shutil.copytree(os.path.join(diagram.PIECES_DIR, "merida"), os.path.join(tmp, "mein_satz")) | |
| 41 | + shutil.copytree(os.path.join(diagram.PIECES_DIR, "merida"), os.path.join(tmp, "cburnett")) # Name belegt | |
| 42 | + os.makedirs(os.path.join(tmp, "halb")) | |
| 43 | + shutil.copy(os.path.join(diagram.PIECES_DIR, "merida", "wk.svg"), os.path.join(tmp, "halb")) | |
| 44 | + themes = diagram.available_themes(user_dir=tmp) | |
| 45 | + own = themes[len(diagram.PIECE_SETS):] | |
| 46 | + self.assertEqual(own, [("mein_satz", "Mein Satz (eigener Satz)")]) | |
| 47 | + with open(os.path.join(tmp, "mein_satz", "bq.svg"), "rb") as fh: | |
| 48 | + self.assertEqual(diagram.piece_svg("mein_satz", "q", user_dir=tmp), fh.read()) | |
| 49 | + | |
| 36 | 50 | def test_unknown_set_falls_back(self): |
| 37 | 51 | self.assertTrue(diagram.piece_svg("gibt-es-nicht", "K").startswith(b"<svg")) |
| 38 | 52 | |