获取事件响应器定义所在源码信息。 参数: depth: 调用栈深度
(depth: int = 0)
| 79 | |
| 80 | |
| 81 | def get_matcher_source(depth: int = 0) -> MatcherSource | None: |
| 82 | """获取事件响应器定义所在源码信息。 |
| 83 | |
| 84 | 参数: |
| 85 | depth: 调用栈深度 |
| 86 | """ |
| 87 | current_frame = inspect.currentframe() |
| 88 | if current_frame is None: |
| 89 | return None |
| 90 | |
| 91 | frame = current_frame |
| 92 | d = depth + 1 |
| 93 | while d > 0: |
| 94 | frame = frame.f_back |
| 95 | if frame is None: |
| 96 | raise ValueError("Depth out of range") |
| 97 | d -= 1 |
| 98 | |
| 99 | module_name = (module := inspect.getmodule(frame)) and module.__name__ |
| 100 | |
| 101 | # matcher defined when plugin loading |
| 102 | plugin: Plugin | None = _current_plugin.get() |
| 103 | # matcher defined when plugin running |
| 104 | if plugin is None and module_name: |
| 105 | plugin = get_plugin_by_module_name(module_name) |
| 106 | |
| 107 | return MatcherSource( |
| 108 | plugin_id=plugin and plugin.id_, |
| 109 | module_name=module_name, |
| 110 | lineno=frame.f_lineno, |
| 111 | ) |
| 112 | |
| 113 | |
| 114 | def on( |
no test coverage detected