Files
pocketsync/test_tkinter.py
T
2026-01-20 14:01:26 +01:00

199 lines
6.9 KiB
Python

import os
import json
import subprocess
import tkinter as tk
from tkinter import filedialog, messagebox
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
CONFIG_FILE = os.path.join(BASE_DIR, "config.json")
class DirectorySelectorApp:
def __init__(self, root):
self.root = root
self.root.title("Selector de Directorios")
self.root.geometry("600x600")
# Variables de rutas
self.path_esde_src = tk.StringVar()
self.path_roms_src = tk.StringVar()
self.path_esde_dst = tk.StringVar()
self.path_roms_dst = tk.StringVar()
# ---------------------------
# RUTAS DE ORIGEN
# ---------------------------
tk.Label(root, text="Rutas de origen", font=("Arial", 12, "bold")).pack(pady=(10, 0))
self.create_path_selector("Origen ES-DE:", self.path_esde_src)
self.create_path_selector("Origen ROMs:", self.path_roms_src, callback=self.update_directory_list)
# ---------------------------
# LISTA DE DIRECTORIOS
# ---------------------------
tk.Label(root, text="Directorios encontrados en ROMs:", font=("Arial", 11)).pack(pady=(15, 5))
self.listbox = tk.Listbox(root, selectmode=tk.MULTIPLE, height=15)
self.listbox.pack(fill="both", expand=True, padx=10)
# ---------------------------
# RUTAS DE DESTINO
# ---------------------------
tk.Label(root, text="Rutas de destino", font=("Arial", 12, "bold")).pack(pady=(15, 0))
self.create_path_selector("Destino ES-DE:", self.path_esde_dst)
self.create_path_selector("Destino ROMs:", self.path_roms_dst)
# ---------------------------
# BOTÓN ROBOCOPY
# ---------------------------
tk.Button(root, text="Robocopy", font=("Arial", 12, "bold"),
command=self.run_robocopy).pack(pady=15)
# Evento de cierre
self.root.protocol("WM_DELETE_WINDOW", self.on_close)
# Cargar configuración previa
self.load_config()
# ---------------------------------------------------------
# CREAR SELECTOR DE RUTA
# ---------------------------------------------------------
def create_path_selector(self, label_text, var, callback=None):
frame = tk.Frame(self.root)
frame.pack(fill="x", padx=10, pady=5)
tk.Label(frame, text=label_text, width=15, anchor="w").pack(side="left")
tk.Entry(frame, textvariable=var, width=50).pack(side="left", padx=5)
tk.Button(frame, text="Buscar", command=lambda: self.choose_path(var, callback)).pack(side="left")
def choose_path(self, var, callback):
path = filedialog.askdirectory()
if not path:
return
var.set(path)
if callback:
callback(path)
# ---------------------------------------------------------
# LISTA DE DIRECTORIOS
# ---------------------------------------------------------
def update_directory_list(self, path):
self.listbox.delete(0, tk.END)
if not os.path.isdir(path):
return
try:
dirs = sorted(
d for d in os.listdir(path)
if os.path.isdir(os.path.join(path, d))
)
for d in dirs:
self.listbox.insert(tk.END, d)
except Exception as e:
messagebox.showerror("Error", f"No se pudo leer la ruta:\n{e}")
# ---------------------------------------------------------
# EJECUTAR ROBOCOPY
# ---------------------------------------------------------
def run_robocopy(self):
selected = [self.listbox.get(i) for i in self.listbox.curselection()]
if not selected:
messagebox.showwarning("Aviso", "No has seleccionado ningún sistema.")
return
esde_src = self.path_esde_src.get()
roms_src = self.path_roms_src.get()
esde_dst = self.path_esde_dst.get()
roms_dst = self.path_roms_dst.get()
if not all([esde_src, roms_src, esde_dst, roms_dst]):
messagebox.showerror("Error", "Debes configurar todas las rutas antes de continuar.")
return
for system in selected:
# --- ROMs ---
src = os.path.join(roms_src, system)
dst = os.path.join(roms_dst, system)
self.launch_robocopy(src, dst)
# --- ES-DE gamelists ---
src = os.path.join(esde_src, "gamelists", system)
dst = os.path.join(esde_dst, "gamelists", system)
self.launch_robocopy(src, dst)
# --- ES-DE downloaded_media ---
src = os.path.join(esde_src, "downloaded_media", system)
dst = os.path.join(esde_dst, "downloaded_media", system)
self.launch_robocopy(src, dst)
messagebox.showinfo("Completado", "Robocopy finalizado.")
def launch_robocopy(self, src, dst):
if not os.path.isdir(src):
return # No existe, se ignora
os.makedirs(dst, exist_ok=True)
cmd = f'robocopy "{src}" "{dst}" /MIR'
# Abrir en consola visible
subprocess.Popen(["cmd", "/c", cmd])
# ---------------------------------------------------------
# PERSISTENCIA
# ---------------------------------------------------------
def save_config(self):
data = {
"esde_src": self.path_esde_src.get(),
"roms_src": self.path_roms_src.get(),
"esde_dst": self.path_esde_dst.get(),
"roms_dst": self.path_roms_dst.get(),
"selected": [self.listbox.get(i) for i in self.listbox.curselection()]
}
try:
with open(CONFIG_FILE, "w", encoding="utf-8") as f:
json.dump(data, f, indent=4)
except Exception as e:
messagebox.showerror("Error", f"No se pudo guardar la configuración:\n{e}")
def load_config(self):
if not os.path.exists(CONFIG_FILE):
return
try:
with open(CONFIG_FILE, "r", encoding="utf-8") as f:
data = json.load(f)
except Exception:
return
self.path_esde_src.set(data.get("esde_src", ""))
self.path_roms_src.set(data.get("roms_src", ""))
self.path_esde_dst.set(data.get("esde_dst", ""))
self.path_roms_dst.set(data.get("roms_dst", ""))
roms_path = self.path_roms_src.get()
if os.path.isdir(roms_path):
self.update_directory_list(roms_path)
selected = set(data.get("selected", []))
for i in range(self.listbox.size()):
if self.listbox.get(i) in selected:
self.listbox.selection_set(i)
# ---------------------------------------------------------
# CIERRE
# ---------------------------------------------------------
def on_close(self):
self.save_config()
self.root.destroy()
if __name__ == "__main__":
root = tk.Tk()
app = DirectorySelectorApp(root)
root.mainloop()