Commit ac988740 by fwkz

Handling nested @mute decorators.

parent a2201f96
...@@ -13,6 +13,8 @@ except ImportError: ...@@ -13,6 +13,8 @@ except ImportError:
data_queue = queue.Queue() data_queue = queue.Queue()
printer_queue = queue.Queue() printer_queue = queue.Queue()
thread_output_stream = WeakKeyDictionary()
class DataProducerThread(threading.Thread): class DataProducerThread(threading.Thread):
def __init__(self, data): def __init__(self, data):
...@@ -42,7 +44,6 @@ class PrinterThread(threading.Thread): ...@@ -42,7 +44,6 @@ class PrinterThread(threading.Thread):
def __init__(self): def __init__(self):
super(PrinterThread, self).__init__() super(PrinterThread, self).__init__()
self.daemon = True self.daemon = True
self.std_out = WeakKeyDictionary()
def run(self): def run(self):
while True: while True:
......
...@@ -18,7 +18,7 @@ from abc import ABCMeta, abstractmethod ...@@ -18,7 +18,7 @@ from abc import ABCMeta, abstractmethod
import requests import requests
from .threads import printer_queue from .threads import printer_queue, thread_output_stream
from .exceptions import RoutersploitException from .exceptions import RoutersploitException
from . import modules as rsf_modules from . import modules as rsf_modules
...@@ -164,11 +164,11 @@ def mute(fn): ...@@ -164,11 +164,11 @@ def mute(fn):
""" Suppress function from printing to sys.stdout """ """ Suppress function from printing to sys.stdout """
@wraps(fn) @wraps(fn)
def wrapper(self, *args, **kwargs): def wrapper(self, *args, **kwargs):
sys.stdout = DummyFile() thread_output_stream.setdefault(threading.current_thread(), []).append(DummyFile())
try: try:
return fn(self, *args, **kwargs) return fn(self, *args, **kwargs)
finally: finally:
sys.stdout = sys.__stdout__ thread_output_stream[threading.current_thread()].pop()
return wrapper return wrapper
...@@ -228,10 +228,14 @@ def __cprint(*args, **kwargs): ...@@ -228,10 +228,14 @@ def __cprint(*args, **kwargs):
return return
color = kwargs.get('color', None) color = kwargs.get('color', None)
file_ = kwargs.get('file', sys.stdout)
sep = kwargs.get('sep', ' ') sep = kwargs.get('sep', ' ')
end = kwargs.get('end', '\n') end = kwargs.get('end', '\n')
thread = threading.current_thread() thread = threading.current_thread()
try:
file_ = thread_output_stream.get(thread, ())[-1]
except IndexError:
file_ = kwargs.get('file', sys.stdout)
if color: if color:
printer_queue.put(PrintResource(content='\033[{}m'.format(colors[color]), end='', file=file_, sep=sep, thread=thread)) printer_queue.put(PrintResource(content='\033[{}m'.format(colors[color]), end='', file=file_, sep=sep, thread=thread))
printer_queue.put(PrintResource(content=args, end='', file=file_, sep=sep, thread=thread)) # TODO printing text that starts from newline printer_queue.put(PrintResource(content=args, end='', file=file_, sep=sep, thread=thread)) # TODO printing text that starts from newline
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment