forked from lakinwecker/ChessReanalysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interactive.py
executable file
·98 lines (86 loc) · 2.55 KB
/
interactive.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""
ChessReanalysis v0.1
"""
import glob, os, re
import chess.pgn
import preprocess, analyze
working_set = {}
game_link_regex = re.compile(r'^(https?://)?([a-z]+\.)?lichess\.org/([A-Za-z0-9]{8})([A-Za-z0-9]{4})?([/#\?].*)?$')
def gameid(game):
gamelink = game.headers['Site']
if gamelink is None or gamelink == '':
return None
match = game_link_regex.match(gamelink)
if match is None:
return None
return match.group(3)
def addpgn(filename):
with open(filename, encoding="iso-8859-1") as fin:
n = 0
while True:
game = chess.pgn.read_game(fin)
if not game:
break
gid = gameid(game)
if gid and game.headers.get('Variant') == 'Standard':
working_set[gid] = game
n += 1
print(f'Added {n} games to working set from {filename}')
def addpgnloop():
while True:
files = glob.glob(f'.{os.sep}pgn{os.sep}*.pgn')
print('')
for i, f in enumerate(files, 1):
print(f'({i}) {f}')
print('(^) Enter a regex to match multiple files by name')
print('(0) Cancel')
i = input()
if i == '0':
return
if i.startswith('^'):
regex = re.compile(i)
for f in files:
if regex.match(f.split(os.sep)[-1]):
addpgn(f)
return
try:
f = files[int(i) - 1]
addpgn(f)
return
except (IndexError, ValueError):
pass
def mainloop():
while True:
print('')
print(f'{len(working_set)} games in working set')
print('(1) Add PGN to working set')
print('(2) Clear working set')
print('(3) Pre-process')
print('(4) Analysis 1')
print('(0) Exit')
i = input()
if i == '1':
addpgnloop()
if i == '2':
working_set.clear()
if i == '3':
try:
preprocess.run(working_set)
except KeyboardInterrupt:
pass
if i == '4':
print('(1) Normal output')
print('(2) CSV output')
j = input()
try:
report_name = input('Report name: ')
if j == '1':
analyze.a1(working_set, report_name)
if j == '2':
analyze.a1csv(working_set, report_name)
except KeyboardInterrupt:
pass
if i == '0':
return
if __name__ == "__main__":
mainloop()