From b26f4abefa08adcb5b1662e4b22d1ea8443a9b09 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Thu, 1 Jun 2023 11:20:53 +0200 Subject: [PATCH] turn the print into a gui --- gui.py | 58 ++++++++++++++++++++++++++++++++++++++++ main.py | 66 ++-------------------------------------------- src/clangformat.py | 49 ++++++++++++++++++++++++++++++++++ src/utils.py | 4 +++ 4 files changed, 113 insertions(+), 64 deletions(-) create mode 100644 gui.py create mode 100644 src/clangformat.py diff --git a/gui.py b/gui.py new file mode 100644 index 0000000..3946d56 --- /dev/null +++ b/gui.py @@ -0,0 +1,58 @@ +import sys +from tkinter import Frame, Label, Tk + +from src.clangformat import clang_format, parse_clang_format_output +from src.utils import analyze_args + + +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() + self.f.grid() + # self.change_pos(1280, 720) + self.parent.title(self.name) + self.parent.mainloop() + + def change_pos(self, x: int, y: int) -> None: + """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}") + + 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() + + def _analyse(self) -> None: + """Analyse les données""" + files = analyze_args(sys.argv[1:]) + 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): + 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) + else: + print(f"Aucun avertissement trouvé dans {file}.") diff --git a/main.py b/main.py index f3024cf..9240bd9 100644 --- a/main.py +++ b/main.py @@ -1,66 +1,4 @@ -import re -import subprocess -import sys - -from src.errors import ClangError -from src.utils import analyze_args - - -def clang_format(file_path) -> str: - command = ["clang-format", "--dry-run", file_path] - try: - output = subprocess.check_output(command, stderr=subprocess.STDOUT, text=True) - except subprocess.CalledProcessError as e: - output = e.output - except FileNotFoundError as e: - print(f"Commande non trouvé : {e.filename}", file=sys.stderr) - print( - "clang est requis pour utilisé ce programme, installé-le via " - f"votre gestionnaire de paquet : {e.filename}", - file=sys.stderr, - ) - exit(1) - - return output - - -def parse_clang_format_output(output) -> list[ClangError]: - error_pattern = ( - r"(?P.+):(?P\d+):(?P\d+):" - r" warning: code should be clang-formatted \[(?P.+)\]" - ) - error_matches = re.finditer(error_pattern, output) - - errors = [] - for error_match in error_matches: - filename = str(error_match.group("filename")) - line_number = int(error_match.group("line_number")) - column_number = int(error_match.group("column_number")) - warning_message = str(error_match.group("warning_message")) - - errors.append( - ClangError( - filename, - line_number, - column_number, - warning_message[1:], - ) - ) - - return errors - +from gui import GUI if __name__ == "__main__": - files = analyze_args(sys.argv[1:]) - for file in files: - clang_format_output = clang_format(file) - parsed_output = parse_clang_format_output(clang_format_output) - - if parsed_output: - for error in parsed_output: - print( - f"[{error.warning_message}] Avertissement dans {error.filename} " - f"à la ligne {error.line_number}, caractère {error.column_number}." - ) - else: - print(f"Aucun avertissement trouvé dans {file}.") + GUI().start() diff --git a/src/clangformat.py b/src/clangformat.py new file mode 100644 index 0000000..3ae79af --- /dev/null +++ b/src/clangformat.py @@ -0,0 +1,49 @@ +import re +import subprocess +import sys + +from src.errors import ClangError + + +def clang_format(file_path) -> str: + command = ["clang-format", "--dry-run", file_path] + try: + output = subprocess.check_output(command, stderr=subprocess.STDOUT, text=True) + except subprocess.CalledProcessError as e: + output = e.output + except FileNotFoundError as e: + print(f"Commande non trouvé : {e.filename}", file=sys.stderr) + print( + "clang est requis pour utilisé ce programme, installé-le via " + f"votre gestionnaire de paquet : {e.filename}", + file=sys.stderr, + ) + exit(1) + + return output + + +def parse_clang_format_output(output) -> list[ClangError]: + error_pattern = ( + r"(?P.+):(?P\d+):(?P\d+):" + r" warning: code should be clang-formatted \[(?P.+)\]" + ) + error_matches = re.finditer(error_pattern, output) + + errors = [] + for error_match in error_matches: + filename = str(error_match.group("filename")) + line_number = int(error_match.group("line_number")) + column_number = int(error_match.group("column_number")) + warning_message = str(error_match.group("warning_message")) + + errors.append( + ClangError( + filename, + line_number, + column_number, + warning_message[1:], + ) + ) + + return errors diff --git a/src/utils.py b/src/utils.py index f80246c..a925570 100644 --- a/src/utils.py +++ b/src/utils.py @@ -8,6 +8,10 @@ def analyze_args(args: list) -> list[str]: print("Trop d'arguments renseignés.", file=sys.stderr) exit(1) + if not len(args): + print("Aucun argument renseigné.") + exit(1) + if os.path.isdir(args[0]): args = iterate_over_directory(args[0]) else: