linfo2335-programming-parad.../project-3/regex/detect_flaws.py

56 lines
1.8 KiB
Python

import os
from hardcodedlist import detect_hardcoded_list
from flaw_3 import detect_unused_loop_variable
from flaw_4 import detect_missplaced_return
from flaw_5 import detect_too_much_indent
from flaw_6 import detect_print_instead_of_return
# List of all regex rules
detectors = [
detect_hardcoded_list,
detect_unused_loop_variable,
detect_missplaced_return,
detect_too_much_indent,
detect_print_instead_of_return,
]
def process_code_files():
"""
Process code files in the "./code" directory and detect flaws using the provided detectors.
This function reads each code file in the "./code" directory, applies a set of regex rules
to identify flaws in the code, and writes the results to a CSV file named "result.csv".
"""
with open("result.csv", "w") as csv:
# Create headers
column_name = [detector.__name__ for detector in detectors]
csv.write(f"code;{';'.join(column_name)}\n")
# Loop through all files in "./code"
for path, _, files in os.walk("./code"):
for file in files:
fpath = os.path.join(path, file)
# Open the file and read the code
with open(fpath) as code_file:
result = [file]
code = code_file.read()
try:
for (
detector
) in detectors: # Loop through all regex and check flaws
match = detector(code)
result.append(str(match))
except Exception as e:
print(e)
# Write the output in the result file
csv.write(";".join(result) + "\n")
process_code_files()