Create Rule from frontmatter dict and message body.
(cls, frontmatter: Dict[str, Any], message: str)
| 43 | |
| 44 | @classmethod |
| 45 | def from_dict(cls, frontmatter: Dict[str, Any], message: str) -> 'Rule': |
| 46 | """Create Rule from frontmatter dict and message body.""" |
| 47 | # Handle both simple pattern and complex conditions |
| 48 | conditions = [] |
| 49 | |
| 50 | # New style: explicit conditions list |
| 51 | if 'conditions' in frontmatter: |
| 52 | cond_list = frontmatter['conditions'] |
| 53 | if isinstance(cond_list, list): |
| 54 | conditions = [Condition.from_dict(c) for c in cond_list] |
| 55 | |
| 56 | # Legacy style: simple pattern field |
| 57 | simple_pattern = frontmatter.get('pattern') |
| 58 | if simple_pattern and not conditions: |
| 59 | # Convert simple pattern to condition |
| 60 | # Infer field from event |
| 61 | event = frontmatter.get('event', 'all') |
| 62 | if event == 'bash': |
| 63 | field = 'command' |
| 64 | elif event == 'file': |
| 65 | field = 'new_text' |
| 66 | else: |
| 67 | field = 'content' |
| 68 | |
| 69 | conditions = [Condition( |
| 70 | field=field, |
| 71 | operator='regex_match', |
| 72 | pattern=simple_pattern |
| 73 | )] |
| 74 | |
| 75 | return cls( |
| 76 | name=frontmatter.get('name', 'unnamed'), |
| 77 | enabled=frontmatter.get('enabled', True), |
| 78 | event=frontmatter.get('event', 'all'), |
| 79 | pattern=simple_pattern, |
| 80 | conditions=conditions, |
| 81 | action=frontmatter.get('action', 'warn'), |
| 82 | tool_matcher=frontmatter.get('tool_matcher'), |
| 83 | message=message.strip() |
| 84 | ) |
| 85 | |
| 86 | |
| 87 | def extract_frontmatter(content: str) -> tuple[Dict[str, Any], str]: |
no test coverage detected