Open file / directory
This commit is contained in:
parent
b26f4abefa
commit
a27c0822fa
2 changed files with 69 additions and 33 deletions
64
gui.py
64
gui.py
|
@ -1,8 +1,9 @@
|
||||||
import sys
|
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.clangformat import clang_format, parse_clang_format_output
|
||||||
from src.utils import analyze_args
|
from src.utils import analyze_args, exts_list
|
||||||
|
|
||||||
|
|
||||||
class GUI:
|
class GUI:
|
||||||
|
@ -15,7 +16,6 @@ class GUI:
|
||||||
def start(self) -> None:
|
def start(self) -> None:
|
||||||
"""Affiche la fenêtre"""
|
"""Affiche la fenêtre"""
|
||||||
self._main_screen()
|
self._main_screen()
|
||||||
self.f.grid()
|
|
||||||
# self.change_pos(1280, 720)
|
# self.change_pos(1280, 720)
|
||||||
self.parent.title(self.name)
|
self.parent.title(self.name)
|
||||||
self.parent.mainloop()
|
self.parent.mainloop()
|
||||||
|
@ -31,28 +31,72 @@ class GUI:
|
||||||
|
|
||||||
self.parent.geometry(f"{x}x{y}+{x_i}+{y_i}")
|
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):
|
def _main_screen(self):
|
||||||
"""Écran principal"""
|
"""Écran principal"""
|
||||||
Label(
|
Label(
|
||||||
self.parent,
|
self.parent,
|
||||||
text=f"{self.name} est une application qui permet d'utiliser "
|
text=f"{self.name} est une application qui permet d'utiliser "
|
||||||
"plus facilement clang-format",
|
"plus facilement clang-format",
|
||||||
).grid(column=0, row=0, columnspan=1)
|
).grid(column=0, row=0, rowspan=2)
|
||||||
self._analyse()
|
|
||||||
|
|
||||||
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"""
|
"""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:
|
for file in files:
|
||||||
clang_format_output = clang_format(file)
|
clang_format_output = clang_format(file)
|
||||||
parsed_output = parse_clang_format_output(clang_format_output)
|
parsed_output = parse_clang_format_output(clang_format_output)
|
||||||
|
|
||||||
if parsed_output:
|
if parsed_output:
|
||||||
for idx, error in enumerate(parsed_output):
|
for error in parsed_output:
|
||||||
Label(
|
Label(
|
||||||
self.parent,
|
self.parent,
|
||||||
text=f"Avertissement dans {error.filename} "
|
text=f"Avertissement dans {error.filename} "
|
||||||
f"à la ligne {error.line_number}, caractère {error.column_number}.",
|
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:
|
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
|
||||||
|
|
38
src/utils.py
38
src/utils.py
|
@ -1,6 +1,20 @@
|
||||||
import os
|
import os
|
||||||
import sys
|
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]:
|
def analyze_args(args: list) -> list[str]:
|
||||||
"""Renvoie la liste des fichiers"""
|
"""Renvoie la liste des fichiers"""
|
||||||
|
@ -33,29 +47,7 @@ def check_ext(filename: str) -> bool:
|
||||||
supported_exts = tuple(
|
supported_exts = tuple(
|
||||||
map(
|
map(
|
||||||
lambda ext: "." + ext,
|
lambda ext: "." + ext,
|
||||||
[
|
[ext for exts in exts_list.values() for ext in exts],
|
||||||
"cs",
|
|
||||||
"java",
|
|
||||||
"mjs",
|
|
||||||
"js",
|
|
||||||
"ts",
|
|
||||||
"json",
|
|
||||||
"m",
|
|
||||||
"mm",
|
|
||||||
"proto",
|
|
||||||
"protodevel",
|
|
||||||
"td",
|
|
||||||
"textpb",
|
|
||||||
"pb.txt",
|
|
||||||
"textproto",
|
|
||||||
"asciipb",
|
|
||||||
"sv",
|
|
||||||
"svh",
|
|
||||||
"v",
|
|
||||||
"vh",
|
|
||||||
"c",
|
|
||||||
"cpp",
|
|
||||||
],
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Reference in a new issue