-
Notifications
You must be signed in to change notification settings - Fork 0
/
inspect.py
61 lines (49 loc) · 1.56 KB
/
inspect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import os
import inspect
from typing import Callable
from types import FrameType
def module_filepath(filepath: str):
'''模块格式的路径,path.to.module.file'''
try:
filepath = os.path.relpath(filepath)
except:
pass
filepath = filepath.replace('\\', '/').replace(':/', '.')
filepath = filepath.replace('../', '?-.').replace('./', '')
return filepath.replace('/', '.').removesuffix('.py')
def _get_filename(frame: FrameType) -> str:
try:
file_name = frame.f_globals['__name__']
except:
file_name = module_filepath(frame.f_code.co_filename)
return file_name
def find_caller(depth: int = 1) -> tuple[str, str, int]:
'''
获取调用者信息
Args:
depth (int, optional): 调用深度. Defaults to 1.
Returns:
tuple[str, str, int]: (文件名, 调用者名称, 调用处行数)
'''
frame = inspect.currentframe()
if frame is None:
return 'unknown', 'unknown', 0
while depth > 0:
next_frame = frame.f_back
if next_frame is None:
break
frame = next_frame
if _get_filename(frame) != 'logging':
depth -= 1
code = frame.f_code
file_name = _get_filename(frame)
return file_name, code.co_name, frame.f_lineno
def wrapped_info(source: Callable | type):
'''
装饰器获取被装饰函数或类的信息
Args:
source (Callable | type): 被装饰函数或类
Returns:
tuple[str, str]: (文件名, 函数名)
'''
return source.__module__, source.__qualname__