blob: fb7bfaddb000fb9798e67d755ee2e1818563cc76 (
plain)
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
|
import shutil
import tempfile
from .PBNBoard import PBNBoard
class PBNFile(object):
def __init__(self, filename):
self._filename = filename
self.output_file = None
self.boards = []
lines = []
with open(self._filename) as pbn_file:
contents = pbn_file.readlines()
for line in contents:
line = line.strip()
if not line:
if len(lines) > 0:
self.boards.append(PBNBoard(lines))
lines = []
else:
lines.append(line)
if len(lines) > 0:
self.boards.append(PBNBoard(lines))
if not self.boards[0].has_field('Event'):
self.boards[0].write_event('')
def write_board(self, board):
if self.output_file is None:
self.output_file = tempfile.NamedTemporaryFile(
mode='w', delete=False)
for field in board.fields:
self.output_file.write(field.raw_field + '\r\n')
self.output_file.write('\r\n')
def save(self):
if self.output_file is None:
raise IOError('No boards written to PBN file, unable to save it.')
tmp_path = self.output_file.name
self.output_file.close()
shutil.move(tmp_path, self._filename)
|