-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.py
50 lines (40 loc) · 1.32 KB
/
core.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
import os
import chess
import chess.pgn
from easygui import fileopenbox, filesavebox
class Database:
def __init__(self, file=""):
self.file = file
self.games = []
try:
with open(file, "r") as fin:
game = True
while game is not None:
self.games.append(game)
game = chess.pgn.read_game(fin)
self.games.pop(0)
except FileNotFoundError:
self.add()
def add(self, item=None):
if item is None:
item = chess.pgn.Game()
self.games.append(item)
def new_file(self):
fil = fileopenbox("Which file to open?", "WayChess", filetypes=("pgn",))
self.__init__(fil)
def save(self, file=None):
isfile = os.path.isfile
if (self.file is None and file is None) or (
not isfile(file) and not isfile(self.file)
):
file = filesavebox("Save to which file?", "WayChess", filetypes=("pgn",))
self.file = file
with open(file, "w+") as fout:
for game in self.games:
print(game, file=fout, end="\n\n")
def __len__(self):
return len(self.games)
def __iter__(self):
return iter(self.games)
def __getitem__(self, item):
return self.games[item]