-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgnviewer.py
executable file
·66 lines (55 loc) · 1.61 KB
/
pgnviewer.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
"""usage: pgnviewer <PGNfile> [query]"""
import os
import shutil
import sys
import textwrap
from pathlib import Path
from docopt import docopt
from chess import Board
from chess.pgn import read_headers, read_game
from chess.svg import board as display
def display(board, title, moves_played):
os.system("clear")
print(title)
print()
print(board)
print()
for chunk in moves_played[-5:]:
print(chunk)
def choose_game(pgnpath):
pgn = open(pgnpath, "r")
games_in_pgn = []
while True:
offset = pgn.tell()
header = read_headers(pgn)
if header is None:
break
game_desc = f"{header['White']} vs {header['Black']}, {header['Result']}"
games_in_pgn.append((game_desc, offset))
chosen = games_in_pgn[0]
if len(games_in_pgn) > 1:
for i, (head, peek) in enumerate(games_in_pgn):
print(f"{i+1:3d}: {head}")
chosen = games_in_pgn[int(input("Choose: ")) - 1]
pgn.seek(chosen[1])
return read_game(pgn), chosen[0]
def main():
args = docopt(__doc__)
game, title = choose_game(args["<PGNfile>"])
b = Board()
moves, annotations = [], []
for i, move in enumerate(game.mainline_moves()):
if i % 2 == 0:
moves.append(f"{int(i/2)+1}. {b.san(move)}")
else:
moves[-1] += f" {b.san(move)} "
b.push(move)
display(b, title, moves)
annotate = input("> ")
if annotate:
annotations.append((int(i / 2) + 1, annotate))
print(annotate)
print("FINISHED")
if __name__ == "__main__":
sys.exit(main())