def read_pgn(self, fichero_pgn): fichero_pgn = os.path.abspath(fichero_pgn) cfecha_pgn = str(os.path.getmtime(fichero_pgn)) cdir = self.configuracion.folder_databases_pgn() file_db = os.path.join(cdir, os.path.basename(fichero_pgn)[:-4] + ".lcdb") if Util.exist_file(file_db): create = False db = DBgames.DBgames(file_db) cfecha_pgn_ant = db.recuperaConfig("PGN_DATE") fichero_pgn_ant = db.recuperaConfig("PGN_FILE") db.close() if cfecha_pgn != cfecha_pgn_ant or fichero_pgn_ant != fichero_pgn: create = True Util.remove_file(file_db) else: create = True if create: db = DBgames.DBgames(file_db) dlTmp = QTVarios.ImportarFicheroPGN(self.main_window) dlTmp.show() db.leerPGNs([fichero_pgn], dlTmp=dlTmp) db.guardaConfig("PGN_DATE", cfecha_pgn) db.guardaConfig("PGN_FILE", fichero_pgn) db.close() dlTmp.close() self.database("R", file_db, temporary=True)
def tg_importar_PGN(self): path = QTVarios.select_pgn(self) if not path: return None dlTmp = QTVarios.ImportarFicheroPGN(self) dlTmp.show() self.dbGamesFEN.leerPGN(path, dlTmp) self.actualiza(True)
def tg_importar_PGN(self): files = QTVarios.select_pgns(self) if not files: return None dlTmp = QTVarios.ImportarFicheroPGN(self) dlTmp.show() self.dbGames.leerPGNs(files, dlTmp) self.actualiza(True) self.wsummary.reset()
def tw_importar_PGN(self): files = QTVarios.select_pgns(self) if not files: return None dlTmp = QTVarios.ImportarFicheroPGN(self) if self.dbGames.allows_duplicates: dlTmp.hide_duplicates() dlTmp.show() self.dbGames.leerPGNs(files, dlTmp) self.rehaz_columnas() self.actualiza(True) if self.wsummary: self.wsummary.reset()
def read_pgn(self, fichero_pgn): fichero_pgn = os.path.abspath(fichero_pgn) cfecha_pgn = str(os.path.getmtime(fichero_pgn)) path_temp_pgns = self.configuration.folder_databases_pgn() li = list(os.scandir(path_temp_pgns)) li_ant = [] for entry in li: if entry.name.endswith(".lcdb"): li_ant.append(entry) else: Util.remove_file(entry.path) if len(li_ant) > 10: li_ant.sort(key=lambda x: x.stat()[ST_ATIME], reverse=True) for x in li_ant[10:]: Util.remove_file(x.path) file_db = os.path.join(path_temp_pgns, os.path.basename(fichero_pgn)[:-3] + "lcdb") if Util.exist_file(file_db): create = False db = DBgames.DBgames(file_db) cfecha_pgn_ant = db.recuperaConfig("PGN_DATE") fichero_pgn_ant = db.recuperaConfig("PGN_FILE") db.close() if cfecha_pgn != cfecha_pgn_ant or fichero_pgn_ant != fichero_pgn: create = True Util.remove_file(file_db) else: create = True if create: db = DBgames.DBgames(file_db) dlTmp = QTVarios.ImportarFicheroPGN(self.main_window) dlTmp.show() db.import_pgns([fichero_pgn], dlTmp=dlTmp) db.save_config("PGN_DATE", cfecha_pgn) db.save_config("PGN_FILE", fichero_pgn) db.close() dlTmp.close() self.database("R", file_db, temporary=True)
def select_1_pgn(self, wparent=None): wparent = self.main_window if wparent is None else wparent path = QTVarios.select_pgn(wparent) if path: fichero_pgn = os.path.abspath(path) cfecha_pgn = str(os.path.getmtime(fichero_pgn)) cdir = self.configuration.folder_databases_pgn() file_db = os.path.join( cdir, os.path.basename(fichero_pgn)[:-4] + ".lcdb") if Util.exist_file(file_db): create = False db = DBgames.DBgames(file_db) cfecha_pgn_ant = db.recuperaConfig("PGN_DATE") fichero_pgn_ant = db.recuperaConfig("PGN_FILE") db.close() if cfecha_pgn != cfecha_pgn_ant or fichero_pgn_ant != fichero_pgn: create = True Util.remove_file(file_db) else: create = True if create: db = DBgames.DBgames(file_db) dlTmp = QTVarios.ImportarFicheroPGN(wparent) dlTmp.show() db.import_pgns([fichero_pgn], dlTmp=dlTmp) db.save_config("PGN_DATE", cfecha_pgn) db.save_config("PGN_FILE", fichero_pgn) db.close() dlTmp.close() w = WindowDatabase.WBDatabase(self.main_window, self, file_db, True, True) if w.exec_(): return w.game return None
def importarPGN(self, owner, partidabase, ficheroPGN, maxDepth, variations): erroneos = duplicados = importados = 0 dlTmp = QTVarios.ImportarFicheroPGN(owner) dlTmp.hideDuplicados() dlTmp.show() self.saveHistory(_("Import"), _("PGN with variants"), os.path.basename(ficheroPGN)) cursor = self._conexion.cursor() base = partidabase.pv() if partidabase else self.getconfig("BASEPV") sql_insert = "INSERT INTO LINES( XPV ) VALUES( ? )" sql_update = "UPDATE LINES SET XPV=? WHERE XPV=?" for n, g in enumerate(PGNreader.readGames(ficheroPGN), 1): if not dlTmp.actualiza(n, erroneos, duplicados, importados): break if g.erroneo: erroneos += 1 continue if not g.moves: erroneos += 1 continue def haz_partida(liMoves): njg = len(liMoves) if njg > maxDepth: liMoves = liMoves[:maxDepth] pv = " ".join([move.pv for move in liMoves]) partida = Partida.Partida() partida.leerPV(pv) pv = partida.pv() if base and not pv.startswith(base): return xpv = LCEngine.pv2xpv(pv) updated = False for npos, xpv_ant in enumerate(self.li_xpv): if xpv_ant.startswith(xpv): return if xpv.startswith(xpv_ant): cursor.execute(sql_update, (xpv, xpv_ant)) self.li_xpv[npos] = xpv updated = True break if not updated: cursor.execute(sql_insert, (xpv,)) self.li_xpv.append(xpv) if variations != "N": #None for njug, move in enumerate(liMoves): ok = True if variations != "A": if variations == "W": if njug % 2 == 1: ok = False elif variations == "B": if njug % 2 == 0: ok = False if ok: for lim in move.variantes: limovnv = [liMoves[j].clona() for j in range(njug)] for move in limovnv: move.variantes = [] limovnv.extend(lim.liMoves) haz_partida(limovnv) haz_partida(g.moves.liMoves) if n % 50: self._conexion.commit() cursor.close() self.li_xpv.sort() self._conexion.commit() dlTmp.actualiza(n, erroneos, duplicados, importados) dlTmp.ponContinuar()
def importarPGN(self, owner, partidabase, ficheroPGN, maxDepth): erroneos = duplicados = importados = 0 dlTmp = QTVarios.ImportarFicheroPGN(owner) dlTmp.hideDuplicados() dlTmp.show() cursor = self._conexion.cursor() base = self.getconfig("BASEPV") njugbase = partidabase.numJugadas() n = 0 sql_insert = "INSERT INTO LINES( XPV, LINE ) VALUES( ?, ? )" sql_update = "UPDATE LINES SET XPV=?, LINE=? WHERE XPV=?" for n, g in enumerate(PGNreader.readGames(ficheroPGN), 1): if not dlTmp.actualiza(n, erroneos, duplicados, importados): break if g.erroneo: erroneos += 1 continue if not g.moves: erroneos += 1 continue def haz_partida(partida, liMoves): njg = len(partida) if len(liMoves) + njg > maxDepth: liMoves = liMoves[:maxDepth - njg] pv = " ".join([move.pv for move in liMoves]) partida.leerPV(pv) pv = partida.pv() if base and not pv.startswith(base) or partida.numJugadas( ) <= njugbase: return xpv = LCEngine.pv2xpv(pv) if xpv in self.li_xpv: return line_blob = partida.save2blob() updated = False for npos, xpv_ant in enumerate(self.li_xpv): if xpv.startswith(xpv_ant): cursor.execute(sql_update, (xpv, line_blob, xpv_ant)) self.li_xpv[npos] = xpv updated = True break if not updated: cursor.execute(sql_insert, (xpv, line_blob)) self.li_xpv.append(xpv) for njug, move in enumerate(liMoves): if move.variantes: for lim in move.variantes: p = partida.copia( njug - 1) if njug > 0 else Partida.Partida() haz_partida(p, lim.liMoves) partida = Partida.Partida() haz_partida(partida, g.moves.liMoves) if n % 50: self._conexion.commit() cursor.close() self.li_xpv.sort() self._conexion.commit() dlTmp.actualiza(n, erroneos, duplicados, importados) dlTmp.ponContinuar()
def grabarPGN(self, ventana, ficheroPGN, maxDepth): select = "XPV,PV,COMMENT,NAG,ADV,FEN,POS" SQLDBF.DBF(self.conexion, self.tablaDatos, select) erroneos = duplicados = importados = 0 dlTmp = QTVarios.ImportarFicheroPGN(ventana) dlTmp.hideDuplicados() dlTmp.show() select = "XPV,PV,COMMENT,NAG,ADV,FEN,POS" dbf = SQLDBF.DBF(self.conexion, self.tablaDatos, select) dnag = {"!!": 3, "!": 1, "?": 2, "??": 4, "!?": 5, "?!": 6} for n, g in enumerate(PGNreader.readGames(ficheroPGN)): if not dlTmp.actualiza(n + 1, erroneos, duplicados, importados): break if g.erroneo: erroneos += 1 continue if not g.moves: erroneos += 1 continue liReg = [] def addMoves(moves, depth, seq): for mv in moves.liMoves: if depth > maxDepth: break seqM1 = seq pv = mv.pv seq += LCEngine.pv2xpv(pv) reg = SQLDBF.Almacen() reg.PV = pv reg.XPV = seq reg.COMMENT = "\n".join(mv.comentarios) reg.FEN = mv.fen reg.NAG = 0 reg.ADV = 0 self.ultPos += 1 reg.POS = self.ultPos for critica in mv.criticas: if critica.isdigit(): t = int(critica) if t in (4, 2, 1, 3, 5, 6): reg.NAG = t elif t in (11, 14, 15, 16, 17, 18, 19): reg.ADV = t else: if critica in dnag: reg.NAG = dnag[critica] liReg.append(reg) if mv.variantes: for variante in mv.variantes: addMoves(variante, depth, seqM1) depth += 1 addMoves(g.moves, 1, "") if liReg: dbf.insertarLista(liReg, None) dbf.cerrar() dlTmp.actualiza(n, erroneos, duplicados, importados) dlTmp.ponContinuar() return len(liReg) > 0
def mas(self): path_pgn = QTVarios.select_pgn(self) if not path_pgn: return path_db = self.configuration.ficheroTemporal("lcdb") db = DBgames.DBgames(path_db) dlTmp = QTVarios.ImportarFicheroPGN(self) dlTmp.show() db.import_pgns([path_pgn], dlTmp=dlTmp) db.close() dlTmp.close() db = DBgames.DBgames(path_db) nreccount = db.all_reccount() if nreccount == 0: return plant = "" shuffle = False reverse = False todos = range(1, nreccount + 1) li_regs = [] max_moves = 0 while True: sep = FormLayout.separador liGen = [] liGen.append((None, "%s: %d" % (_("Total games"), nreccount))) liGen.append(sep) config = FormLayout.Editbox(_("Select games") + "<br>" + _("By example:") + " -5,7-9,14,19-" + "<br>" + _("Empty means all games"), rx="[0-9,\-,\,]*") liGen.append((config, plant)) liGen.append(sep) liGen.append((_("Shuffle") + ":", shuffle)) liGen.append(sep) liGen.append((_("Reverse") + ":", reverse)) liGen.append(sep) config = FormLayout.Spinbox(_("Max moves"), 0, 999, 50) liGen.append((config, 0)) resultado = FormLayout.fedit(liGen, title=_("Select games"), parent=self, anchoMinimo=200, icon=Iconos.Opciones()) if resultado: accion, liResp = resultado plant, shuffle, reverse, max_moves = liResp if plant: ln = Util.ListaNumerosImpresion(plant) li_regs = ln.selected(todos) else: li_regs = todos nregs = len(li_regs) if 12 <= nregs <= 500: break else: QTUtil2.message_error( self, "%s (%d)" % (_("Number of games must be in range 12-500"), nregs)) li_regs = None else: break if li_regs: if shuffle: random.shuffle(li_regs) if reverse: li_regs.sort(reverse=True) li_regs = [x - 1 for x in li_regs] # 0 init dic = {} dic["TOURNEY"] = os.path.basename(path_pgn)[:-4] games = dic["GAMES"] = [] for recno in li_regs: g = db.read_game_recno(recno) pv = g.pv() if max_moves: lipv = pv.strip().split(" ") if len(lipv) > max_moves: pv = " ".join(lipv[:max_moves]) dt = {"LABELS": g.li_tags, "XPV": FasterCode.pv_xpv(pv)} games.append(dt) self.litourneys.append(dic) li = [("%s (%d)" % (tourney["TOURNEY"], len(tourney["GAMES"])), tourney) for tourney in self.litourneys] self.cbtourney.rehacer(li, dic) db.close()