turn the print into a gui

This commit is contained in:
Mylloon 2023-06-01 11:20:53 +02:00
parent 3510eb41ad
commit b26f4abefa
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
4 changed files with 113 additions and 64 deletions

58
gui.py Normal file
View file

@ -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}.")

66
main.py
View file

@ -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<filename>.+):(?P<line_number>\d+):(?P<column_number>\d+):"
r" warning: code should be clang-formatted \[(?P<warning_message>.+)\]"
)
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()

49
src/clangformat.py Normal file
View file

@ -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<filename>.+):(?P<line_number>\d+):(?P<column_number>\d+):"
r" warning: code should be clang-formatted \[(?P<warning_message>.+)\]"
)
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

View file

@ -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: