Load all hookify rules from .claude directory. Args: event: Optional event filter ("bash", "file", "stop", etc.) Returns: List of enabled Rule objects matching the event.
(event: Optional[str] = None)
| 196 | |
| 197 | |
| 198 | def load_rules(event: Optional[str] = None) -> List[Rule]: |
| 199 | """Load all hookify rules from .claude directory. |
| 200 | |
| 201 | Args: |
| 202 | event: Optional event filter ("bash", "file", "stop", etc.) |
| 203 | |
| 204 | Returns: |
| 205 | List of enabled Rule objects matching the event. |
| 206 | """ |
| 207 | rules = [] |
| 208 | |
| 209 | # Find all hookify.*.local.md files |
| 210 | pattern = os.path.join('.claude', 'hookify.*.local.md') |
| 211 | files = glob.glob(pattern) |
| 212 | |
| 213 | for file_path in files: |
| 214 | try: |
| 215 | rule = load_rule_file(file_path) |
| 216 | if not rule: |
| 217 | continue |
| 218 | |
| 219 | # Filter by event if specified |
| 220 | if event: |
| 221 | if rule.event != 'all' and rule.event != event: |
| 222 | continue |
| 223 | |
| 224 | # Only include enabled rules |
| 225 | if rule.enabled: |
| 226 | rules.append(rule) |
| 227 | |
| 228 | except (IOError, OSError, PermissionError) as e: |
| 229 | # File I/O errors - log and continue |
| 230 | print(f"Warning: Failed to read {file_path}: {e}", file=sys.stderr) |
| 231 | continue |
| 232 | except (ValueError, KeyError, AttributeError, TypeError) as e: |
| 233 | # Parsing errors - log and continue |
| 234 | print(f"Warning: Failed to parse {file_path}: {e}", file=sys.stderr) |
| 235 | continue |
| 236 | except Exception as e: |
| 237 | # Unexpected errors - log with type details |
| 238 | print(f"Warning: Unexpected error loading {file_path} ({type(e).__name__}): {e}", file=sys.stderr) |
| 239 | continue |
| 240 | |
| 241 | return rules |
| 242 | |
| 243 | |
| 244 | def load_rule_file(file_path: str) -> Optional[Rule]: |
no test coverage detected