.. _fbf.utils.trace: trace ~~~~~ .. automodule:: fbf.utils.trace :show-inheritance: :members: :undoc-members: CODE ---- :: # fbf/utils/trace.py # # """ trace related functions """ .. _fbf.utils.trace_basic_imports: basic imports ---------------- :: import sys import os .. _fbf.utils.trace_defines_: defines ---------- :: stopmarkers = ['fbf', 'fbf-myplugs', 'myplugs', 'python3', 'runtime', 'api', 'versions'] .. _fbf.utils.trace_calledfrom_function: calledfrom function ---------------------- :: def calledfrom(frame): """ return the plugin name where given frame occured. """ try: filename = frame.f_back.f_code.co_filename plugfile = filename.split(os.sep) if plugfile: mod = [] for i in plugfile[::-1]: mod.append(i) if i in stopmarkers: break modstr = '.'.join(mod[::-1])[:-3] if 'handler_' in modstr: modstr = modstr.split('.')[-1] except AttributeError: modstr = None del frame return modstr .. _fbf.utils.trace_callstack_function: callstack function --------------------- :: def callstack(frame): """ return callstack trace as a string. """ result = [] loopframe = frame marker = "" while 1: try: filename = loopframe.f_back.f_code.co_filename plugfile = filename.split(os.sep) if plugfile: mod = [] for i in plugfile[::-1]: mod.append(i) if i in stopmarkers: marker = i ; break modstr = '.'.join(mod[::-1])[:-3] if 'handler_' in modstr: modstr = modstr.split('.')[-1] if not modstr: modstr = plugfile result.append("%s:%s" % (modstr, loopframe.f_back.f_lineno)) loopframe = loopframe.f_back except: break del frame del loopframe return result .. _fbf.utils.trace_where_function: where function ----------------- :: def where(): return callstack(sys._getframe(0)) .. _fbf.utils.trace_whichmodule_function: whichmodule function ----------------------- :: def whichmodule(depth=1): """ return filename:lineno of the module. """ try: frame = sys._getframe(depth) plugfile = frame.f_back.f_code.co_filename[:-3].split('/') lineno = frame.f_back.f_lineno mod = [] stop = False for i in plugfile[::-1]: if stop: break mod.append(i) for j in stopmarkers: if j in i: stop = True modstr = '.'.join(mod[::-1]) + ':' + str(lineno) #if 'handler_' in modstr or "python" in modstr: modstr = modstr.split('.')[-1] except AttributeError: modstr = None del frame return modstr .. _fbf.utils.trace_whichplugin_function: whichplugin function ----------------------- :: def whichplugin(depth=1): """ return filename:lineno of the module. """ try: frame = sys._getframe(depth) plugfile = frame.f_back.f_code.co_filename[:-3].split('/') lineno = frame.f_back.f_lineno mod = [] for i in plugfile[::-1]: mod.append(i) if i in stopmarkers: break modstr = '.'.join(mod[::-1]) if 'handler_' in modstr: modstr = modstr.split('.')[-1] except AttributeError: modstr = None del frame return modstr