MCPcopy
hub / github.com/nonebot/nonebot2 / get_matcher_source

Function get_matcher_source

nonebot/plugin/on.py:81–111  ·  view source on GitHub ↗

获取事件响应器定义所在源码信息。 参数: depth: 调用栈深度

(depth: int = 0)

Source from the content-addressed store, hash-verified

79
80
81def 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
114def on(

Callers 3

get_matcher_pluginFunction · 0.85
get_matcher_moduleFunction · 0.85
onFunction · 0.85

Calls 3

MatcherSourceClass · 0.90
getMethod · 0.45

Tested by

no test coverage detected