MCPcopy Create free account
hub / github.com/anthropics/claude-code / Rule

Class Rule

plugins/hookify/core/config_loader.py:33–84  ·  view source on GitHub ↗

A hookify rule.

Source from the content-addressed store, hash-verified

31
32@dataclass
33class Rule:
34 """A hookify rule."""
35 name: str
36 enabled: bool
37 event: str # "bash", "file", "stop", "all", etc.
38 pattern: Optional[str] = None # Simple pattern (legacy)
39 conditions: List[Condition] = field(default_factory=list)
40 action: str = "warn" # "warn" or "block" (future)
41 tool_matcher: Optional[str] = None # Override tool matching
42 message: str = "" # Message body from markdown
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
87def extract_frontmatter(content: str) -> tuple[Dict[str, Any], str]:

Callers 1

rule_engine.pyFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected