feat(opening): ship PyChess' opening book and eco.db with the program
public
feat(opening): ship PyChess' opening book and eco.db with the program Both files come from the PyChess repository (GPL-3.0, combinable with AGPL-3.0) and now live in schach/daten/ with a HERKUNFT.md, so opening names and book moves work without PyChess installed. The bundled book is always listed first; copies of it elsewhere are recognised by size and content and listed once. eco.db is opened read-only. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| Changeset | 1f217976cc30df2ee625dae27bc05bff2432580a |
|---|---|
| Author (claimed) | Timo Stolz <timo.stolz@nullachtvierzehn.de>, |
| Parents | 9b1634b37f2a |
| Children | 7cc599b649b3 |
| Pushlog | by timo via ssh by timo via ssh |
modified README.md +9 −8
| @@ -50,12 +50,13 @@ Schlichtes GNOME-Schachprogramm (Python, | ||
| 50 | 50 | spielbarer Buchzug (mit Namen, z. B. „Spanische Partie“), kein Buchzug aber in Ordnung, |
| 51 | 51 | oder Fehler. Zugumstellungen zählen (Vergleich nach Stellung, nicht nach Zugfolge). |
| 52 | 52 | Hinweis zeigt den Kurszug. Am Ende: „Nächste Linie“ oder „Gegen Maia weiterspielen“. |
| 53 | - Eröffnungsnamen stammen von PyChess (`eco.db`). Als Buchzug zählt, was in irgendeinem | |
| 54 | - Eröffnungsbuch steht. | |
| 55 | -- **Eröffnungsbücher** (Einstellungen → Allgemein): beliebig viele Polyglot-Bücher (`.bin`) – | |
| 56 | - per „+“ wählen oder in den Bücherordner legen (`~/.local/share/schach/buecher/`); PyChess' | |
| 57 | - `pychess_book.bin` wird mitbenutzt, wenn installiert. In der Seitenleiste unter „Buch“ hat | |
| 58 | - jedes Buch eine eigene Sektion zum Auf- und Zuklappen, mit eigenen Anteilen. | |
| 53 | + Als Buchzug zählt, was in irgendeinem Eröffnungsbuch steht. | |
| 54 | +- **Eröffnungsbücher und -namen:** mitgeliefert sind PyChess' Buch aus Partien starker Spieler | |
| 55 | + (`pychess_book.bin`) und die Eröffnungsnamen (`eco.db`, auch auf Deutsch), beide in | |
| 56 | + `schach/daten/` (Herkunft: `schach/daten/HERKUNFT.md`). Weitere Polyglot-Bücher (`.bin`) | |
| 57 | + unter Einstellungen → Allgemein per „+“ oder im Bücherordner (`~/.local/share/schach/buecher/`). | |
| 58 | + In der Seitenleiste unter „Buch“ hat jedes Buch eine eigene Sektion zum Auf- und Zuklappen, | |
| 59 | + mit eigenen Anteilen. | |
| 59 | 60 | - **Zwei Spieler** (Pfeil neben `+`): abwechselnd am selben Rechner. Handicap pro Farbe: |
| 60 | 61 | Anzahl Hinweise (Hinweis und Drohung kosten je einen) und welche Zugzeichen man sieht |
| 61 | 62 | (keine / eigene Züge / alle – nur solange diese Farbe am Zug ist; Standard: keine). |
| @@ -87,8 +88,8 @@ Schlichtes GNOME-Schachprogramm (Python, | ||
| 87 | 88 | |
| 88 | 89 | Abhängigkeiten: `python-gobject`, `libadwaita`, `librsvg`, `python-chess`, |
| 89 | 90 | `stockfish`, `python-requests`, `python-keyring`, und für Maia-3 `onnxruntime-cpu` (oder eine |
| 90 | -andere onnxruntime-Variante), `python-numpy` sowie `python-onnx` (wandelt das Modell einmal | |
| 91 | -nach fp32 um – ohne rechnet Maia auf der CPU rund zehnmal langsamer). | |
| 91 | +andere onnxruntime-Variante), `python-numpy`; optional `python-onnx` (wandelt das Modell einmal | |
| 92 | +nach fp32 um – auf der CPU rund 35 statt 100 ms je Zug). | |
| 92 | 93 | |
| 93 | 94 | Maia-3 läuft über ONNX Runtime statt PyTorch (`schach/maia_onnx.py`). Das Modell (ONNX-Export |
| 94 | 95 | von Maia3-79M mit Zughistorie, `bqrio/maia3-onnx`, 156 MB, per Prüfsumme festgelegt) lädt |
added schach/daten/HERKUNFT.md +14 −0
| @@ -0,0 +1,14 @@ | ||
| 1 | +# Mitgelieferte Daten | |
| 2 | + | |
| 3 | +Beide Dateien stammen aus PyChess (https://github.com/pychess/pychess, GPL-3.0), | |
| 4 | +hier aus pychess 1.2.0. GPL-3.0 und die AGPL-3.0 dieses Programms erlauben die | |
| 5 | +Kombination (jeweils §13). | |
| 6 | + | |
| 7 | +- `pychess_book.bin` – Eröffnungsbuch im Polyglot-Format, im PyChess-Repository | |
| 8 | + unter diesem Namen. Gebaut aus Partien starker Spieler | |
| 9 | + (`polyglot make-book -min-game 10`): ein Zug steht nur drin, wenn er mindestens | |
| 10 | + zehnmal gespielt wurde. Rund 95.000 Stellungen, bis etwa Zug 13. | |
| 11 | +- `eco.db` – Eröffnungsnamen (SQLite: Polyglot-Hash der Stellung → ECO-Code und | |
| 12 | + Name, in en, de, fr, es, da, hu). PyChess erzeugt sie beim Bauen mit | |
| 13 | + `pgn2ecodb.py` aus `lang/*/eco.pgn`; die deutschen Namen kommen aus | |
| 14 | + `lang/de/eco.pgn`. | |
added schach/daten/eco.db +0 −0
Binary file changed.
added schach/daten/pychess_book.bin +0 −0
Binary file changed.
modified schach/opening.py +29 −15
| @@ -4,14 +4,15 @@ Alles wird über die Stellung gesucht (Polyglot-Zobrist-Hash), nicht über die | ||
| 4 | 4 | Zugfolge. Damit zählen Zugumstellungen von selbst: Wer auf anderem Weg in eine |
| 5 | 5 | Kursstellung kommt, ist trotzdem im Kurs. |
| 6 | 6 | |
| 7 | -Daten von PyChess: Eröffnungsbuch ``pychess_book.bin`` (Polyglot) und | |
| 8 | -``eco.db`` (Eröffnungsnamen, auch auf Deutsch; der Hash ist derselbe). | |
| 7 | +Daten von PyChess, mitgeliefert in ``daten/``: Eröffnungsbuch ``pychess_book.bin`` | |
| 8 | +(Polyglot) und ``eco.db`` (Eröffnungsnamen, auch auf Deutsch; der Hash ist derselbe). | |
| 9 | 9 | """ |
| 10 | 10 | |
| 11 | 11 | import dataclasses |
| 12 | 12 | import glob |
| 13 | 13 | import io |
| 14 | 14 | import os |
| 15 | +import pathlib | |
| 15 | 16 | import re |
| 16 | 17 | import sqlite3 |
| 17 | 18 | |
| @@ -21,8 +22,9 @@ import chess.polyglot | ||
| 21 | 22 | from gi.repository import GLib |
| 22 | 23 | |
| 23 | 24 | COURSE_DIR = os.path.join(os.path.dirname(__file__), "kurse") |
| 24 | -BOOK_PATH = "/usr/share/pychess/pychess_book.bin" | |
| 25 | -ECO_PATH = "/usr/share/pychess/eco.db" | |
| 25 | +DATA_DIR = os.path.join(os.path.dirname(__file__), "daten") # mitgeliefert, siehe daten/HERKUNFT.md | |
| 26 | +BOOK_PATH = os.path.join(DATA_DIR, "pychess_book.bin") | |
| 27 | +ECO_PATH = os.path.join(DATA_DIR, "eco.db") | |
| 26 | 28 | |
| 27 | 29 | def key(board): |
| 28 | 30 | return chess.polyglot.zobrist_hash(board) |
| @@ -195,26 +197,37 @@ def _book_name(path): | ||
| 195 | 197 | BOOK_DIR = os.path.join(GLib.get_user_data_dir(), "schach", "buecher") |
| 196 | 198 | |
| 197 | 199 | |
| 198 | -def book_paths(extra=(), book_dir=None, legacy=BOOK_PATH): | |
| 199 | - """Alle Polyglot-Bücher: ausdrücklich gewählte, dann ``*.bin`` im Bücherordner, dann | |
| 200 | - PyChess' Buch, falls installiert. Ohne Doppelte (gleiche Datei über zwei Wege).""" | |
| 200 | +def book_paths(extra=(), book_dir=None, builtin=BOOK_PATH): | |
| 201 | + """Alle Polyglot-Bücher: das mitgelieferte, dann ausdrücklich gewählte, dann ``*.bin`` im | |
| 202 | + Bücherordner. Ohne Doppelte – auch nicht, wenn dieselbe Datei als Kopie woanders liegt.""" | |
| 201 | 203 | book_dir = BOOK_DIR if book_dir is None else book_dir |
| 202 | - found = list(extra) | |
| 204 | + found = ([builtin] if builtin else []) + list(extra) | |
| 203 | 205 | if os.path.isdir(book_dir): |
| 204 | 206 | found += sorted(glob.glob(os.path.join(book_dir, "*.bin"))) |
| 205 | - if legacy: | |
| 206 | - found.append(legacy) | |
| 207 | 207 | seen, result = set(), [] |
| 208 | 208 | for path in found: |
| 209 | 209 | if not os.path.isfile(path): |
| 210 | 210 | continue |
| 211 | - real = os.path.realpath(path) | |
| 212 | - if real not in seen: | |
| 213 | - seen.add(real) | |
| 211 | + key = _fingerprint(path) | |
| 212 | + if key not in seen: | |
| 213 | + seen.add(key) | |
| 214 | 214 | result.append(path) |
| 215 | 215 | return result |
| 216 | 216 | |
| 217 | 217 | |
| 218 | +def _fingerprint(path, sample=1 << 16): | |
| 219 | + """Größe und Anfang/Ende der Datei: erkennt Kopien, ohne große Bücher ganz zu lesen.""" | |
| 220 | + import hashlib | |
| 221 | + size = os.path.getsize(path) | |
| 222 | + digest = hashlib.sha256() | |
| 223 | + with open(path, "rb") as fh: | |
| 224 | + digest.update(fh.read(sample)) | |
| 225 | + if size > sample: | |
| 226 | + fh.seek(max(sample, size - sample)) | |
| 227 | + digest.update(fh.read(sample)) | |
| 228 | + return size, digest.hexdigest() | |
| 229 | + | |
| 230 | + | |
| 218 | 231 | class Books: |
| 219 | 232 | """Mehrere Bücher. Für das Training zählt ein Zug, wenn er in irgendeinem steht; |
| 220 | 233 | die Seitenleiste zeigt jedes Buch einzeln (``books``).""" |
| @@ -249,8 +262,9 @@ class Names: | ||
| 249 | 262 | def __init__(self, path=ECO_PATH): |
| 250 | 263 | self._conn = None |
| 251 | 264 | if os.path.exists(path): |
| 252 | - try: | |
| 253 | - self._conn = sqlite3.connect(path, check_same_thread=False) | |
| 265 | + try: # nur lesen: Programmordner kann schreibgeschützt sein | |
| 266 | + uri = pathlib.Path(os.path.abspath(path)).as_uri() + "?mode=ro" | |
| 267 | + self._conn = sqlite3.connect(uri, uri=True, check_same_thread=False) | |
| 254 | 268 | except sqlite3.Error: |
| 255 | 269 | self._conn = None |
| 256 | 270 | |
modified schach/prefs.py +1 −1
| @@ -112,7 +112,7 @@ class Preferences(Adw.PreferencesDialog) | ||
| 112 | 112 | remove.connect("clicked", lambda _b, p=path: self._set_books([c for c in chosen if c != p])) |
| 113 | 113 | row.add_suffix(remove) |
| 114 | 114 | elif path == opening.BOOK_PATH: |
| 115 | - row.set_subtitle("von PyChess installiert") | |
| 115 | + row.set_subtitle("mitgeliefert – Partien starker Spieler") | |
| 116 | 116 | else: |
| 117 | 117 | row.set_subtitle("aus dem Bücherordner") |
| 118 | 118 | group.add(row) |
modified tests/test_opening.py +16 −3
| @@ -97,7 +97,6 @@ class CourseTest(unittest.TestCase): | ||
| 97 | 97 | ) |
| 98 | 98 | |
| 99 | 99 | |
| 100 | -@unittest.skipUnless(os.path.exists(opening.ECO_PATH), "eco.db von PyChess fehlt") | |
| 101 | 100 | class NamesTest(unittest.TestCase): |
| 102 | 101 | def test_english_names_translated_whole(self): |
| 103 | 102 | self.assertEqual(opening._german("King's Pawn"), "Königsbauernspiel") |
| @@ -115,7 +114,6 @@ class NamesTest(unittest.TestCase): | ||
| 115 | 114 | self.assertTrue(names.name(play("e4", "e5", "Nf3", "Nc6", "Bc4", "h6", "a3", "a6"))) |
| 116 | 115 | |
| 117 | 116 | |
| 118 | -@unittest.skipUnless(os.path.exists(opening.BOOK_PATH), "Eröffnungsbuch von PyChess fehlt") | |
| 119 | 117 | class BookTest(unittest.TestCase): |
| 120 | 118 | def test_book_knows_main_moves(self): |
| 121 | 119 | moves = [m.uci() for m, _w in opening.Book().moves(play("e4", "e5", "Nf3", "Nc6"))] |
| @@ -157,9 +155,24 @@ class BooksTest(unittest.TestCase): | ||
| 157 | 155 | def test_paths_without_duplicates(self): |
| 158 | 156 | link = os.path.join(self.dir.name, "link.bin") |
| 159 | 157 | os.symlink(self.a, link) |
| 160 | - paths = opening.book_paths(extra=[link, "/gibt/es/nicht.bin"], book_dir=self.dir.name, legacy=None) | |
| 158 | + paths = opening.book_paths(extra=[link, "/gibt/es/nicht.bin"], book_dir=self.dir.name, builtin=None) | |
| 161 | 159 | self.assertEqual(paths, [link, self.b]) # a.bin ist dieselbe Datei wie link.bin |
| 162 | 160 | |
| 161 | + def test_copy_of_builtin_counts_once(self): | |
| 162 | + import shutil | |
| 163 | + copy = os.path.join(self.dir.name, "kopie.bin") | |
| 164 | + shutil.copy(opening.BOOK_PATH, copy) | |
| 165 | + paths = opening.book_paths(book_dir=self.dir.name) | |
| 166 | + self.assertEqual(paths[0], opening.BOOK_PATH) | |
| 167 | + self.assertNotIn(copy, paths) | |
| 168 | + | |
| 169 | + def test_bundled_data_present(self): | |
| 170 | + self.assertTrue(opening.Book().available) | |
| 171 | + board = chess.Board() | |
| 172 | + for san in ("e4", "e5", "Nf3", "Nc6", "Bc4"): | |
| 173 | + board.push_san(san) | |
| 174 | + self.assertIn("Italien", opening.Names().name(board) or "") | |
| 175 | + | |
| 163 | 176 | def test_union(self): |
| 164 | 177 | books = opening.Books([self.a, self.b]) |
| 165 | 178 | self.assertEqual([b.name for b in books.books], ["a", "b buch"]) |