Load a single rule file. Returns: Rule object or None if file is invalid.
(file_path: str)
| 242 | |
| 243 | |
| 244 | def load_rule_file(file_path: str) -> Optional[Rule]: |
| 245 | """Load a single rule file. |
| 246 | |
| 247 | Returns: |
| 248 | Rule object or None if file is invalid. |
| 249 | """ |
| 250 | try: |
| 251 | with open(file_path, 'r') as f: |
| 252 | content = f.read() |
| 253 | |
| 254 | frontmatter, message = extract_frontmatter(content) |
| 255 | |
| 256 | if not frontmatter: |
| 257 | print(f"Warning: {file_path} missing YAML frontmatter (must start with ---)", file=sys.stderr) |
| 258 | return None |
| 259 | |
| 260 | rule = Rule.from_dict(frontmatter, message) |
| 261 | return rule |
| 262 | |
| 263 | except (IOError, OSError, PermissionError) as e: |
| 264 | print(f"Error: Cannot read {file_path}: {e}", file=sys.stderr) |
| 265 | return None |
| 266 | except (ValueError, KeyError, AttributeError, TypeError) as e: |
| 267 | print(f"Error: Malformed rule file {file_path}: {e}", file=sys.stderr) |
| 268 | return None |
| 269 | except UnicodeDecodeError as e: |
| 270 | print(f"Error: Invalid encoding in {file_path}: {e}", file=sys.stderr) |
| 271 | return None |
| 272 | except Exception as e: |
| 273 | print(f"Error: Unexpected error parsing {file_path} ({type(e).__name__}): {e}", file=sys.stderr) |
| 274 | return None |
| 275 | |
| 276 | |
| 277 | # For testing |
no test coverage detected