-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudit.py
More file actions
82 lines (68 loc) · 2.89 KB
/
Copy pathaudit.py
File metadata and controls
82 lines (68 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# audit.py
# CLI entry point for the MAS Accessibility Audit Toolkit.
# Wires together: input validation, HTML loading, all ten checks, and reporting.
import argparse
import importlib
import pkgutil
from bs4 import BeautifulSoup
import checks
import config
from utils.logger import masLog
from utils.fetcher import load_html
from reporter import generate_report
def run_audit(source: str) -> None:
"""Run the full accessibility audit pipeline on a URL or local file."""
masLog(f"Audit started — source: {source}")
print(f"\n{config.TOOL_NAME} v{config.TOOL_VERSION}")
print(f"Auditing: {source}")
print(config.REPORT_SEPARATOR)
# Step 1: Load HTML
html = load_html(source)
if html is None:
print("ERROR: Could not load the source. Check the log for details.")
masLog("Audit aborted — HTML could not be loaded", level="error")
return
# Step 2: Parse HTML
soup = BeautifulSoup(html, config.HTML_PARSER)
masLog("HTML parsed successfully")
# Step 3: Discover and run all check modules automatically.
# pkgutil.iter_modules walks the checks/ package directory.
# Any module that exposes a run() function is treated as a check.
# Adding a new check module to checks/ requires no changes here.
all_findings = []
for finder, module_name, _ in pkgutil.iter_modules(checks.__path__):
module_config = config.MODULES.get(module_name, {})
if not module_config.get("enabled", True):
masLog(f"Skipping disabled module: checks.{module_name}")
continue
module = importlib.import_module(f"checks.{module_name}")
if hasattr(module, "run"):
masLog(f"Running module: checks.{module_name}")
all_findings.extend(module.run(soup, source))
# Step 4: Print summary to terminal
print(f"Checks complete — {len(all_findings)} finding(s) found\n")
if all_findings:
for i, finding in enumerate(all_findings, start=1):
severity = finding.get("severity", "low")
prefix = "[!]" if severity in ("critical", "high", "medium") else "[>]"
print(f" {prefix} [{i}] {finding['check']} — WCAG {finding['wcag']} (Level {finding.get('level', 'MS')})")
print(f" {finding['message']}")
print()
# Step 5: Generate and save report
report_path = generate_report(source, all_findings)
print(config.REPORT_SEPARATOR)
print(f"Report saved to: {report_path}")
masLog(f"Audit complete — {len(all_findings)} finding(s)")
def main():
parser = argparse.ArgumentParser(
description=f"{config.TOOL_NAME} — WCAG 2.1 AA accessibility checker"
)
parser.add_argument(
"source",
metavar="URL_OR_FILE",
help="URL (https://example.com) or local HTML file path to audit"
)
args = parser.parse_args()
run_audit(args.source)
if __name__ == "__main__":
main()