Added lookup caching and converted indentation to tabs

This commit is contained in:
Philip Trauner 2016-09-11 15:20:06 +02:00
parent 617e336f0b
commit 13b4e0235c

View file

@ -5,12 +5,11 @@ from inspect import stack
from sys import stderr from sys import stderr
from sys import stdout from sys import stdout
try: try:
import sublime import sublime
print_fallback = True print_fallback = True
except ImportError: except ImportError:
print_fallback = False print_fallback = False
# Formatting # Formatting
BOLD = "\033[1m" BOLD = "\033[1m"
@ -57,53 +56,63 @@ LIGHT_BLUE = "\033[94m"
LIGHT_MAGENTA = "\033[95m" LIGHT_MAGENTA = "\033[95m"
LIGHT_CYAN = "\033[96m" LIGHT_CYAN = "\033[96m"
CACHED_MODULES = {}
# If the stack becomes too complex to figure out a caller we go through and assume the first valid module is the caller. # If the stack becomes too complex to figure out a caller we go through and assume the first valid module is the caller.
# This works reasonably well but isn't 100% accurate and will only happen if the caller is a thread. # This works reasonably well but isn't 100% accurate and will only happen if the caller is a thread.
def print_out(message, color, file): def print_out(message, color, file):
stack_ = stack() stack_ = stack()
# Interestingly the if statement below is not executed when excepting KeyboardInterrupts. Weird. # Interestingly the if statement below is not executed when excepting KeyboardInterrupts. Weird.
# To prevent a crash we assume the module's name is 'Unknown' # To prevent a crash we assume the module's name is 'Unknown'
module = "Unknown" module = "Unknown"
if getmodule(stack_[2][0]) == None: if getmodule(stack_[2][0]) == None:
for i in stack_[2:]: for i in stack_[2:]:
if getmodule(i[0]) != None: if getmodule(i[0]) != None:
module = getmodule(i[0]).__name__ try:
else: module = CACHED_MODULES[i[0].f_code]
module = getmodule(stack_[2][0]).__name__ except KeyError:
if not print_fallback: module = getmodule(i[0]).__name__
print("[%s] %s: %s%s\033[0m" % (strftime("%H:%M:%S"), module, color, message), file=file) CACHED_MODULES[i[0].f_code] = module
file.flush() else:
else: try:
print("[%s] %s: %s" % (strftime("%H:%M:%S"), module, message)) module = CACHED_MODULES[stack_[2][0].f_code]
except KeyError:
module = getmodule(stack_[2][0]).__name__
CACHED_MODULES[stack_[2][0].f_code] = module
if not print_fallback:
print("[%s] %s: %s%s\033[0m" % (strftime("%H:%M:%S"), module, color, message), file=file)
file.flush()
else:
print("[%s] %s: %s" % (strftime("%H:%M:%S"), module, message))
def info(message, color=""): def info(message, color=""):
print_out(message, color, stdout) print_out(message, color, stdout)
def header(message): def header(message):
print_out(message, MAGENTA, stdout) print_out(message, MAGENTA, stdout)
def warning(message): def warning(message):
print_out(message, YELLOW, stderr) print_out(message, YELLOW, stderr)
def error(message): def error(message):
print_out(message, RED, stderr) print_out(message, RED, stderr)
def success(message): def success(message):
print_out(message, GREEN, stdout) print_out(message, GREEN, stdout)
if __name__ == "__main__": if __name__ == "__main__":
import sys import sys
info("Hi!", color=UNDERLINE+BACKGROUND_GREEN+RED) info("Hi!", color=UNDERLINE+BACKGROUND_GREEN+RED)
header("This is a header") header("This is a header")
warning("This is a warning") # > stderr warning("This is a warning") # > stderr
error("This is an error") # > stderr error("This is an error") # > stderr
success("Great success!") success("Great success!")