import code import sys import readline import atexit import os import logging from textwrap import TextWrapper #FIXME - This needs to be cleaned up. Not sure what the design # for pybald model logging should be RED = "\033[1;31m" WHITE = "\033[1;m" GREEN = "\033[1;32m" YELLOW = "\033[1;33m" def set_logging(): '''Colored Logging.''' # experimental logging handling class WrappedStream(object): def __init__(self): self.sql_wrapper = TextWrapper(width=100, initial_indent=' '*15+'sql> ', subsequent_indent=' '*20) # , # replace_whitespace=False) def write(self, text): if text: print ("".join((YELLOW, "\n".join([self.sql_wrapper.fill(t) for t in text.splitlines(False)]), WHITE))) def flush(self, *pargs, **kargs): pass # logging.basicConfig() engine_log = logging.getLogger('sqlalchemy.engine') logging.getLogger('sqlalchemy.orm.unitofwork').setLevel(logging.WARN) # print dir(engine_log) h = logging.StreamHandler(WrappedStream()) engine_log.setLevel(logging.INFO) formatter = logging.Formatter("%(message)s") h.setFormatter(formatter) engine_log.addHandler(h) class Console(code.InteractiveConsole): def __init__(self, locals=None, filename="", histfile=os.path.expanduser("~/.pybald-demo-console-history")): code.InteractiveConsole.__init__(self, locals, filename) self.init_history(histfile) def init_history(self, histfile): readline.parse_and_bind("tab: complete") if hasattr(readline, "read_history_file"): try: readline.read_history_file(histfile) except IOError: pass atexit.register(self.save_history, histfile) def save_history(self, histfile): readline.write_history_file(histfile)