This repository has been archived on 2023-06-07. You can view files and clone it, but cannot push or open issues or pull requests.
errsy/gui.py

121 lines
3.9 KiB
Python
Raw Normal View History

2023-06-01 11:20:53 +02:00
import sys
2023-06-01 12:49:10 +02:00
from tkinter import Button, Frame, Label, Tk, Toplevel
2023-06-01 11:57:50 +02:00
from tkinter.filedialog import askdirectory, askopenfile
2023-06-01 11:20:53 +02:00
from src.clangformat import clang_format, parse_clang_format_output
2023-06-01 11:57:50 +02:00
from src.utils import analyze_args, exts_list
2023-06-01 11:20:53 +02:00
class GUI:
def __init__(self) -> None:
self.name = "Errsy"
self.parent = Tk()
self.parent.resizable(False, False)
self.f = Frame(self.parent)
def start(self) -> None:
"""Affiche la fenêtre"""
self._main_screen()
2023-06-01 11:59:08 +02:00
# self._change_pos(1280, 720)
2023-06-01 11:20:53 +02:00
self.parent.title(self.name)
self.parent.mainloop()
2023-06-01 11:59:08 +02:00
def _change_pos(self, x: int, y: int) -> None:
2023-06-01 11:20:53 +02:00
"""Change les dimensions de la fenêtre"""
width = self.parent.winfo_screenwidth()
height = self.parent.winfo_screenheight()
# Centre
x_i = (width // 2) - (x // 2)
y_i = (height // 2) - (y // 2)
self.parent.geometry(f"{x}x{y}+{x_i}+{y_i}")
2023-06-01 11:59:08 +02:00
def _open_file(self) -> None:
2023-06-01 11:57:50 +02:00
try:
chemin = askopenfile(
title=f"Choisir un fichier - {self.name}",
filetypes=[("Tous les fichiers", "*")]
+ [
(langage, f"*.{ext}")
for langage, exts in exts_list.items()
for ext in exts
],
)
except AttributeError:
print("Impossible de trouver le fichier", file=sys.stderr)
exit(1)
else:
if chemin:
return self._analyse(2, chemin.name)
2023-06-01 11:59:08 +02:00
def _open_directory(self) -> None:
2023-06-01 11:57:50 +02:00
try:
chemin = askdirectory()
except AttributeError:
print("Impossible de trouver le dossier", file=sys.stderr)
exit(1)
else:
return self._analyse(2, chemin)
2023-06-01 11:59:08 +02:00
def _main_screen(self) -> None:
2023-06-01 11:20:53 +02:00
"""Écran principal"""
Label(
self.parent,
text=f"{self.name} est une application qui permet d'utiliser "
"plus facilement clang-format",
2023-06-01 11:57:50 +02:00
).grid(column=0, row=0, rowspan=2)
Button(self.parent, text="Ouvrir un fichier", command=self._open_file).grid(
column=1, row=0
)
Button(
self.parent, text="Ouvrir un dossier", command=self._open_directory
).grid(column=1, row=1)
2023-06-01 11:20:53 +02:00
2023-06-01 12:51:18 +02:00
def _show_info(self, name_err: str, msg: str) -> None:
title = f"{name_err} - {self.name}"
2023-06-01 12:49:10 +02:00
info = Toplevel()
info.resizable(False, False)
info.title(title)
Label(info, text=msg).pack()
width = max([len(line) for line in [title] + msg.splitlines()]) * 12
height = (3 + msg.count("\n")) * 10
info.geometry(f"{width}x{height}")
2023-06-01 11:57:50 +02:00
def _analyse(self, start_row: int, path: str) -> None:
2023-06-01 11:20:53 +02:00
"""Analyse les données"""
2023-06-01 11:57:50 +02:00
self.f.destroy()
self.f = Frame(self.parent)
self.f.grid()
files = analyze_args([path])
idx = start_row
2023-06-01 11:20:53 +02:00
for file in files:
clang_format_output = clang_format(file)
parsed_output = parse_clang_format_output(clang_format_output)
if parsed_output:
2023-06-01 11:57:50 +02:00
for error in parsed_output:
2023-06-01 12:49:10 +02:00
Button(
2023-06-01 11:20:53 +02:00
self.parent,
text=f"Avertissement dans {error.filename} "
2023-06-01 12:49:10 +02:00
f"à la ligne {error.line_number}, "
2023-06-01 11:58:03 +02:00
f"caractère {error.column_number}.",
2023-06-01 12:49:10 +02:00
borderwidth=0,
justify="left",
anchor="w",
2023-06-01 12:51:18 +02:00
command=lambda: self._show_info(
error.warning_message, error.warning_content
),
2023-06-01 12:49:10 +02:00
).grid(column=0, row=idx)
2023-06-01 11:57:50 +02:00
idx += 1
2023-06-01 11:20:53 +02:00
else:
2023-06-01 11:57:50 +02:00
Label(
self.parent,
text=f"Aucun avertissement trouvé dans {file}.",
2023-06-01 12:49:10 +02:00
).grid(column=0, row=idx)
2023-06-01 11:57:50 +02:00
idx += 1