Skip to content

Commit

Permalink
Added bufferisation capability to msg() and made context() use it to …
Browse files Browse the repository at this point in the history
…avoid lag while printing to the console which caused my head to explode.
  • Loading branch information
wapiflapi committed Jun 1, 2013
1 parent 0697441 commit 45e0fde
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 22 deletions.
72 changes: 50 additions & 22 deletions lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import string
import re
import itertools
import StringIO
import functools
from subprocess import *
import config

Expand Down Expand Up @@ -73,7 +75,7 @@ def reset_cache(module=None):
"""
if module is None:
module = sys.modules['__main__']

for m in dir(module):
m = getattr(module, m)
if isinstance(m, memoized):
Expand Down Expand Up @@ -106,7 +108,7 @@ def colorize(text, color=None, attrib=None):

if config.Option.get("ansicolor") != "on":
return text

ccode = ""
if attrib:
for attr in attrib.lower().split():
Expand All @@ -133,21 +135,47 @@ def blue(text, attrib=None):
"""Wrapper for colorize(text, 'blue')"""
return colorize(text, "blue", attrib)

def msg(text, color=None, attrib=None, teefd=None):
class mesager(object):
"""
Generic pretty printer with redirection
Generic pretty printer with redirection.
It also suports buffering using bufferize() and flush().
"""
if not teefd:
teefd = config.Option.get("_teefd")

if isinstance(text, str) and "\x00" not in text:
print colorize(text, color, attrib)
if teefd:
print >> teefd, colorize(text, color, attrib)
else:
pprint.pprint(text)
if teefd:
pprint.pprint(text, teefd)

def __init__(self):
self.out = sys.stdout

def bufferize(self, f=None):
"""Activate mesager's bufferization, can also be used
as a decorater."""
self.out = StringIO.StringIO()

if f != None:
@functools.wraps(f)
def wrapper(*args, **kwargs):
self.bufferize()
f(*args, **kwargs)
self.flush()
return wrapper

def flush(self):
data = self.out.getvalue()
self.out = sys.stdout
self.out.write(data)

def __call__(self, text, color=None, attrib=None, teefd=None):
if not teefd:
teefd = config.Option.get("_teefd")

if isinstance(text, str) and "\x00" not in text:
print >> self.out, colorize(text, color, attrib)
if teefd:
print >> teefd, colorize(text, color, attrib)
else:
pprint.pprint(text, self.out)
if teefd:
pprint.pprint(text, teefd)

msg = mesager()

def warning_msg(text):
"""Colorize warning message with prefix"""
Expand Down Expand Up @@ -195,7 +223,7 @@ def pager(text, pagesize=None):
"""
if not pagesize:
pagesize = config.Option.get("pagesize")

if pagesize <= 0:
msg(text)
return
Expand Down Expand Up @@ -377,7 +405,7 @@ def check_badchars(data, chars=None):

if not chars:
chars = config.Option.get("badchars")

if chars:
for c in chars:
if c in to_search:
Expand Down Expand Up @@ -522,7 +550,7 @@ def cyclic_pattern_charset(charset_type=None):
charset += ["ABCDEFGHIJKLMNOPQRSTUVWXYZ"] # string.uppercase
charset += ["abcdefghijklmnopqrstuvwxyz"] # string.lowercase
charset += ["0123456789"] # string.digits

if not charset_type:
charset_type = config.Option.get("pattern")

Expand All @@ -546,7 +574,7 @@ def cyclic_pattern_charset(charset_type=None):

def de_bruijn(charset, n, maxlen):
"""
Generate the De Bruijn Sequence up to `maxlen` characters for the charset `charset`
Generate the De Bruijn Sequence up to `maxlen` characters for the charset `charset`
and subsequences of length `n`.
Algorithm modified from wikipedia http://en.wikipedia.org/wiki/De_Bruijn_sequence
"""
Expand Down Expand Up @@ -575,7 +603,7 @@ def db(t, p):
@memoized
def cyclic_pattern(size=None, start=None, charset_type=None):
"""
Generate a cyclic pattern
Generate a cyclic pattern
Args:
- size: size of generated pattern (Int)
Expand Down Expand Up @@ -621,7 +649,7 @@ def cyclic_pattern_offset(value):
search = value
else:
search = hex2str(to_int(value))

pos = pattern.find(search)
return pos if pos != -1 else None

Expand Down Expand Up @@ -651,5 +679,5 @@ def cyclic_pattern_search(buf):
i = pattern.find(s)
if i != -1:
result += [(m.start()+k, len(s), i)]

return result
1 change: 1 addition & 0 deletions peda.py
Original file line number Diff line number Diff line change
Expand Up @@ -4204,6 +4204,7 @@ def context_stack(self, *arg):

return

@msg.bufferize
def context(self, *arg):
"""
Display various information of current execution context
Expand Down

0 comments on commit 45e0fde

Please sign in to comment.