|
1 | | -import yaml |
| 1 | +"""Custom linting rules loaded from a .roast.yaml config file.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import logging |
2 | 6 | import re |
3 | | -import os |
4 | 7 | from dataclasses import dataclass |
5 | | -from typing import List, Optional |
| 8 | +from pathlib import Path |
| 9 | +from typing import Any |
| 10 | + |
| 11 | +import yaml |
| 12 | + |
| 13 | +LOGGER = logging.getLogger(__name__) |
6 | 14 |
|
7 | | -@dataclass |
| 15 | + |
| 16 | +@dataclass(slots=True) |
8 | 17 | class CustomRule: |
9 | 18 | name: str |
10 | 19 | pattern: str |
11 | 20 | severity: str |
12 | 21 | message: str |
13 | 22 | category: str = "Code Quality" |
14 | 23 |
|
15 | | -def load_custom_rules(config_path: str = ".roast.yaml") -> List[CustomRule]: |
| 24 | + |
| 25 | +def load_custom_rules(config_path: str | Path = ".roast.yaml") -> list[CustomRule]: |
| 26 | + """Load custom rules from a YAML config file. |
| 27 | +
|
| 28 | + Returns an empty list if the file does not exist, is invalid, or |
| 29 | + encounters an error. |
| 30 | + """ |
| 31 | + path = Path(config_path) |
| 32 | + if not path.exists(): |
| 33 | + return [] |
| 34 | + |
16 | 35 | try: |
17 | | - if not os.path.exists(config_path): |
18 | | - return [] |
19 | | - with open(config_path, "r") as f: |
20 | | - config = yaml.safe_load(f) |
21 | | - if not config or "rules" not in config: |
22 | | - return [] |
23 | | - |
24 | | - rules = [] |
25 | | - for r in config["rules"]: |
26 | | - rules.append(CustomRule( |
27 | | - name=r["name"], |
28 | | - pattern=r["pattern"], |
29 | | - severity=r.get("severity", "medium"), |
30 | | - message=r["message"], |
31 | | - category=r.get("category", "Code Quality") |
32 | | - )) |
33 | | - return rules |
34 | | - except Exception as e: |
35 | | - print(f"Error loading custom rules: {e}") |
| 36 | + with path.open("r", encoding="utf-8") as fh: |
| 37 | + config: dict[str, Any] | None = yaml.safe_load(fh) |
| 38 | + except (yaml.YAMLError, OSError) as exc: |
| 39 | + LOGGER.warning("Failed to read custom rules from %s: %s", path, exc) |
36 | 40 | return [] |
| 41 | + |
| 42 | + if not config or "rules" not in config: |
| 43 | + return [] |
| 44 | + |
| 45 | + rules: list[CustomRule] = [] |
| 46 | + for idx, rule in enumerate(config["rules"]): |
| 47 | + try: |
| 48 | + rules.append( |
| 49 | + CustomRule( |
| 50 | + name=rule["name"], |
| 51 | + pattern=rule["pattern"], |
| 52 | + severity=rule.get("severity", "medium"), |
| 53 | + message=rule["message"], |
| 54 | + category=rule.get("category", "Code Quality"), |
| 55 | + ) |
| 56 | + ) |
| 57 | + except KeyError as exc: |
| 58 | + LOGGER.warning( |
| 59 | + "Skipping malformed custom rule #%d in %s (missing key: %s)", |
| 60 | + idx + 1, |
| 61 | + path, |
| 62 | + exc, |
| 63 | + ) |
| 64 | + continue |
| 65 | + # Validate the regex pattern at load time rather than at scan time. |
| 66 | + try: |
| 67 | + re.compile(rule["pattern"]) |
| 68 | + except re.error as exc: |
| 69 | + LOGGER.warning( |
| 70 | + "Skipping custom rule %r in %s (invalid regex: %s)", |
| 71 | + rule.get("name", f"#{idx + 1}"), |
| 72 | + path, |
| 73 | + exc, |
| 74 | + ) |
| 75 | + rules.pop() # Remove the rule we just added |
| 76 | + continue |
| 77 | + |
| 78 | + return rules |
0 commit comments