diff --git a/gui.py b/gui.py index 3946d56..208553c 100644 --- a/gui.py +++ b/gui.py @@ -1,8 +1,9 @@ import sys -from tkinter import Frame, Label, Tk +from tkinter import Button, Frame, Label, Tk +from tkinter.filedialog import askdirectory, askopenfile from src.clangformat import clang_format, parse_clang_format_output -from src.utils import analyze_args +from src.utils import analyze_args, exts_list class GUI: @@ -15,7 +16,6 @@ class GUI: def start(self) -> None: """Affiche la fenêtre""" self._main_screen() - self.f.grid() # self.change_pos(1280, 720) self.parent.title(self.name) self.parent.mainloop() @@ -31,28 +31,72 @@ class GUI: self.parent.geometry(f"{x}x{y}+{x_i}+{y_i}") + def _open_file(self): + 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) + + def _open_directory(self): + try: + chemin = askdirectory() + except AttributeError: + print("Impossible de trouver le dossier", file=sys.stderr) + exit(1) + else: + return self._analyse(2, chemin) + def _main_screen(self): """Écran principal""" Label( self.parent, text=f"{self.name} est une application qui permet d'utiliser " "plus facilement clang-format", - ).grid(column=0, row=0, columnspan=1) - self._analyse() + ).grid(column=0, row=0, rowspan=2) - def _analyse(self) -> None: + 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) + + def _analyse(self, start_row: int, path: str) -> None: """Analyse les données""" - files = analyze_args(sys.argv[1:]) + + self.f.destroy() + self.f = Frame(self.parent) + self.f.grid() + + files = analyze_args([path]) + idx = start_row for file in files: clang_format_output = clang_format(file) parsed_output = parse_clang_format_output(clang_format_output) if parsed_output: - for idx, error in enumerate(parsed_output): + for error in parsed_output: Label( self.parent, text=f"Avertissement dans {error.filename} " f"à la ligne {error.line_number}, caractère {error.column_number}.", - ).grid(column=0, row=idx + 1, columnspan=1) + ).grid(column=0, row=idx, columnspan=1) + idx += 1 else: - print(f"Aucun avertissement trouvé dans {file}.") + Label( + self.parent, + text=f"Aucun avertissement trouvé dans {file}.", + ).grid(column=0, row=idx, columnspan=1) + idx += 1 diff --git a/src/utils.py b/src/utils.py index a925570..fe761f9 100644 --- a/src/utils.py +++ b/src/utils.py @@ -1,6 +1,20 @@ import os import sys +exts_list = { + "CSharp": ["cs"], + "Java": ["java"], + "JavaScript": ["mjs", "js", "ts"], + "Json": ["json"], + "Objective-C": ["m", "mm"], + "Proto": ["proto", "protodevel"], + "TableGen": ["td"], + "TextProto": ["textpb", "pb.txt", "textproto", "asciipb"], + "Verilog": ["sv", "svh", "v", "vh"], + "C": ["c"], + "C++": ["cpp"], +} + def analyze_args(args: list) -> list[str]: """Renvoie la liste des fichiers""" @@ -33,29 +47,7 @@ def check_ext(filename: str) -> bool: supported_exts = tuple( map( lambda ext: "." + ext, - [ - "cs", - "java", - "mjs", - "js", - "ts", - "json", - "m", - "mm", - "proto", - "protodevel", - "td", - "textpb", - "pb.txt", - "textproto", - "asciipb", - "sv", - "svh", - "v", - "vh", - "c", - "cpp", - ], + [ext for exts in exts_list.values() for ext in exts], ) )