133 lines
4.8 KiB
Python
133 lines
4.8 KiB
Python
from __future__ import annotations
|
||
|
||
import os
|
||
import re
|
||
import tkinter as tk
|
||
from collections import Counter
|
||
from dataclasses import dataclass
|
||
from pathlib import Path
|
||
from tkinter import messagebox, ttk
|
||
from typing import Callable
|
||
|
||
from copienator import EvaluationWorkspace
|
||
from copienator.platform import open_path, safe_filename
|
||
|
||
|
||
from copienator.return_identity import (RETURN_FOLDER_PATTERN, NameIssue, find_name_issues, original_copy_path, rename_return_copy)
|
||
|
||
|
||
class GivingNamesPanel(ttk.LabelFrame):
|
||
def __init__(
|
||
self,
|
||
master: tk.Misc,
|
||
evaluation: Path,
|
||
*,
|
||
opener: Callable[[Path], None] = open_path,
|
||
) -> None:
|
||
super().__init__(master, text="Vérification des noms", padding=7)
|
||
self.evaluation = evaluation
|
||
self.opener = opener
|
||
self.columnconfigure(0, weight=1)
|
||
|
||
actions = ttk.Frame(self)
|
||
actions.grid(row=0, column=0, sticky="w", pady=(0, 7))
|
||
ttk.Button(
|
||
actions,
|
||
text="Ouvrir A Rendre",
|
||
command=self.open_return_folder,
|
||
).pack(side="left")
|
||
ttk.Button(actions, text="Recharger", command=self.reload).pack(
|
||
side="left", padx=6
|
||
)
|
||
|
||
self.issue_frame = ttk.Frame(self)
|
||
self.issue_frame.grid(row=1, column=0, sticky="ew")
|
||
self.issue_frame.columnconfigure(1, weight=1)
|
||
self.reload()
|
||
|
||
def open_return_folder(self) -> None:
|
||
path = EvaluationWorkspace(self.evaluation).return_dir
|
||
if not path.is_dir():
|
||
messagebox.showerror(
|
||
"Dossier introuvable",
|
||
"Exécutez d’abord l’attribution des noms pour créer A Rendre.",
|
||
)
|
||
return
|
||
try:
|
||
self.opener(path)
|
||
except (OSError, RuntimeError) as exc:
|
||
messagebox.showerror("Ouverture impossible", str(exc))
|
||
|
||
def open_original(self, copy_id: str) -> None:
|
||
path = original_copy_path(self.evaluation, copy_id)
|
||
if path is None:
|
||
messagebox.showerror(
|
||
"Copie introuvable", f"Copie{copy_id}.pdf est introuvable."
|
||
)
|
||
return
|
||
try:
|
||
self.opener(path)
|
||
except (OSError, RuntimeError) as exc:
|
||
messagebox.showerror("Ouverture impossible", str(exc))
|
||
|
||
def rename_issue(self, issue: NameIssue, variable: tk.StringVar) -> None:
|
||
try:
|
||
destination = rename_return_copy(issue.folder, variable.get())
|
||
except (OSError, ValueError) as exc:
|
||
messagebox.showerror("Renommage impossible", str(exc))
|
||
return
|
||
self.reload()
|
||
messagebox.showinfo("Copie renommée", f"Nouveau dossier : {destination.name}")
|
||
|
||
def reload(self) -> None:
|
||
for child in self.issue_frame.winfo_children():
|
||
child.destroy()
|
||
issues = find_name_issues(self.evaluation)
|
||
if not issues:
|
||
return_dir = EvaluationWorkspace(self.evaluation).return_dir
|
||
text = (
|
||
"Aucun nom inconnu ou dupliqué."
|
||
if return_dir.is_dir()
|
||
else "A Rendre n’a pas encore été généré."
|
||
)
|
||
ttk.Label(self.issue_frame, text=text).grid(
|
||
row=0, column=0, columnspan=5, sticky="w"
|
||
)
|
||
return
|
||
|
||
ttk.Label(self.issue_frame, text="Copie / problème").grid(
|
||
row=0, column=0, sticky="w", padx=(0, 6)
|
||
)
|
||
ttk.Label(self.issue_frame, text="Nouveau nom").grid(
|
||
row=0, column=1, sticky="w"
|
||
)
|
||
ttk.Label(self.issue_frame, text="Nom attribué").grid(
|
||
row=0, column=4, sticky="w", padx=(12, 0)
|
||
)
|
||
for row, issue in enumerate(issues, start=1):
|
||
ttk.Label(
|
||
self.issue_frame,
|
||
text=f"Copie{issue.copy_id} — {', '.join(issue.reasons)}",
|
||
).grid(row=row, column=0, sticky="w", padx=(0, 6), pady=2)
|
||
variable = tk.StringVar()
|
||
ttk.Entry(self.issue_frame, textvariable=variable).grid(
|
||
row=row, column=1, sticky="ew", pady=2
|
||
)
|
||
original = original_copy_path(self.evaluation, issue.copy_id)
|
||
ttk.Button(
|
||
self.issue_frame,
|
||
text="Ouvrir l’originale",
|
||
command=lambda ident=issue.copy_id: self.open_original(ident),
|
||
state="normal" if original else "disabled",
|
||
).grid(row=row, column=2, padx=6, pady=2)
|
||
ttk.Button(
|
||
self.issue_frame,
|
||
text="Renommer",
|
||
command=lambda item=issue, value=variable: self.rename_issue(
|
||
item, value
|
||
),
|
||
).grid(row=row, column=3, pady=2)
|
||
ttk.Label(self.issue_frame, text=issue.name).grid(
|
||
row=row, column=4, sticky="w", padx=(12, 0), pady=2
|
||
)
|