Spaces:
Sleeping
Sleeping
Update mew_log/attr_utils.py
Browse files- mew_log/attr_utils.py +260 -260
mew_log/attr_utils.py
CHANGED
|
@@ -1,260 +1,260 @@
|
|
| 1 |
-
from
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
def get_type_name(attr):
|
| 5 |
-
return type(attr).__name__
|
| 6 |
-
|
| 7 |
-
def is_complex_type(obj):
|
| 8 |
-
return is_of_type(variable, (list, set, dict)) or hasattr(variable, '__dict__')
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
def is_bytes_like_type(obj, bytes_like_types=(memoryview, bytes, bytearray), raise_err=False):
|
| 12 |
-
try:
|
| 13 |
-
return is_of_type(obj, bytes_like_types)
|
| 14 |
-
except TypeError as e:
|
| 15 |
-
if raise_err:
|
| 16 |
-
raise TypeError(f"Provided object does not match the provided types: {bytes_like_types}")
|
| 17 |
-
return False
|
| 18 |
-
|
| 19 |
-
def is_of_type(obj, types, raise_err=False):
|
| 20 |
-
if isinstance(obj, (types)):
|
| 21 |
-
return True
|
| 22 |
-
elif raise_err:
|
| 23 |
-
raise TypeError(f"Provided object does not match the provided types: {tuple(types)}")
|
| 24 |
-
return False
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
def get_partial_argspec(method):
|
| 28 |
-
if not callable(method):
|
| 29 |
-
return None # Not a callable object
|
| 30 |
-
try:
|
| 31 |
-
full_argspec = inspect.getfullargspec(method)
|
| 32 |
-
return full_argspec
|
| 33 |
-
except TypeError:
|
| 34 |
-
# Fallback to using inspect.signature
|
| 35 |
-
signature = get_signature(method)
|
| 36 |
-
if signature:
|
| 37 |
-
parameters = signature.parameters
|
| 38 |
-
args = [param for param in parameters if parameters[param].default == parameters[param].empty]
|
| 39 |
-
varargs = signature.varargs
|
| 40 |
-
varkw = signature.varkw
|
| 41 |
-
defaults = [parameters[param].default for param in args]
|
| 42 |
-
return inspect.FullArgSpec(args, varargs, varkw, defaults)
|
| 43 |
-
|
| 44 |
-
def get_signature(method):
|
| 45 |
-
try:
|
| 46 |
-
signature = inspect.signature(method)
|
| 47 |
-
return signature
|
| 48 |
-
except (TypeError, ValueError):
|
| 49 |
-
return None
|
| 50 |
-
|
| 51 |
-
def get_method_args(method):
|
| 52 |
-
|
| 53 |
-
full_arg_spec = get_partial_argspec(method)
|
| 54 |
-
if full_arg_spec:
|
| 55 |
-
args = [arg for arg in full_arg_spec.args if
|
| 56 |
-
getattr(method, arg, None) is not None and getattr(method, arg, "") != ""]
|
| 57 |
-
kwargs = {key: getattr(method, key, None) for key in full_arg_spec.kwonlyargs}
|
| 58 |
-
kwargs_defaults = {key: value for key, value in
|
| 59 |
-
zip(full_arg_spec.kwonlyargs, full_arg_spec.kwonlydefaults or ())}
|
| 60 |
-
args.extend(f"{key}={value}" for key, value in kwargs.items() if value is not None and value != "")
|
| 61 |
-
return args
|
| 62 |
-
return None
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
def get_source_info(attr):
|
| 66 |
-
try:
|
| 67 |
-
source_lines, line_number = inspect.getsourcelines(attr)
|
| 68 |
-
source_file_path = inspect.getsourcefile(attr)
|
| 69 |
-
source_file = os.path.relpath(source_file_path)
|
| 70 |
-
return f"/{source_file}::{line_number}"
|
| 71 |
-
except Exception as e:
|
| 72 |
-
return "Source info not available!"
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
def get_method_info(method):
|
| 76 |
-
args = get_method_args(method)
|
| 77 |
-
args_str = ", ".join(args) if args else ''
|
| 78 |
-
signature = get_signature(method)
|
| 79 |
-
return_str = f' -> {signature.return_annotation}' if signature and signature.return_annotation is not inspect.Signature.empty else ''
|
| 80 |
-
|
| 81 |
-
try:
|
| 82 |
-
source_info = get_source_info(method)
|
| 83 |
-
except Exception as e:
|
| 84 |
-
raise Exception("Source info not available!", e)
|
| 85 |
-
|
| 86 |
-
# Construct the file:// URL with line number for the method if available
|
| 87 |
-
method_file_url = f"file://{inspect.getsourcefile(method)}#L{inspect.getsourcelines(method)[1]}"
|
| 88 |
-
method_link = ansi_link_str(method_file_url, "Source")
|
| 89 |
-
|
| 90 |
-
# Include the link in the method signature string
|
| 91 |
-
method_signature = f"{signature}{return_str}: {method_link}\n-->{source_info}"
|
| 92 |
-
|
| 93 |
-
return method_signature
|
| 94 |
-
def get_var_value(variable):
|
| 95 |
-
return f'{str(variable)}' if not is_of_type(variable, (list, set, dict)) or hasattr(variable, '__dict__') else '...'
|
| 96 |
-
|
| 97 |
-
def get_variable_info(variable):
|
| 98 |
-
return f"<{ get_type_name(variable) }>: { get_var_value(variable) }"
|
| 99 |
-
|
| 100 |
-
def list_class_attributes(cls, verbose=True, use_color=False):
|
| 101 |
-
def format_str(s, fg=None):
|
| 102 |
-
return ansi_color_str(s, fg=fg) if use_color else s
|
| 103 |
-
|
| 104 |
-
# Determine whether cls is a class or an instance of a class
|
| 105 |
-
if inspect.isclass(cls):
|
| 106 |
-
class_name = cls.__name__
|
| 107 |
-
else:
|
| 108 |
-
class_name = cls.__class__.__name__
|
| 109 |
-
|
| 110 |
-
variables = [
|
| 111 |
-
f'{attribute}{get_variable_info(getattr(cls, attribute))}' if verbose else f'{attribute}<{get_type_name(getattr(cls, attribute))}>'
|
| 112 |
-
for attribute in dir(cls) if not attribute.startswith('__') and not callable(getattr(cls, attribute))]
|
| 113 |
-
methods = [f'{attribute}{get_method_info(getattr(cls, attribute))}' if verbose else f'{attribute}<method>' for
|
| 114 |
-
attribute in dir(cls) if not attribute.startswith('__') and callable(getattr(cls, attribute))]
|
| 115 |
-
|
| 116 |
-
variables_str = '\n'.join([f' - {format_str(var, fg="green")}' for var in variables])
|
| 117 |
-
methods_str = '\n'.join([f' - {format_str(method, fg="blue")}' for method in methods])
|
| 118 |
-
|
| 119 |
-
cls_name = format_str(class_name, fg="cyan") if use_color else class_name
|
| 120 |
-
return f'===list_class_attributes of: {cls_name}:\n===<variables>===\n{variables_str}\n===<methods>===\n{methods_str}'
|
| 121 |
-
|
| 122 |
-
def get_class_attributes(cls, verbose=True, use_color=True):
|
| 123 |
-
def format_str(s, fg=None):
|
| 124 |
-
return ansi_color_str(s, fg=fg) if use_color else s
|
| 125 |
-
|
| 126 |
-
attributes_dict = {'variables': {}, 'methods': {}}
|
| 127 |
-
|
| 128 |
-
for attribute_v in vars(cls):
|
| 129 |
-
if not attribute_v.startswith('__') and 'stat' not in attribute_v:
|
| 130 |
-
attr = getattr(cls, attribute_v)
|
| 131 |
-
if not callable(attr):
|
| 132 |
-
attr_info = get_variable_info(attr) if verbose else ''
|
| 133 |
-
formatted_key = format_str(attribute_v, fg="green")
|
| 134 |
-
formatted_value = format_str(attr_info, fg="cyan") if verbose else ''
|
| 135 |
-
attributes_dict['variables'][attribute_v] = f'\n ~ {formatted_key}{formatted_value}'
|
| 136 |
-
|
| 137 |
-
for attribute in dir(cls):
|
| 138 |
-
if not attribute.startswith('__'):
|
| 139 |
-
attr = getattr(cls, attribute)
|
| 140 |
-
if callable(attr):
|
| 141 |
-
method_info = get_method_info(attr) if verbose else ''
|
| 142 |
-
formatted_key = format_str(attribute, fg="blue")
|
| 143 |
-
formatted_value = format_str(method_info, fg="cyan") if verbose else ''
|
| 144 |
-
attributes_dict['methods'][attribute] = f'\n ~ {formatted_key}{formatted_value}'
|
| 145 |
-
|
| 146 |
-
return attributes_dict
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
import threading
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
def get_thread_info(message='', use_color=True):
|
| 153 |
-
current_thread = threading.current_thread()
|
| 154 |
-
current_thread_name = current_thread.name
|
| 155 |
-
current_thread_id = current_thread.ident
|
| 156 |
-
current_thread_alive = current_thread.is_alive()
|
| 157 |
-
|
| 158 |
-
# Construct the colored thread info
|
| 159 |
-
thread_info = f'thread:{current_thread_name}::{current_thread_id}::{current_thread_alive}'
|
| 160 |
-
if use_color:
|
| 161 |
-
thread_info = ansi_color_str(thread_info,fg='yellow', bg='bright_yellow')
|
| 162 |
-
|
| 163 |
-
formatted_message = f'{thread_info}:{message}'
|
| 164 |
-
|
| 165 |
-
return formatted_message
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
import inspect
|
| 169 |
-
import os
|
| 170 |
-
|
| 171 |
-
# Get the current frame
|
| 172 |
-
def get_prev_frame(steps=1):
|
| 173 |
-
curr_frame = inspect.currentframe()
|
| 174 |
-
|
| 175 |
-
# Traverse back the specified number of steps in the call stack
|
| 176 |
-
for _ in range(steps):
|
| 177 |
-
if curr_frame is not None:
|
| 178 |
-
curr_frame = curr_frame.f_back
|
| 179 |
-
|
| 180 |
-
if curr_frame is None:
|
| 181 |
-
return None
|
| 182 |
-
|
| 183 |
-
return curr_frame
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
def get_prev_frame_from_frame(frame, steps=1):
|
| 187 |
-
curr_frame = frame
|
| 188 |
-
|
| 189 |
-
# Traverse back the specified number of steps in the call stack
|
| 190 |
-
for _ in range(steps):
|
| 191 |
-
if frame is not None:
|
| 192 |
-
curr_frame = curr_frame.f_back
|
| 193 |
-
|
| 194 |
-
if curr_frame is None:
|
| 195 |
-
return None
|
| 196 |
-
|
| 197 |
-
return curr_frame
|
| 198 |
-
|
| 199 |
-
def get_prev_caller_info(message='', use_color=True, steps=99):
|
| 200 |
-
# Get the current frame
|
| 201 |
-
curr_frame = inspect.currentframe()
|
| 202 |
-
caller_frame = curr_frame.f_back
|
| 203 |
-
|
| 204 |
-
while not caller_frame.f_back is None:
|
| 205 |
-
caller_frame = caller_frame.f_back
|
| 206 |
-
steps -=1
|
| 207 |
-
if steps <= 0:
|
| 208 |
-
break
|
| 209 |
-
|
| 210 |
-
previous_frame = caller_frame
|
| 211 |
-
|
| 212 |
-
# Retrieve the information about the previous frame
|
| 213 |
-
frame_info = inspect.getframeinfo(previous_frame)
|
| 214 |
-
# Get the file name where the function was called
|
| 215 |
-
filename_with_path = frame_info.filename
|
| 216 |
-
# Extract only the file name
|
| 217 |
-
filename = os.path.basename(filename_with_path)
|
| 218 |
-
# Get the line number in the file where the function was called
|
| 219 |
-
linenumber = frame_info.lineno
|
| 220 |
-
# Get the function name
|
| 221 |
-
function = frame_info.function
|
| 222 |
-
# Format the string to include the passed message
|
| 223 |
-
caller_link = ansi_link_str(f"file:///{filename_with_path}", f"{filename}::{linenumber}::{function}") if use_color else f"{filename}::{linenumber}::{function}"
|
| 224 |
-
info_str = f"{caller_link}: {message}"
|
| 225 |
-
# Clean up to prevent reference cycles
|
| 226 |
-
del curr_frame
|
| 227 |
-
del caller_frame
|
| 228 |
-
del previous_frame
|
| 229 |
-
return info_str
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
def get_caller_info(message='', use_color=True):
|
| 233 |
-
# Get the current frame
|
| 234 |
-
curr_frame = inspect.currentframe()
|
| 235 |
-
# Get the caller's frame
|
| 236 |
-
caller_frame = curr_frame.f_back
|
| 237 |
-
# Retrieve the information about the caller's frame
|
| 238 |
-
frame_info = inspect.getframeinfo(caller_frame)
|
| 239 |
-
# Get the file name where the function was called
|
| 240 |
-
filename_with_path = frame_info.filename
|
| 241 |
-
# Extract only the file name
|
| 242 |
-
filename = os.path.basename(filename_with_path)
|
| 243 |
-
# Get the line number in the file where the function was called
|
| 244 |
-
linenumber = frame_info.lineno
|
| 245 |
-
# get the function name
|
| 246 |
-
function = frame_info.function
|
| 247 |
-
# Format the string to include the passed message
|
| 248 |
-
#caller_link = f"file:///{filename_with_path}", f"{filename}::{linenumber}::{function}"
|
| 249 |
-
caller_link = ansi_link_str(f"file:///{filename_with_path}", f"{filename}::{linenumber}::{function}") if use_color else f"{filename}::{linenumber}::{function}"
|
| 250 |
-
#caller_link = f"{filename}::{linenumber}::{function}"
|
| 251 |
-
|
| 252 |
-
info_str = f"{caller_link}: {message}" # file://
|
| 253 |
-
# Clean up to prevent reference cycles
|
| 254 |
-
del curr_frame
|
| 255 |
-
del caller_frame
|
| 256 |
-
return info_str
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
def get_class_name(obj):
|
| 260 |
-
return obj.__class__.__name__
|
|
|
|
| 1 |
+
from ansi_utils import ansi_link_str, ansi_color_str
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def get_type_name(attr):
|
| 5 |
+
return type(attr).__name__
|
| 6 |
+
|
| 7 |
+
def is_complex_type(obj):
|
| 8 |
+
return is_of_type(variable, (list, set, dict)) or hasattr(variable, '__dict__')
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def is_bytes_like_type(obj, bytes_like_types=(memoryview, bytes, bytearray), raise_err=False):
|
| 12 |
+
try:
|
| 13 |
+
return is_of_type(obj, bytes_like_types)
|
| 14 |
+
except TypeError as e:
|
| 15 |
+
if raise_err:
|
| 16 |
+
raise TypeError(f"Provided object does not match the provided types: {bytes_like_types}")
|
| 17 |
+
return False
|
| 18 |
+
|
| 19 |
+
def is_of_type(obj, types, raise_err=False):
|
| 20 |
+
if isinstance(obj, (types)):
|
| 21 |
+
return True
|
| 22 |
+
elif raise_err:
|
| 23 |
+
raise TypeError(f"Provided object does not match the provided types: {tuple(types)}")
|
| 24 |
+
return False
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def get_partial_argspec(method):
|
| 28 |
+
if not callable(method):
|
| 29 |
+
return None # Not a callable object
|
| 30 |
+
try:
|
| 31 |
+
full_argspec = inspect.getfullargspec(method)
|
| 32 |
+
return full_argspec
|
| 33 |
+
except TypeError:
|
| 34 |
+
# Fallback to using inspect.signature
|
| 35 |
+
signature = get_signature(method)
|
| 36 |
+
if signature:
|
| 37 |
+
parameters = signature.parameters
|
| 38 |
+
args = [param for param in parameters if parameters[param].default == parameters[param].empty]
|
| 39 |
+
varargs = signature.varargs
|
| 40 |
+
varkw = signature.varkw
|
| 41 |
+
defaults = [parameters[param].default for param in args]
|
| 42 |
+
return inspect.FullArgSpec(args, varargs, varkw, defaults)
|
| 43 |
+
|
| 44 |
+
def get_signature(method):
|
| 45 |
+
try:
|
| 46 |
+
signature = inspect.signature(method)
|
| 47 |
+
return signature
|
| 48 |
+
except (TypeError, ValueError):
|
| 49 |
+
return None
|
| 50 |
+
|
| 51 |
+
def get_method_args(method):
|
| 52 |
+
|
| 53 |
+
full_arg_spec = get_partial_argspec(method)
|
| 54 |
+
if full_arg_spec:
|
| 55 |
+
args = [arg for arg in full_arg_spec.args if
|
| 56 |
+
getattr(method, arg, None) is not None and getattr(method, arg, "") != ""]
|
| 57 |
+
kwargs = {key: getattr(method, key, None) for key in full_arg_spec.kwonlyargs}
|
| 58 |
+
kwargs_defaults = {key: value for key, value in
|
| 59 |
+
zip(full_arg_spec.kwonlyargs, full_arg_spec.kwonlydefaults or ())}
|
| 60 |
+
args.extend(f"{key}={value}" for key, value in kwargs.items() if value is not None and value != "")
|
| 61 |
+
return args
|
| 62 |
+
return None
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def get_source_info(attr):
|
| 66 |
+
try:
|
| 67 |
+
source_lines, line_number = inspect.getsourcelines(attr)
|
| 68 |
+
source_file_path = inspect.getsourcefile(attr)
|
| 69 |
+
source_file = os.path.relpath(source_file_path)
|
| 70 |
+
return f"/{source_file}::{line_number}"
|
| 71 |
+
except Exception as e:
|
| 72 |
+
return "Source info not available!"
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def get_method_info(method):
|
| 76 |
+
args = get_method_args(method)
|
| 77 |
+
args_str = ", ".join(args) if args else ''
|
| 78 |
+
signature = get_signature(method)
|
| 79 |
+
return_str = f' -> {signature.return_annotation}' if signature and signature.return_annotation is not inspect.Signature.empty else ''
|
| 80 |
+
|
| 81 |
+
try:
|
| 82 |
+
source_info = get_source_info(method)
|
| 83 |
+
except Exception as e:
|
| 84 |
+
raise Exception("Source info not available!", e)
|
| 85 |
+
|
| 86 |
+
# Construct the file:// URL with line number for the method if available
|
| 87 |
+
method_file_url = f"file://{inspect.getsourcefile(method)}#L{inspect.getsourcelines(method)[1]}"
|
| 88 |
+
method_link = ansi_link_str(method_file_url, "Source")
|
| 89 |
+
|
| 90 |
+
# Include the link in the method signature string
|
| 91 |
+
method_signature = f"{signature}{return_str}: {method_link}\n-->{source_info}"
|
| 92 |
+
|
| 93 |
+
return method_signature
|
| 94 |
+
def get_var_value(variable):
|
| 95 |
+
return f'{str(variable)}' if not is_of_type(variable, (list, set, dict)) or hasattr(variable, '__dict__') else '...'
|
| 96 |
+
|
| 97 |
+
def get_variable_info(variable):
|
| 98 |
+
return f"<{ get_type_name(variable) }>: { get_var_value(variable) }"
|
| 99 |
+
|
| 100 |
+
def list_class_attributes(cls, verbose=True, use_color=False):
|
| 101 |
+
def format_str(s, fg=None):
|
| 102 |
+
return ansi_color_str(s, fg=fg) if use_color else s
|
| 103 |
+
|
| 104 |
+
# Determine whether cls is a class or an instance of a class
|
| 105 |
+
if inspect.isclass(cls):
|
| 106 |
+
class_name = cls.__name__
|
| 107 |
+
else:
|
| 108 |
+
class_name = cls.__class__.__name__
|
| 109 |
+
|
| 110 |
+
variables = [
|
| 111 |
+
f'{attribute}{get_variable_info(getattr(cls, attribute))}' if verbose else f'{attribute}<{get_type_name(getattr(cls, attribute))}>'
|
| 112 |
+
for attribute in dir(cls) if not attribute.startswith('__') and not callable(getattr(cls, attribute))]
|
| 113 |
+
methods = [f'{attribute}{get_method_info(getattr(cls, attribute))}' if verbose else f'{attribute}<method>' for
|
| 114 |
+
attribute in dir(cls) if not attribute.startswith('__') and callable(getattr(cls, attribute))]
|
| 115 |
+
|
| 116 |
+
variables_str = '\n'.join([f' - {format_str(var, fg="green")}' for var in variables])
|
| 117 |
+
methods_str = '\n'.join([f' - {format_str(method, fg="blue")}' for method in methods])
|
| 118 |
+
|
| 119 |
+
cls_name = format_str(class_name, fg="cyan") if use_color else class_name
|
| 120 |
+
return f'===list_class_attributes of: {cls_name}:\n===<variables>===\n{variables_str}\n===<methods>===\n{methods_str}'
|
| 121 |
+
|
| 122 |
+
def get_class_attributes(cls, verbose=True, use_color=True):
|
| 123 |
+
def format_str(s, fg=None):
|
| 124 |
+
return ansi_color_str(s, fg=fg) if use_color else s
|
| 125 |
+
|
| 126 |
+
attributes_dict = {'variables': {}, 'methods': {}}
|
| 127 |
+
|
| 128 |
+
for attribute_v in vars(cls):
|
| 129 |
+
if not attribute_v.startswith('__') and 'stat' not in attribute_v:
|
| 130 |
+
attr = getattr(cls, attribute_v)
|
| 131 |
+
if not callable(attr):
|
| 132 |
+
attr_info = get_variable_info(attr) if verbose else ''
|
| 133 |
+
formatted_key = format_str(attribute_v, fg="green")
|
| 134 |
+
formatted_value = format_str(attr_info, fg="cyan") if verbose else ''
|
| 135 |
+
attributes_dict['variables'][attribute_v] = f'\n ~ {formatted_key}{formatted_value}'
|
| 136 |
+
|
| 137 |
+
for attribute in dir(cls):
|
| 138 |
+
if not attribute.startswith('__'):
|
| 139 |
+
attr = getattr(cls, attribute)
|
| 140 |
+
if callable(attr):
|
| 141 |
+
method_info = get_method_info(attr) if verbose else ''
|
| 142 |
+
formatted_key = format_str(attribute, fg="blue")
|
| 143 |
+
formatted_value = format_str(method_info, fg="cyan") if verbose else ''
|
| 144 |
+
attributes_dict['methods'][attribute] = f'\n ~ {formatted_key}{formatted_value}'
|
| 145 |
+
|
| 146 |
+
return attributes_dict
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
import threading
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
def get_thread_info(message='', use_color=True):
|
| 153 |
+
current_thread = threading.current_thread()
|
| 154 |
+
current_thread_name = current_thread.name
|
| 155 |
+
current_thread_id = current_thread.ident
|
| 156 |
+
current_thread_alive = current_thread.is_alive()
|
| 157 |
+
|
| 158 |
+
# Construct the colored thread info
|
| 159 |
+
thread_info = f'thread:{current_thread_name}::{current_thread_id}::{current_thread_alive}'
|
| 160 |
+
if use_color:
|
| 161 |
+
thread_info = ansi_color_str(thread_info,fg='yellow', bg='bright_yellow')
|
| 162 |
+
|
| 163 |
+
formatted_message = f'{thread_info}:{message}'
|
| 164 |
+
|
| 165 |
+
return formatted_message
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
import inspect
|
| 169 |
+
import os
|
| 170 |
+
|
| 171 |
+
# Get the current frame
|
| 172 |
+
def get_prev_frame(steps=1):
|
| 173 |
+
curr_frame = inspect.currentframe()
|
| 174 |
+
|
| 175 |
+
# Traverse back the specified number of steps in the call stack
|
| 176 |
+
for _ in range(steps):
|
| 177 |
+
if curr_frame is not None:
|
| 178 |
+
curr_frame = curr_frame.f_back
|
| 179 |
+
|
| 180 |
+
if curr_frame is None:
|
| 181 |
+
return None
|
| 182 |
+
|
| 183 |
+
return curr_frame
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
def get_prev_frame_from_frame(frame, steps=1):
|
| 187 |
+
curr_frame = frame
|
| 188 |
+
|
| 189 |
+
# Traverse back the specified number of steps in the call stack
|
| 190 |
+
for _ in range(steps):
|
| 191 |
+
if frame is not None:
|
| 192 |
+
curr_frame = curr_frame.f_back
|
| 193 |
+
|
| 194 |
+
if curr_frame is None:
|
| 195 |
+
return None
|
| 196 |
+
|
| 197 |
+
return curr_frame
|
| 198 |
+
|
| 199 |
+
def get_prev_caller_info(message='', use_color=True, steps=99):
|
| 200 |
+
# Get the current frame
|
| 201 |
+
curr_frame = inspect.currentframe()
|
| 202 |
+
caller_frame = curr_frame.f_back
|
| 203 |
+
|
| 204 |
+
while not caller_frame.f_back is None:
|
| 205 |
+
caller_frame = caller_frame.f_back
|
| 206 |
+
steps -=1
|
| 207 |
+
if steps <= 0:
|
| 208 |
+
break
|
| 209 |
+
|
| 210 |
+
previous_frame = caller_frame
|
| 211 |
+
|
| 212 |
+
# Retrieve the information about the previous frame
|
| 213 |
+
frame_info = inspect.getframeinfo(previous_frame)
|
| 214 |
+
# Get the file name where the function was called
|
| 215 |
+
filename_with_path = frame_info.filename
|
| 216 |
+
# Extract only the file name
|
| 217 |
+
filename = os.path.basename(filename_with_path)
|
| 218 |
+
# Get the line number in the file where the function was called
|
| 219 |
+
linenumber = frame_info.lineno
|
| 220 |
+
# Get the function name
|
| 221 |
+
function = frame_info.function
|
| 222 |
+
# Format the string to include the passed message
|
| 223 |
+
caller_link = ansi_link_str(f"file:///{filename_with_path}", f"{filename}::{linenumber}::{function}") if use_color else f"{filename}::{linenumber}::{function}"
|
| 224 |
+
info_str = f"{caller_link}: {message}"
|
| 225 |
+
# Clean up to prevent reference cycles
|
| 226 |
+
del curr_frame
|
| 227 |
+
del caller_frame
|
| 228 |
+
del previous_frame
|
| 229 |
+
return info_str
|
| 230 |
+
|
| 231 |
+
|
| 232 |
+
def get_caller_info(message='', use_color=True):
|
| 233 |
+
# Get the current frame
|
| 234 |
+
curr_frame = inspect.currentframe()
|
| 235 |
+
# Get the caller's frame
|
| 236 |
+
caller_frame = curr_frame.f_back
|
| 237 |
+
# Retrieve the information about the caller's frame
|
| 238 |
+
frame_info = inspect.getframeinfo(caller_frame)
|
| 239 |
+
# Get the file name where the function was called
|
| 240 |
+
filename_with_path = frame_info.filename
|
| 241 |
+
# Extract only the file name
|
| 242 |
+
filename = os.path.basename(filename_with_path)
|
| 243 |
+
# Get the line number in the file where the function was called
|
| 244 |
+
linenumber = frame_info.lineno
|
| 245 |
+
# get the function name
|
| 246 |
+
function = frame_info.function
|
| 247 |
+
# Format the string to include the passed message
|
| 248 |
+
#caller_link = f"file:///{filename_with_path}", f"{filename}::{linenumber}::{function}"
|
| 249 |
+
caller_link = ansi_link_str(f"file:///{filename_with_path}", f"{filename}::{linenumber}::{function}") if use_color else f"{filename}::{linenumber}::{function}"
|
| 250 |
+
#caller_link = f"{filename}::{linenumber}::{function}"
|
| 251 |
+
|
| 252 |
+
info_str = f"{caller_link}: {message}" # file://
|
| 253 |
+
# Clean up to prevent reference cycles
|
| 254 |
+
del curr_frame
|
| 255 |
+
del caller_frame
|
| 256 |
+
return info_str
|
| 257 |
+
|
| 258 |
+
|
| 259 |
+
def get_class_name(obj):
|
| 260 |
+
return obj.__class__.__name__
|