.. _fbf.utils.exception: exception ~~~~~~~~~ .. automodule:: fbf.utils.exception :show-inheritance: :members: :undoc-members: CODE ---- :: # fbf/utils/exception.py # # """ exception related functions. """ .. _fbf.utils.exception_basic_imports: basic imports ---------------- :: import sys import traceback import logging import _thread import os import logging .. _fbf.utils.exception_defines_: defines ---------- :: bork = False exceptionlist = [] exceptionevents = [] ERASE_LINE = '\033[2K' BOLD='\033[1m' RED = '\033[91m' YELLOW = '\033[93m' GREEN = '\033[92m' ENDC = '\033[0m' .. _fbf.utils.exception_exceptionmsg_function: exceptionmsg function ------------------------ :: def exceptionmsg(): """ create exception message as a string. """ exctype, excvalue, tb = sys.exc_info() trace = traceback.extract_tb(tb) result = "" for i in trace: fname = i[0] linenr = i[1] func = i[2] plugfile = fname[:-3].split(os.sep) mod = [] for i in plugfile[::-1]: if i in ['fbf.upload']: break mod.append(i) if i in ['fbf', 'data']: break ownname = '.'.join(mod[::-1]) result += "%s:%s %s | " % (ownname, linenr, func) del trace res = "%s%s: %s" % (result, exctype, excvalue) if res not in exceptionlist: exceptionlist.append(res) return res .. _fbf.utils.exception_handle_exception_function: handle_exception function ---------------------------- :: def handle_exception(event=None, log=True, txt="", stop=False): """ handle exception.. for now only print it. """ errormsg = exceptionmsg() if txt: errormsg = "%s - %s" % (txt, errormsg) if log: logging.error(RED + txt + " " + errormsg + ENDC) if event: exceptionevents.append((event, errormsg)) if event.bot: event.bot.error = errormsg if event.bot.type == "irc": target = event.nick else: target = event.channel if target and log: event.bot.saynocb(target, "*sorry* - an exception occured - %s" % errormsg) if stop or bork: os._exit(1)