~exarkun/+junk/training

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
import sys

from twisted.internet.protocol import FileWrapper
from twisted.protocols.basic import LineOnlyReceiver

class Echoer(LineOnlyReceiver):
    delimiter = "\n"

    def lineReceived(self, line):
        self.sendLine(line)


def main():
    echo = Echoer()
    echo.makeConnection(FileWrapper(sys.stdout))

    while True:
        byte = sys.stdin.read(1)
        if not byte:
            break
        echo.dataReceived(byte)


def test():
    from StringIO import StringIO
    output = StringIO()
    echo = Echoer()
    echo.makeConnection(FileWrapper(output))
    echo.lineReceived("foo")
    echo.lineReceived("bar")
    assert output.getvalue() == "foo\nbar\n"

test()