Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: dfe0f5e8a1
Fetching contributors…

Cannot retrieve contributors at this time

file 46 lines (35 sloc) 1.241 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
import sys
class OutputWrapper(object):
    def __init__(self, outputter, orig):
        self.outputter = outputter
        self.orig = orig
        
    def write(self, data):
        self.outputter.write(self.orig, data)

    def __getattr__(self, key):
        return getattr(self.orig, key)

class Outputter(object):
    
    def __init__(self, stdout):
        self.observers = []
        self.stdout = stdout
        self.buffer = ""
        self.locked = False
        
        
    def add_observer(self, observer):
        self.observers.append(observer)
    
    def do_write(self):
        if '\n' in self.buffer:
            out, self.buffer = self.buffer.rsplit('\n', 1)
            for observer in self.observers:
                observer(out + '\n')
        self.locked = False

    def _print(self, *args):
        self.stdout.write(' '.join([str(a) for a in args]) + '\n')
        
    def write(self, target, data):
        target.write(data)
        self.buffer += data
        if not self.locked:
            self.locked = True
            eventlet.spawn(self.do_write)


outputter = Outputter(sys.stdout)
sys.stdout = OutputWrapper(outputter, sys.stdout)
sys.stderr = OutputWrapper(outputter, sys.stderr)
import eventlet
Something went wrong with that request. Please try again.