<s> from twisted . internet . protocol import Protocol <EOL> import sys , traceback <EOL> class Cli_session ( Protocol ) : <EOL> command_cb = None <EOL> rbuffer = None <EOL> wbuffer = None <EOL> cb_busy = False <EOL> expect_lf = True <EOL> raddr = None <EOL> def __init__ ( self ) : <EOL> self . rbuffer = '<STR_LIT>' <EOL> self . wbuffer = '<STR_LIT>' <EOL> def dataReceived ( self , data ) : <EOL> if len ( data ) == <NUM_LIT:0> : <EOL> return <EOL> self . rbuffer += data <EOL> self . pump_rxdata ( ) <EOL> def pump_rxdata ( self ) : <EOL> while self . rbuffer != None and len ( self . rbuffer ) > <NUM_LIT:0> : <EOL> if self . cb_busy : <EOL> return <EOL> if self . rbuffer . find ( '<STR_LIT:\n>' ) == - <NUM_LIT:1> and self . expect_lf : <EOL> return <EOL> parts = self . rbuffer . split ( '<STR_LIT:\n>' , <NUM_LIT:1> ) <EOL> if len ( parts ) == <NUM_LIT:1> : <EOL> parts = ( parts [ <NUM_LIT:0> ] , '<STR_LIT>' ) <EOL> cmd , self . rbuffer = parts <EOL> cmd = cmd . strip ( ) <EOL> if len ( cmd ) > <NUM_LIT:0> : <EOL> try : <EOL> self . cb_busy = self . command_cb ( self , cmd ) <EOL> except : <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT:->' * <NUM_LIT> <EOL> traceback . print_exc ( file = sys . stdout ) <EOL> print '<STR_LIT:->' * <NUM_LIT> <EOL> def done ( self ) : <EOL> self . cb_busy = False <EOL> self . pump_rxdata ( ) <EOL> def send ( self , data ) : <EOL> if isinstance ( data , unicode ) : <EOL> data = data . encode ( '<STR_LIT:ascii>' ) <EOL> return self . transport . write ( data ) <EOL> def close ( self ) : <EOL> return self . transport . loseConnection ( ) </s>
