| 62 | self._finals: _t.Dict[str, _t.Any] = {} |
| 63 | |
| 64 | def add_hook( |
| 65 | self, |
| 66 | hook: str, |
| 67 | func: _t.Callable, |
| 68 | priority: _t.Optional[int] = None, |
| 69 | final: bool = False, |
| 70 | data: _t.Optional[HookDataType] = None, |
| 71 | ): |
| 72 | if final: |
| 73 | existing = self._finals.get(hook) |
| 74 | if existing: |
| 75 | raise HookError("Final hook already present") |
| 76 | self._finals[hook] = _Hook(func, final, data=data) |
| 77 | return |
| 78 | hks = self._ns.get(hook, []) |
| 79 | |
| 80 | p = priority or 0 |
| 81 | if not priority and len(hks): |
| 82 | # Take the minimum value and remove 1 to keep the order. |
| 83 | priority_min = min(h.priority for h in hks) |
| 84 | p = priority_min - 1 |
| 85 | |
| 86 | hks.append(_Hook(func, priority=p, data=data)) |
| 87 | self._ns[hook] = sorted(hks, reverse=True, key=lambda h: h.priority) |
| 88 | |
| 89 | def get_hooks(self, hook: str) -> _t.List[_Hook]: |
| 90 | final = self._finals.get(hook, None) |