From edb1a506b61a6288b58edae3c800c12c79bd116e Mon Sep 17 00:00:00 2001 From: Emmanuel Nwafor Date: Sun, 19 Nov 2023 19:38:45 +0100 Subject: [PATCH] fix, feat: Raises error is python file has a Syntax error. Add proper Error logging for Syntax error in project files --- alxcheck/checks/python.py | 9 ++++++++- alxcheck/utils/error_logging.py | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/alxcheck/checks/python.py b/alxcheck/checks/python.py index b03b17b..7d07c95 100644 --- a/alxcheck/checks/python.py +++ b/alxcheck/checks/python.py @@ -6,6 +6,7 @@ print_no_function_docstring, print_no_class_docstring, print_check_docstrings, + print_error_parsing_file, ) @@ -35,8 +36,14 @@ def check_module_function_class_documentation(file_path): content = "" else: content = content.split(b"\n", 1)[1] - tree = ast.parse(content) + tree = None try: + tree = ast.parse(content) + except Exception: + print_error_parsing_file(file_path) + try: + if tree is None: + return for node in ast.walk(tree): # check module docstring if isinstance(node, ast.Module): diff --git a/alxcheck/utils/error_logging.py b/alxcheck/utils/error_logging.py index 61fbb00..1143b1f 100644 --- a/alxcheck/utils/error_logging.py +++ b/alxcheck/utils/error_logging.py @@ -72,3 +72,19 @@ def print_var_was_used(file_path): def print_check_docstrings(file_path): print(Fore.RED + f"Error: Check docstrings in {file_path}" + Fore.RESET) + + +def print_error_parsing_file(file_path): + import ast + + try: + with open(file_path, "r") as f: + ast.parse(f.read()) + except SyntaxError as syntax_error: + print( + Fore.RED + + f"SyntaxError\n\tFile: {file_path}\n\tLine: {syntax_error.lineno}\tMessage: {syntax_error.msg}" + + Fore.RESET + ) + except Exception as e: + print(Fore.RED + f"Error Parsing File:\n\t{type(e)}" + Fore.RESET)