Skip to content

Commit 3d6b255

Browse files
committed
Support listening for code to eval on a tcp port when using urwid+twisted.
1 parent dade7d1 commit 3d6b255

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

bpython/urwid.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,33 @@
7575
}
7676

7777

78+
try:
79+
from twisted.internet import protocol
80+
from twisted.protocols import basic
81+
except ImportError:
82+
pass
83+
else:
84+
85+
class EvalProtocol(basic.LineOnlyReceiver):
86+
87+
delimiter = '\n'
88+
89+
def __init__(self, myrepl):
90+
self.repl = myrepl
91+
92+
def lineReceived(self, line):
93+
self.repl.push(line)
94+
95+
96+
class EvalFactory(protocol.ServerFactory):
97+
98+
def __init__(self, myrepl):
99+
self.repl = myrepl
100+
101+
def buildProtocol(self, addr):
102+
return EvalProtocol(self.repl)
103+
104+
78105
class Statusbar(object):
79106

80107
"""Statusbar object, ripped off from bpython.cli.
@@ -549,6 +576,8 @@ def main(args=None, locals_=None, banner=None):
549576
help='Run a reactor (see --help-reactors)'),
550577
Option('--help-reactors', action='store_true',
551578
help='List available reactors for -r'),
579+
Option('--server', '-s', type='int',
580+
help='Port to run an eval server on (forces Twisted)'),
552581
]))
553582

554583
if options.help_reactors:
@@ -566,6 +595,9 @@ def main(args=None, locals_=None, banner=None):
566595
('bold ' + name, color + ',bold', background, monochrome)
567596
for name, color, background, monochrome in palette])
568597

598+
if options.server and not options.reactor:
599+
options.reactor = 'select'
600+
569601
if options.reactor:
570602
from twisted.application import reactors
571603
try:
@@ -612,6 +644,10 @@ def main(args=None, locals_=None, banner=None):
612644
myrepl = URWIDRepl(loop, frame, listbox, overlay, tooltip,
613645
interpreter, statusbar, config)
614646

647+
if options.server:
648+
factory = EvalFactory(myrepl)
649+
reactor.listenTCP(options.server, factory)
650+
615651
if options.reactor:
616652
# Twisted sets a sigInt handler that stops the reactor unless
617653
# it sees a different custom signal handler.

0 commit comments

Comments
 (0)