Use a class for errors

This commit is contained in:
Mylloon 2023-05-29 11:09:40 +02:00
parent beb4f1ae54
commit 2a4026212d
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
2 changed files with 36 additions and 10 deletions

22
main.py
View file

@ -2,6 +2,8 @@ import re
import subprocess
import sys
from src.errors import ClangError
def clang_format(file_path):
command = ["clang-format", "--dry-run", file_path]
@ -21,7 +23,7 @@ def clang_format(file_path):
return output
def parse_clang_format_output(output):
def parse_clang_format_output(output) -> None | 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>.+)\]"
@ -37,12 +39,12 @@ def parse_clang_format_output(output):
warning_message = error_match.group("warning_message")
errors.append(
{
"filename": filename,
"line_number": line_number,
"column_number": column_number,
"warning_message": warning_message[1:],
}
ClangError(
filename,
line_number,
column_number,
warning_message[1:],
)
)
return errors
@ -57,8 +59,8 @@ if __name__ == "__main__":
if parsed_output:
for error in parsed_output:
print(
f"Warning dans {error['filename']} à la ligne {error['line_number']},"
+ f" caractère {error['column_number']} : {error['warning_message']}"
f"Avertissement dans {error.filename} à la ligne {error.line_number},"
+ f" caractère {error.column_number} : {error.warning_message}"
)
else:
print("No warnings found.")
print("Aucun avertissement trouvé.")

24
src/errors.py Normal file
View file

@ -0,0 +1,24 @@
class ClangError:
def __init__(
self, filename: str, line_number: int, column_number: int, warning_message: str
) -> None:
self._filename = filename
self._line_number = line_number
self._column_number = column_number
self._warning_message = warning_message
@property
def filename(self) -> str:
return self._filename
@property
def line_number(self) -> int:
return self._line_number
@property
def column_number(self) -> int:
return self._column_number
@property
def warning_message(self) -> str:
return self._warning_message