75
75
}
76
76
77
77
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
+
78
105
class Statusbar (object ):
79
106
80
107
"""Statusbar object, ripped off from bpython.cli.
@@ -549,6 +576,8 @@ def main(args=None, locals_=None, banner=None):
549
576
help = 'Run a reactor (see --help-reactors)' ),
550
577
Option ('--help-reactors' , action = 'store_true' ,
551
578
help = 'List available reactors for -r' ),
579
+ Option ('--server' , '-s' , type = 'int' ,
580
+ help = 'Port to run an eval server on (forces Twisted)' ),
552
581
]))
553
582
554
583
if options .help_reactors :
@@ -566,6 +595,9 @@ def main(args=None, locals_=None, banner=None):
566
595
('bold ' + name , color + ',bold' , background , monochrome )
567
596
for name , color , background , monochrome in palette ])
568
597
598
+ if options .server and not options .reactor :
599
+ options .reactor = 'select'
600
+
569
601
if options .reactor :
570
602
from twisted .application import reactors
571
603
try :
@@ -612,6 +644,10 @@ def main(args=None, locals_=None, banner=None):
612
644
myrepl = URWIDRepl (loop , frame , listbox , overlay , tooltip ,
613
645
interpreter , statusbar , config )
614
646
647
+ if options .server :
648
+ factory = EvalFactory (myrepl )
649
+ reactor .listenTCP (options .server , factory )
650
+
615
651
if options .reactor :
616
652
# Twisted sets a sigInt handler that stops the reactor unless
617
653
# it sees a different custom signal handler.
0 commit comments