Add directory support + error handling

This commit is contained in:
Mylloon 2023-05-29 17:00:06 +02:00
parent ecf6289da3
commit f04e89cf3f
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
2 changed files with 68 additions and 29 deletions

61
main.py
View file

@ -1,9 +1,9 @@
import re
import subprocess
import sys
from typing import Union
from src.errors import ClangError
from src.utils import analyze_args
def clang_format(file_path):
@ -24,44 +24,47 @@ def clang_format(file_path):
return output
def parse_clang_format_output(output) -> Union[None, list[ClangError]]:
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)
if error_matches:
errors = []
for error_match in error_matches:
filename = error_match.group("filename")
line_number = int(error_match.group("line_number"))
column_number = int(error_match.group("column_number"))
warning_message = error_match.group("warning_message")
if not error_matches:
return []
errors.append(
ClangError(
filename,
line_number,
column_number,
warning_message[1:],
)
errors = []
for error_match in error_matches:
filename = error_match.group("filename")
line_number = int(error_match.group("line_number"))
column_number = int(error_match.group("column_number"))
warning_message = error_match.group("warning_message")
errors.append(
ClangError(
filename,
line_number,
column_number,
warning_message[1:],
)
)
return errors
return None
return errors
if __name__ == "__main__":
clang_format_output = clang_format("tests/1.c")
parsed_output = parse_clang_format_output(clang_format_output)
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"Avertissement dans {error.filename} à la ligne {error.line_number},"
f" caractère {error.column_number} : {error.warning_message}"
)
else:
print("Aucun avertissement trouvé.")
if parsed_output:
for error in parsed_output:
print(
f"Avertissement dans {error.filename} "
f"à la ligne {error.line_number}, "
f"caractère {error.column_number} : {error.warning_message}"
)
else:
print(f"Aucun avertissement trouvé dans {file}.")

36
src/utils.py Normal file
View file

@ -0,0 +1,36 @@
import os
import sys
def analyze_args(args: list):
"""Renvoie la liste des fichiers"""
if len(args) > 1:
print("Trop d'arguments renseignés.", file=sys.stderr)
exit(1)
if os.path.isdir(args[0]):
args = iterate_over_directory(args[0])
else:
if not list(filter(lambda file: os.path.exists(file), args)):
print("Le fichier n'existe pas.", file=sys.stderr)
exit(1)
files = list(filter(check_ext, args))
if not files:
print(f"Aucun fichiers supporté ({', '.join(args)}).", file=sys.stderr)
exit(1)
return files
def check_ext(filename: str):
"""Vérifie qu'un fichier est supporté en se basant sur son extension"""
supported_exts = ".c"
return filename.endswith(supported_exts)
def iterate_over_directory(dirname: str):
"""Trouve tout les fichiers d'un dossier"""
return next(os.walk(dirname))[2]