def load_test_pgn(self): for gameno in range(GAME_COUNT): game_model = pgnfile1.loadToModel(gameno) game_model.players = (TestPlayer("White"), TestPlayer("Black")) save(None, game_model) return load(None)
def test_database_save_load(self): """Testing database save-load""" game_model = pgnfile0.loadToModel(0) p0, p1 = pgnfile0.get_player_names(0) game_model.players = (TestPlayer(p0), TestPlayer(p1)) in_game = [] walk(game_model.boards[0].board, in_game, game_model) in_game = " ".join(in_game) in_bb_list = [board.board.friends for board in game_model.boards] save(None, game_model) db = load(None) result = self.engine.execute(db.select) db.games = result.fetchall() print("%s selected" % len(db.games)) game_model = db.loadToModel(0) out_game = [] walk(game_model.boards[0].board, out_game, game_model) out_game = " ".join(out_game) out_bb_list = [board.board.friends for board in game_model.boards] self.assertEqual(in_game, out_game) self.assertEqual(in_bb_list, out_bb_list)
def saveGamePGN(self, game): if game.practice_game: return True if conf.get("saveOwnGames", False) and not game.hasLocalPlayer(): return True filename = conf.get("autoSaveFormat", "pychess") filename = filename.replace("#n1", game.tags["White"]) filename = filename.replace("#n2", game.tags["Black"]) year, month, day = parseDateTag(game.tags["Date"]) year = '' if year is None else str(year) month = '' if month is None else str(month) day = '' if day is None else str(day) filename = filename.replace("#y", "%s" % year) filename = filename.replace("#m", "%s" % month) filename = filename.replace("#d", "%s" % day) pgn_path = conf.get("autoSavePath", os.path.expanduser("~")) + "/" + filename + ".pgn" append = True try: if not os.path.isfile(pgn_path): # create new file with open(pgn_path, "w"): pass base_offset = os.path.getsize(pgn_path) # save to .sqlite database_path = os.path.splitext(pgn_path)[0] + '.sqlite' database.save(database_path, game, base_offset) # save to .scout from pychess.Savers.pgn import scoutfish_path if scoutfish_path is not None: pgn_text = pgn.save(StringIO(), game) tmp = tempfile.NamedTemporaryFile(mode="w", delete=False) pgnfile = tmp.name with tmp.file as f: f.write(pgn_text) # create new .scout from pgnfile we are importing args = [scoutfish_path, "make", pgnfile, "%s" % base_offset] output = subprocess.check_output(args, stderr=subprocess.STDOUT) # append it to our existing one if output.decode().find(u"Processing...done") > 0: old_scout = os.path.splitext(pgn_path)[0] + '.scout' new_scout = os.path.splitext(pgnfile)[0] + '.scout' with open(old_scout, "ab") as file1, open(new_scout, "rb") as file2: file1.write(file2.read()) # TODO: do we realy want to update .bin ? It can be huge/slow! # save to .pgn game.save(pgn_path, pgn, append) return True except IOError: return False
def saveGamePGN(self, game): if conf.get("saveOwnGames") and not game.hasLocalPlayer(): return True filename = conf.get("autoSaveFormat") filename = filename.replace("#n1", game.tags["White"]) filename = filename.replace("#n2", game.tags["Black"]) year, month, day = parseDateTag(game.tags["Date"]) year = "" if year is None else str(year) month = "" if month is None else str(month) day = "" if day is None else str(day) filename = filename.replace("#y", "%s" % year) filename = filename.replace("#m", "%s" % month) filename = filename.replace("#d", "%s" % day) pgn_path = conf.get("autoSavePath") + "/" + filename + ".pgn" append = True try: if not os.path.isfile(pgn_path): # create new file with open(pgn_path, "w"): pass base_offset = os.path.getsize(pgn_path) # save to .sqlite database_path = os.path.splitext(pgn_path)[0] + ".sqlite" database.save(database_path, game, base_offset) # save to .scout from pychess.Savers.pgn import scoutfish_path if scoutfish_path is not None: pgn_text = pgn.save(StringIO(), game) tmp = tempfile.NamedTemporaryFile( mode="w", encoding="utf-8", delete=False ) pgnfile = tmp.name with tmp.file as f: f.write(pgn_text) # create new .scout from pgnfile we are importing args = [scoutfish_path, "make", pgnfile, "%s" % base_offset] output = subprocess.check_output(args, stderr=subprocess.STDOUT) # append it to our existing one if output.decode().find(u"Processing...done") > 0: old_scout = os.path.splitext(pgn_path)[0] + ".scout" new_scout = os.path.splitext(pgnfile)[0] + ".scout" with open(old_scout, "ab") as file1, open(new_scout, "rb") as file2: file1.write(file2.read()) # TODO: do we realy want to update .bin ? It can be huge/slow! # save to .pgn game.save(pgn_path, pgn, append) return True except IOError: return False
def test_databas(self): """Testing database save-load""" model = pgnfile.loadToModel(0) p0, p1 = pgnfile.get_player_names(0) model.players = (TestPlayer(p0), TestPlayer(p1)) in_game = [] walk(model.boards[0].board, in_game, model) in_game = " ".join(in_game) save(None, model) db = load(None) result = self.conn.execute(db.select) db.games = result.fetchall() print("%s selected" % len(db.games)) model = db.loadToModel(0) out_game = [] walk(model.boards[0].board, out_game, model) out_game = " ".join(out_game) self.assertEqual(in_game, out_game)
def saveGamePGN(self, game): if conf.get("saveOwnGames", False) and not game.hasLocalPlayer(): return True filename = conf.get("autoSaveFormat", "pychess") filename = filename.replace("#n1", game.tags["White"]) filename = filename.replace("#n2", game.tags["Black"]) filename = filename.replace("#y", "%s" % game.tags["Year"]) filename = filename.replace("#m", "%s" % game.tags["Month"]) filename = filename.replace("#d", "%s" % game.tags["Day"]) path = conf.get("autoSavePath", os.path.expanduser("~")) + \ "/" + filename + ".pgn" append = True try: if not os.path.isfile(path): # create new file with open(path, "w"): pass offset = os.path.getsize(path) database_path = os.path.splitext(path)[0] + '.sqlite' database.save(database_path, game, offset) # TODO: update .bin and .scout databases game.save(path, pgn, append) return True except IOError: return False