-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpgnutil.py
More file actions
35 lines (30 loc) · 1.01 KB
/
Copy pathpgnutil.py
File metadata and controls
35 lines (30 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"""
Shared PGN text utilities used across pipeline stages.
"""
import io
import chess.pgn
def split_pgn_games(text: str) -> list[str]:
"""
Split a multi-game PGN text blob into a list of individual game PGN
strings, re-serialised through python-chess so each is complete and
self-contained.
Deliberately not a '\\n(?=\\[)' regex split — that splits between
every header line, not just between games, since a normal PGN header
block has no blank lines between tags. It silently produces one
fragment per header line plus a movetext fragment missing most of
its headers (including White/Black), which then never attributes to
any player.
"""
games = []
buf = io.StringIO(text)
while True:
try:
game = chess.pgn.read_game(buf)
except Exception:
break
if game is None:
break
out = io.StringIO()
game.accept(chess.pgn.FileExporter(out))
games.append(out.getvalue().strip())
return games