def append_game(self, game_tree): # first remember the last position # of the current file # this is going to be the offfset for # the game that is written start_offset = self.get_end_offset() dos_newlines = False if "\r\n".encode() in open(self.filename, "rb").read(): dos_newlines = True # append the game with open(self.filename, 'a') as pgn: if (dos_newlines): print('\r\n\r\n', file=pgn) print(game_tree.root(), file=pgn, end='\r\n\r\n') else: print("\n\n", file=pgn) print(game_tree.root(), file=pgn, end="\n\n") self.checksum = crc32_from_file(self.filename) # create new entry self.entries.append(Entry(start_offset, game_tree.root().headers)) self.index_current_game = len(self.entries) - 1
def reload_if_necessary(self, mainWindow): print("MY FILENAME WHEN CALLED:"+self.filename) print(os.path.isfile(self.filename)) if not os.path.isfile(self.filename): self.filename = ad.user_data_dir(appname, appauthor) + "/mygames.pgn" self.index_current_game = None # if still path doesn't exist, we have to create # new default pgn from scratch print("my filename is now:"+self.filename) if(not os.path.isfile(self.filename)): self.create_new_pgn() else: crc = crc32_from_file(self.filename) print("crc caluclated: "+str(crc)) print(self.checksum) if not crc == self.checksum: try: self.init_from_pgn(mainWindow, mainWindow.trUtf8("Re-loading PGN File...")) except BaseException as e: print(e) print("EXECPTION SO CHANING FILENAME") self.filename = ad.user_data_dir(appname, appauthor) + "/mygames.pgn" self.index_current_game = None self.create_new_pgn() else: print("CHECKSUM EQUAL SO DOING NOTHING")
def append_game(self, game_tree): # first remember the last position # of the current file # this is going to be the offfset for # the game that is written start_offset = self.get_end_offset() dos_newlines = False if "\r\n".encode() in open(self.filename,"rb").read(): dos_newlines = True # append the game with open(self.filename, 'a') as pgn: if(dos_newlines): print('\r\n\r\n', file=pgn) print(game_tree.root(), file=pgn, end='\r\n\r\n') else: print("\n\n", file=pgn) print(game_tree.root(), file=pgn, end="\n\n") self.checksum = crc32_from_file(self.filename) # create new entry self.entries.append(Entry(start_offset,game_tree.root().headers)) self.index_current_game = len(self.entries) - 1
def create_new_pgn(self): filename = self.filename if not os.path.exists(os.path.dirname(filename)): os.makedirs(os.path.dirname(filename)) f = open(filename, 'wb') f.close() self.checksum = crc32_from_file(self.filename) self.filename = filename
def create_new_pgn(self): filename = self.filename if not os.path.exists(os.path.dirname(filename)): os.makedirs(os.path.dirname(filename)) f = open(filename, 'wb') #print(" ",file=f) f.close() self.checksum = crc32_from_file(self.filename) self.filename = filename
def init_from_pgn(self, mainWindow, msg): with open(self.filename) as pgn: size = os.path.getsize(self.filename) self.entries = [] pDialog = QProgressDialog(msg, None, 0, size, mainWindow) pDialog.show() pDialog.setWindowModality(PyQt4.QtCore.Qt.WindowModal) QApplication.processEvents() for offset, headers in chess.pgn.scan_headers(pgn): QApplication.processEvents() pDialog.setValue(offset) self.entries.append(Entry(offset, headers)) pDialog.close() self.checksum = crc32_from_file(self.filename)
def update_game(self, idx, game_tree): old_length = self.delete_game_for_update_at(idx) dos_newlines = False if "\r\n".encode() in open(self.filename,"rb").read(): dos_newlines = True if(dos_newlines): game_str = (str(game_tree.root())).replace('\n','\r\n') game_str = (game_str + '\r\n\r\n').encode('utf-8') else: game_str = (str(game_tree.root())+"\n\n").encode('utf-8') length = len(game_str) offset = self.entries[idx].pgn_offset # we can't mmap an empty file # the file is however empty, if the current # game was the only one in the database # just append it current_filesize = os.stat(self.filename).st_size print("current fs: "+str(current_filesize)) if current_filesize == 0: print("file is empty") self.entries = [] self.append_game(game_tree) else: pgn = open(self.filename, 'r+') m=mm.mmap(pgn.fileno(),0) size = len(m) new_size = size + length m.flush() m.close() pgn.seek(size) pgn.write('A'*length) pgn.flush() m=mm.mmap(pgn.fileno(),0) m.move(offset+length,offset,size-offset) m.seek(offset) m.write(game_str) m.flush() m.close() pgn.close() for i in range(idx+1, len(self.entries)): self.entries[i].pgn_offset += (length-old_length) self.checksum = crc32_from_file(self.filename) self.entries[idx] = Entry(offset, game_tree.root().headers)
def init_from_pgn(self, mainWindow, msg): print("loading from: "+self.filename) with open(self.filename) as pgn: size = os.path.getsize(self.filename) self.entries = [] pDialog = QProgressDialog(msg,None,0,size,mainWindow) pDialog.show() pDialog.setWindowModality(PyQt4.QtCore.Qt.WindowModal) QApplication.processEvents() for offset, headers in chess.pgn.scan_headers(pgn): print(headers) QApplication.processEvents() pDialog.setValue(offset) self.entries.append(Entry(offset,headers)) pDialog.close() self.checksum = crc32_from_file(self.filename)
def update_game(self, idx, game_tree): old_length = self.delete_game_for_update_at(idx) dos_newlines = False if "\r\n".encode() in open(self.filename, "rb").read(): dos_newlines = True if (dos_newlines): game_str = (str(game_tree.root())).replace('\n', '\r\n') game_str = (game_str + '\r\n\r\n').encode('utf-8') else: game_str = (str(game_tree.root()) + "\n\n").encode('utf-8') length = len(game_str) offset = self.entries[idx].pgn_offset # we can't mmap an empty file # the file is however empty, if the current # game was the only one in the database # just append it current_filesize = os.stat(self.filename).st_size if current_filesize == 0: self.entries = [] self.append_game(game_tree) else: pgn = open(self.filename, 'r+') m = mm.mmap(pgn.fileno(), 0) size = len(m) new_size = size + length m.flush() m.close() pgn.seek(size) pgn.write('A' * length) pgn.flush() m = mm.mmap(pgn.fileno(), 0) m.move(offset + length, offset, size - offset) m.seek(offset) m.write(game_str) m.flush() m.close() pgn.close() for i in range(idx + 1, len(self.entries)): self.entries[i].pgn_offset += (length - old_length) self.checksum = crc32_from_file(self.filename) self.entries[idx] = Entry(offset, game_tree.root().headers)
def delete_game_at(self,idx): start_offset = self.entries[idx].pgn_offset # if the last game is to be deleted, first get # the offset at the very end of the file stop_offset = None if(idx == len(self.entries) -1): stop_offset = self.get_end_offset() else: # just take the start of the next game as end stop_offset = self.entries[idx+1].pgn_offset length = stop_offset - start_offset # we can't mmap an empty file # the file is however empty, the # game to be deleted was the only one in the database # just delete it current_filesize = os.stat(self.filename).st_size print("current fs: "+str(current_filesize)) if current_filesize == 0: print("file is empty") pgn = open(self.filename, 'r+') pgn.close() else: pgn = open(self.filename, 'r+') m=mm.mmap(pgn.fileno(),0) size = len(m) new_size = size - length m.move(start_offset,stop_offset,size-stop_offset) m.flush() m.close() pgn.truncate(new_size) pgn.close() self.checksum = crc32_from_file(self.filename) for i in range(idx, len(self.entries)): self.entries[i].pgn_offset -= length del(self.entries[idx]) # if idx was the current open game, set current_idx to None if(idx == self.index_current_game): self.index_current_game = None
def delete_game_at(self, idx): start_offset = self.entries[idx].pgn_offset # if the last game is to be deleted, first get # the offset at the very end of the file stop_offset = None if (idx == len(self.entries) - 1): stop_offset = self.get_end_offset() else: # just take the start of the next game as end stop_offset = self.entries[idx + 1].pgn_offset length = stop_offset - start_offset # we can't mmap an empty file # the file is however empty, the # game to be deleted was the only one in the database # just delete it current_filesize = os.stat(self.filename).st_size if current_filesize == 0: pgn = open(self.filename, 'r+') pgn.close() else: pgn = open(self.filename, 'r+') m = mm.mmap(pgn.fileno(), 0) size = len(m) new_size = size - length m.move(start_offset, stop_offset, size - stop_offset) m.flush() m.close() pgn.truncate(new_size) pgn.close() self.checksum = crc32_from_file(self.filename) for i in range(idx, len(self.entries)): self.entries[i].pgn_offset -= length del (self.entries[idx]) # if idx was the current open game, set current_idx to None if (idx == self.index_current_game): self.index_current_game = None
def reload_if_necessary(self, mainWindow): if not os.path.isfile(self.filename): self.filename = ad.user_data_dir(appname, appauthor) + "/mygames.pgn" self.index_current_game = None # if still path doesn't exist, we have to create # new default pgn from scratch if (not os.path.isfile(self.filename)): self.create_new_pgn() else: crc = crc32_from_file(self.filename) if not crc == self.checksum: try: self.init_from_pgn( mainWindow, mainWindow.trUtf8("Re-loading PGN File...")) except BaseException as e: #print(e) self.filename = ad.user_data_dir( appname, appauthor) + "/mygames.pgn" self.index_current_game = None self.create_new_pgn() else: pass
def is_consistent(self): checksum = crc32_from_file(self.filename) if not checksum == self.checksum: return False else: return True