class EngineListener(object): def __init__(self, engine, ignored=('PRAGMA')): self.engine = engine self.ignored = ignored self.buf = StringIO() @event.listens_for(engine, "before_cursor_execute") def before_cursor_execute(conn, cursor, statement, parameters, context, executemany): sql = to_unicode(statement) for string in self.ignored: if sql.lower().startswith(string.lower()): return sql = sql.replace(' \n', '\n').rstrip('\n') self.buf.write(sql.rstrip('\n') + ";" + '\n') @property def raw_sql(self): self.buf.seek(0) return self.buf.getvalue().replace('\t', ' ')\ .rstrip('\n')