<s> """<STR_LIT>""" <EOL> from zope . interface import implementer , Interface <EOL> from twisted . internet import protocol , defer , interfaces as iinternet <EOL> class ITerminalProtocol ( Interface ) : <EOL> def makeConnection ( transport ) : <EOL> """<STR_LIT>""" <EOL> def keystrokeReceived ( keyID , modifier ) : <EOL> """<STR_LIT>""" <EOL> def terminalSize ( width , height ) : <EOL> """<STR_LIT>""" <EOL> def unhandledControlSequence ( seq ) : <EOL> """<STR_LIT>""" <EOL> def connectionLost ( reason ) : <EOL> """<STR_LIT>""" <EOL> @ implementer ( ITerminalProtocol ) <EOL> class TerminalProtocol ( object ) : <EOL> def makeConnection ( self , terminal ) : <EOL> self . terminal = terminal <EOL> self . connectionMade ( ) <EOL> def connectionMade ( self ) : <EOL> """<STR_LIT>""" <EOL> def keystrokeReceived ( self , keyID , modifier ) : <EOL> pass <EOL> def terminalSize ( self , width , height ) : <EOL> pass <EOL> def unhandledControlSequence ( self , seq ) : <EOL> pass <EOL> def connectionLost ( self , reason ) : <EOL> pass <EOL> class ITerminalTransport ( iinternet . ITransport ) : <EOL> def cursorUp ( n = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> def cursorDown ( n = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> def cursorForward ( n = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> def cursorBackward ( n = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> def cursorPosition ( column , line ) : <EOL> """<STR_LIT>""" <EOL> def cursorHome ( ) : <EOL> """<STR_LIT>""" <EOL> def index ( ) : <EOL> """<STR_LIT>""" <EOL> def reverseIndex ( ) : <EOL> """<STR_LIT>""" <EOL> def nextLine ( ) : <EOL> """<STR_LIT>""" <EOL> def saveCursor ( ) : <EOL> """<STR_LIT>""" <EOL> def restoreCursor ( ) : <EOL> """<STR_LIT>""" <EOL> def setModes ( modes ) : <EOL> """<STR_LIT>""" <EOL> def resetModes ( mode ) : <EOL> """<STR_LIT>""" <EOL> def setPrivateModes ( modes ) : <EOL> """<STR_LIT>""" <EOL> def resetPrivateModes ( modes ) : <EOL> """<STR_LIT>""" <EOL> def applicationKeypadMode ( ) : <EOL> """<STR_LIT>""" <EOL> def numericKeypadMode ( ) : <EOL> """<STR_LIT>""" <EOL> def selectCharacterSet ( charSet , which ) : <EOL> """<STR_LIT>""" <EOL> def shiftIn ( ) : <EOL> """<STR_LIT>""" <EOL> def shiftOut ( ) : <EOL> """<STR_LIT>""" <EOL> def singleShift2 ( ) : <EOL> """<STR_LIT>""" <EOL> def singleShift3 ( ) : <EOL> """<STR_LIT>""" <EOL> def selectGraphicRendition ( * attributes ) : <EOL> """<STR_LIT>""" <EOL> def horizontalTabulationSet ( ) : <EOL> """<STR_LIT>""" <EOL> def tabulationClear ( ) : <EOL> """<STR_LIT>""" <EOL> def tabulationClearAll ( ) : <EOL> """<STR_LIT>""" <EOL> def doubleHeightLine ( top = True ) : <EOL> """<STR_LIT>""" <EOL> def singleWidthLine ( ) : <EOL> """<STR_LIT>""" <EOL> def doubleWidthLine ( ) : <EOL> """<STR_LIT>""" <EOL> def eraseToLineEnd ( ) : <EOL> """<STR_LIT>""" <EOL> def eraseToLineBeginning ( ) : <EOL> """<STR_LIT>""" <EOL> def eraseLine ( ) : <EOL> """<STR_LIT>""" <EOL> def eraseToDisplayEnd ( ) : <EOL> """<STR_LIT>""" <EOL> def eraseToDisplayBeginning ( ) : <EOL> """<STR_LIT>""" <EOL> def eraseDisplay ( ) : <EOL> """<STR_LIT>""" <EOL> def deleteCharacter ( n = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> def insertLine ( n = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> def deleteLine ( n = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> def reportCursorPosition ( ) : <EOL> """<STR_LIT>""" <EOL> def reset ( ) : <EOL> """<STR_LIT>""" <EOL> def unhandledControlSequence ( seq ) : <EOL> """<STR_LIT>""" <EOL> CSI = '<STR_LIT>' <EOL> CST = { '<STR_LIT>' : '<STR_LIT>' } <EOL> class modes : <EOL> """<STR_LIT>""" <EOL> KEYBOARD_ACTION = KAM = <NUM_LIT:2> <EOL> INSERTION_REPLACEMENT = IRM = <NUM_LIT:4> <EOL> LINEFEED_NEWLINE = LNM = <NUM_LIT:20> <EOL> class privateModes : <EOL> """<STR_LIT>""" <EOL> ERROR = <NUM_LIT:0> <EOL> CURSOR_KEY = <NUM_LIT:1> <EOL> ANSI_VT52 = <NUM_LIT:2> <EOL> COLUMN = <NUM_LIT:3> <EOL> SCROLL = <NUM_LIT:4> <EOL> SCREEN = <NUM_LIT:5> <EOL> ORIGIN = <NUM_LIT:6> <EOL> AUTO_WRAP = <NUM_LIT:7> <EOL> AUTO_REPEAT = <NUM_LIT:8> <EOL> PRINTER_FORM_FEED = <NUM_LIT> <EOL> PRINTER_EXTENT = <NUM_LIT> <EOL> CURSOR_MODE = <NUM_LIT> <EOL> CS_US = '<STR_LIT>' <EOL> CS_UK = '<STR_LIT>' <EOL> CS_DRAWING = '<STR_LIT>' <EOL> CS_ALTERNATE = '<STR_LIT>' <EOL> CS_ALTERNATE_SPECIAL = '<STR_LIT>' <EOL> G0 = '<STR_LIT>' <EOL> G1 = '<STR_LIT>' <EOL> G2 = '<STR_LIT>' <EOL> G3 = '<STR_LIT>' <EOL> NORMAL = <NUM_LIT:0> <EOL> BOLD = <NUM_LIT:1> <EOL> UNDERLINE = <NUM_LIT:4> <EOL> BLINK = <NUM_LIT:5> <EOL> REVERSE_VIDEO = <NUM_LIT:7> <EOL> class Vector : <EOL> def __init__ ( self , x , y ) : <EOL> self . x = x <EOL> self . y = y <EOL> def log ( s ) : <EOL> file ( '<STR_LIT>' , '<STR_LIT:a>' ) . write ( str ( s ) + '<STR_LIT:\n>' ) <EOL> _KEY_NAMES = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> class _const ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name ) : <EOL> self . name = name <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT:[>' + self . name + '<STR_LIT:]>' <EOL> FUNCTION_KEYS = [ <EOL> _const ( _name ) for _name in _KEY_NAMES ] <EOL> @ implementer ( ITerminalTransport ) <EOL> class ServerProtocol ( protocol . Protocol ) : <EOL> protocolFactory = None <EOL> terminalProtocol = None <EOL> TAB = '<STR_LIT:\t>' <EOL> BACKSPACE = '<STR_LIT>' <EOL> lastWrite = '<STR_LIT>' <EOL> state = '<STR_LIT:data>' <EOL> termSize = Vector ( <NUM_LIT> , <NUM_LIT> ) <EOL> cursorPos = Vector ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> scrollRegion = None <EOL> factory = None <EOL> def __init__ ( self , protocolFactory = None , * a , ** kw ) : <EOL> """<STR_LIT>""" <EOL> if protocolFactory is not None : <EOL> self . protocolFactory = protocolFactory <EOL> self . protocolArgs = a <EOL> self . protocolKwArgs = kw <EOL> self . _cursorReports = [ ] <EOL> def connectionMade ( self ) : <EOL> if self . protocolFactory is not None : <EOL> self . terminalProtocol = self . protocolFactory ( * self . protocolArgs , ** self . protocolKwArgs ) <EOL> try : <EOL> factory = self . factory <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> self . terminalProtocol . factory = factory <EOL> self . terminalProtocol . makeConnection ( self ) <EOL> def dataReceived ( self , data ) : <EOL> for ch in data : <EOL> if self . state == '<STR_LIT:data>' : <EOL> if ch == '<STR_LIT>' : <EOL> self . state = '<STR_LIT>' <EOL> else : <EOL> self . terminalProtocol . keystrokeReceived ( ch , None ) <EOL> elif self . state == '<STR_LIT>' : <EOL> if ch == '<STR_LIT:[>' : <EOL> self . state = '<STR_LIT>' <EOL> self . escBuf = [ ] <EOL> elif ch == '<STR_LIT:O>' : <EOL> self . state = '<STR_LIT>' <EOL> else : <EOL> self . state = '<STR_LIT:data>' <EOL> self . _handleShortControlSequence ( ch ) <EOL> elif self . state == '<STR_LIT>' : <EOL> if ch == '<STR_LIT:O>' : <EOL> self . state = '<STR_LIT>' <EOL> elif ch . isalpha ( ) or ch == '<STR_LIT>' : <EOL> self . _handleControlSequence ( '<STR_LIT>' . join ( self . escBuf ) + ch ) <EOL> del self . escBuf <EOL> self . state = '<STR_LIT:data>' <EOL> else : <EOL> self . escBuf . append ( ch ) <EOL> elif self . state == '<STR_LIT>' : <EOL> self . _handleLowFunctionControlSequence ( ch ) <EOL> self . state = '<STR_LIT:data>' <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def _handleShortControlSequence ( self , ch ) : <EOL> self . terminalProtocol . keystrokeReceived ( ch , self . ALT ) <EOL> def _handleControlSequence ( self , buf ) : <EOL> buf = '<STR_LIT>' + buf <EOL> f = getattr ( self . controlSequenceParser , CST . get ( buf [ - <NUM_LIT:1> ] , buf [ - <NUM_LIT:1> ] ) , None ) <EOL> if f is None : <EOL> self . unhandledControlSequence ( buf ) <EOL> else : <EOL> f ( self , self . terminalProtocol , buf [ : - <NUM_LIT:1> ] ) <EOL> def unhandledControlSequence ( self , buf ) : <EOL> self . terminalProtocol . unhandledControlSequence ( buf ) <EOL> def _handleLowFunctionControlSequence ( self , ch ) : <EOL> map = { '<STR_LIT:P>' : self . F1 , '<STR_LIT>' : self . F2 , '<STR_LIT:R>' : self . F3 , '<STR_LIT:S>' : self . F4 } <EOL> keyID = map . get ( ch ) <EOL> if keyID is not None : <EOL> self . terminalProtocol . keystrokeReceived ( keyID , None ) <EOL> else : <EOL> self . terminalProtocol . unhandledControlSequence ( '<STR_LIT>' + ch ) <EOL> class ControlSequenceParser : <EOL> def A ( self , proto , handler , buf ) : <EOL> if buf == '<STR_LIT>' : <EOL> handler . keystrokeReceived ( proto . UP_ARROW , None ) <EOL> else : <EOL> handler . unhandledControlSequence ( buf + '<STR_LIT:A>' ) <EOL> def B ( self , proto , handler , buf ) : <EOL> if buf == '<STR_LIT>' : <EOL> handler . keystrokeReceived ( proto . DOWN_ARROW , None ) <EOL> else : <EOL> handler . unhandledControlSequence ( buf + '<STR_LIT:B>' ) <EOL> def C ( self , proto , handler , buf ) : <EOL> if buf == '<STR_LIT>' : <EOL> handler . keystrokeReceived ( proto . RIGHT_ARROW , None ) <EOL> else : <EOL> handler . unhandledControlSequence ( buf + '<STR_LIT:C>' ) <EOL> def D ( self , proto , handler , buf ) : <EOL> if buf == '<STR_LIT>' : <EOL> handler . keystrokeReceived ( proto . LEFT_ARROW , None ) <EOL> else : <EOL> handler . unhandledControlSequence ( buf + '<STR_LIT:D>' ) <EOL> def E ( self , proto , handler , buf ) : <EOL> if buf == '<STR_LIT>' : <EOL> handler . keystrokeReceived ( proto . NUMPAD_MIDDLE , None ) <EOL> else : <EOL> handler . unhandledControlSequence ( buf + '<STR_LIT:E>' ) <EOL> def F ( self , proto , handler , buf ) : <EOL> if buf == '<STR_LIT>' : <EOL> handler . keystrokeReceived ( proto . END , None ) <EOL> else : <EOL> handler . unhandledControlSequence ( buf + '<STR_LIT:F>' ) <EOL> def H ( self , proto , handler , buf ) : <EOL> if buf == '<STR_LIT>' : <EOL> handler . keystrokeReceived ( proto . HOME , None ) <EOL> else : <EOL> handler . unhandledControlSequence ( buf + '<STR_LIT:H>' ) <EOL> def R ( self , proto , handler , buf ) : <EOL> if not proto . _cursorReports : <EOL> handler . unhandledControlSequence ( buf + '<STR_LIT:R>' ) <EOL> elif buf . startswith ( '<STR_LIT>' ) : <EOL> report = buf [ <NUM_LIT:2> : ] <EOL> parts = report . split ( '<STR_LIT:;>' ) <EOL> if len ( parts ) != <NUM_LIT:2> : <EOL> handler . unhandledControlSequence ( buf + '<STR_LIT:R>' ) <EOL> else : <EOL> Pl , Pc = parts <EOL> try : <EOL> Pl , Pc = int ( Pl ) , int ( Pc ) <EOL> except ValueError : <EOL> handler . unhandledControlSequence ( buf + '<STR_LIT:R>' ) <EOL> else : <EOL> d = proto . _cursorReports . pop ( <NUM_LIT:0> ) <EOL> d . callback ( ( Pc - <NUM_LIT:1> , Pl - <NUM_LIT:1> ) ) <EOL> else : <EOL> handler . unhandledControlSequence ( buf + '<STR_LIT:R>' ) <EOL> def Z ( self , proto , handler , buf ) : <EOL> if buf == '<STR_LIT>' : <EOL> handler . keystrokeReceived ( proto . TAB , proto . SHIFT ) <EOL> else : <EOL> handler . unhandledControlSequence ( buf + '<STR_LIT>' ) <EOL> def tilde ( self , proto , handler , buf ) : <EOL> map = { <NUM_LIT:1> : proto . HOME , <NUM_LIT:2> : proto . INSERT , <NUM_LIT:3> : proto . DELETE , <EOL> <NUM_LIT:4> : proto . END , <NUM_LIT:5> : proto . PGUP , <NUM_LIT:6> : proto . PGDN , <EOL> <NUM_LIT:15> : proto . F5 , <NUM_LIT> : proto . F6 , <NUM_LIT> : proto . F7 , <EOL> <NUM_LIT> : proto . F8 , <NUM_LIT:20> : proto . F9 , <NUM_LIT> : proto . F10 , <EOL> <NUM_LIT> : proto . F11 , <NUM_LIT> : proto . F12 } <EOL> if buf . startswith ( '<STR_LIT>' ) : <EOL> ch = buf [ <NUM_LIT:2> : ] <EOL> try : <EOL> v = int ( ch ) <EOL> except ValueError : <EOL> handler . unhandledControlSequence ( buf + '<STR_LIT>' ) <EOL> else : <EOL> symbolic = map . get ( v ) <EOL> if symbolic is not None : <EOL> handler . keystrokeReceived ( map [ v ] , None ) <EOL> else : <EOL> handler . unhandledControlSequence ( buf + '<STR_LIT>' ) <EOL> else : <EOL> handler . unhandledControlSequence ( buf + '<STR_LIT>' ) <EOL> controlSequenceParser = ControlSequenceParser ( ) <EOL> def cursorUp ( self , n = <NUM_LIT:1> ) : <EOL> assert n >= <NUM_LIT:1> <EOL> self . cursorPos . y = max ( self . cursorPos . y - n , <NUM_LIT:0> ) <EOL> self . write ( '<STR_LIT>' % ( n , ) ) <EOL> def cursorDown ( self , n = <NUM_LIT:1> ) : <EOL> assert n >= <NUM_LIT:1> <EOL> self . cursorPos . y = min ( self . cursorPos . y + n , self . termSize . y - <NUM_LIT:1> ) <EOL> self . write ( '<STR_LIT>' % ( n , ) ) <EOL> def cursorForward ( self , n = <NUM_LIT:1> ) : <EOL> assert n >= <NUM_LIT:1> <EOL> self . cursorPos . x = min ( self . cursorPos . x + n , self . termSize . x - <NUM_LIT:1> ) <EOL> self . write ( '<STR_LIT>' % ( n , ) ) <EOL> def cursorBackward ( self , n = <NUM_LIT:1> ) : <EOL> assert n >= <NUM_LIT:1> <EOL> self . cursorPos . x = max ( self . cursorPos . x - n , <NUM_LIT:0> ) <EOL> self . write ( '<STR_LIT>' % ( n , ) ) <EOL> def cursorPosition ( self , column , line ) : <EOL> self . write ( '<STR_LIT>' % ( line + <NUM_LIT:1> , column + <NUM_LIT:1> ) ) <EOL> def cursorHome ( self ) : <EOL> self . cursorPos . x = self . cursorPos . y = <NUM_LIT:0> <EOL> self . write ( '<STR_LIT>' ) <EOL> def index ( self ) : <EOL> self . cursorPos . y = min ( self . cursorPos . y + <NUM_LIT:1> , self . termSize . y - <NUM_LIT:1> ) <EOL> self . write ( '<STR_LIT>' ) <EOL> def reverseIndex ( self ) : <EOL> self . cursorPos . y = max ( self . cursorPos . y - <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> self . write ( '<STR_LIT>' ) <EOL> def nextLine ( self ) : <EOL> self . cursorPos . x = <NUM_LIT:0> <EOL> self . cursorPos . y = min ( self . cursorPos . y + <NUM_LIT:1> , self . termSize . y - <NUM_LIT:1> ) <EOL> self . write ( '<STR_LIT:\n>' ) <EOL> def saveCursor ( self ) : <EOL> self . _savedCursorPos = Vector ( self . cursorPos . x , self . cursorPos . y ) <EOL> self . write ( '<STR_LIT>' ) <EOL> def restoreCursor ( self ) : <EOL> self . cursorPos = self . _savedCursorPos <EOL> del self . _savedCursorPos <EOL> self . write ( '<STR_LIT>' ) <EOL> def setModes ( self , modes ) : <EOL> self . write ( '<STR_LIT>' % ( '<STR_LIT:;>' . join ( map ( str , modes ) ) , ) ) <EOL> def setPrivateModes ( self , modes ) : <EOL> self . write ( '<STR_LIT>' % ( '<STR_LIT:;>' . join ( map ( str , modes ) ) , ) ) <EOL> def resetModes ( self , modes ) : <EOL> self . write ( '<STR_LIT>' % ( '<STR_LIT:;>' . join ( map ( str , modes ) ) , ) ) <EOL> def resetPrivateModes ( self , modes ) : <EOL> self . write ( '<STR_LIT>' % ( '<STR_LIT:;>' . join ( map ( str , modes ) ) , ) ) <EOL> def applicationKeypadMode ( self ) : <EOL> self . write ( '<STR_LIT>' ) <EOL> def numericKeypadMode ( self ) : <EOL> self . write ( '<STR_LIT>' ) <EOL> def selectCharacterSet ( self , charSet , which ) : <EOL> if which == G0 : <EOL> which = '<STR_LIT:(>' <EOL> elif which == G1 : <EOL> which = '<STR_LIT:)>' <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if charSet == CS_UK : <EOL> charSet = '<STR_LIT:A>' <EOL> elif charSet == CS_US : <EOL> charSet = '<STR_LIT:B>' <EOL> elif charSet == CS_DRAWING : <EOL> charSet = '<STR_LIT:0>' <EOL> elif charSet == CS_ALTERNATE : <EOL> charSet = '<STR_LIT:1>' <EOL> elif charSet == CS_ALTERNATE_SPECIAL : <EOL> charSet = '<STR_LIT:2>' <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . write ( '<STR_LIT>' + which + charSet ) <EOL> def shiftIn ( self ) : <EOL> self . write ( '<STR_LIT>' ) <EOL> def shiftOut ( self ) : <EOL> self . write ( '<STR_LIT>' ) <EOL> def singleShift2 ( self ) : <EOL> self . write ( '<STR_LIT>' ) <EOL> def singleShift3 ( self ) : <EOL> self . write ( '<STR_LIT>' ) <EOL> def selectGraphicRendition ( self , * attributes ) : <EOL> attrs = [ ] <EOL> for a in attributes : <EOL> attrs . append ( a ) <EOL> self . write ( '<STR_LIT>' % ( '<STR_LIT:;>' . join ( attrs ) , ) ) <EOL> def horizontalTabulationSet ( self ) : <EOL> self . write ( '<STR_LIT>' ) <EOL> def tabulationClear ( self ) : <EOL> self . write ( '<STR_LIT>' ) <EOL> def tabulationClearAll ( self ) : <EOL> self . write ( '<STR_LIT>' ) <EOL> def doubleHeightLine ( self , top = True ) : <EOL> if top : <EOL> self . write ( '<STR_LIT>' ) <EOL> else : <EOL> self . write ( '<STR_LIT>' ) <EOL> def singleWidthLine ( self ) : <EOL> self . write ( '<STR_LIT>' ) <EOL> def doubleWidthLine ( self ) : <EOL> self . write ( '<STR_LIT>' ) <EOL> def eraseToLineEnd ( self ) : <EOL> self . write ( '<STR_LIT>' ) <EOL> def eraseToLineBeginning ( self ) : <EOL> self . write ( '<STR_LIT>' ) <EOL> def eraseLine ( self ) : <EOL> self . write ( '<STR_LIT>' ) <EOL> def eraseToDisplayEnd ( self ) : <EOL> self . write ( '<STR_LIT>' ) <EOL> def eraseToDisplayBeginning ( self ) : <EOL> self . write ( '<STR_LIT>' ) <EOL> def eraseDisplay ( self ) : <EOL> self . write ( '<STR_LIT>' ) <EOL> def deleteCharacter ( self , n = <NUM_LIT:1> ) : <EOL> self . write ( '<STR_LIT>' % ( n , ) ) <EOL> def insertLine ( self , n = <NUM_LIT:1> ) : <EOL> self . write ( '<STR_LIT>' % ( n , ) ) <EOL> def deleteLine ( self , n = <NUM_LIT:1> ) : <EOL> self . write ( '<STR_LIT>' % ( n , ) ) <EOL> def setScrollRegion ( self , first = None , last = None ) : <EOL> if first is not None : <EOL> first = '<STR_LIT>' % ( first , ) <EOL> else : <EOL> first = '<STR_LIT>' <EOL> if last is not None : <EOL> last = '<STR_LIT>' % ( last , ) <EOL> else : <EOL> last = '<STR_LIT>' <EOL> self . write ( '<STR_LIT>' % ( first , last ) ) <EOL> def resetScrollRegion ( self ) : <EOL> self . setScrollRegion ( ) <EOL> def reportCursorPosition ( self ) : <EOL> d = defer . Deferred ( ) <EOL> self . _cursorReports . append ( d ) <EOL> self . write ( '<STR_LIT>' ) <EOL> return d <EOL> def reset ( self ) : <EOL> self . cursorPos . x = self . cursorPos . y = <NUM_LIT:0> <EOL> try : <EOL> del self . _savedCursorPos <EOL> except AttributeError : <EOL> pass <EOL> self . write ( '<STR_LIT>' ) <EOL> def write ( self , bytes ) : <EOL> if bytes : <EOL> self . lastWrite = bytes <EOL> self . transport . write ( '<STR_LIT:\r\n>' . join ( bytes . split ( '<STR_LIT:\n>' ) ) ) <EOL> def writeSequence ( self , bytes ) : <EOL> self . write ( '<STR_LIT>' . join ( bytes ) ) <EOL> def loseConnection ( self ) : <EOL> self . reset ( ) <EOL> self . transport . loseConnection ( ) <EOL> def connectionLost ( self , reason ) : <EOL> if self . terminalProtocol is not None : <EOL> try : <EOL> self . terminalProtocol . connectionLost ( reason ) <EOL> finally : <EOL> self . terminalProtocol = None <EOL> for name , const in zip ( _KEY_NAMES , FUNCTION_KEYS ) : <EOL> setattr ( ServerProtocol , name , const ) <EOL> class ClientProtocol ( protocol . Protocol ) : <EOL> terminalFactory = None <EOL> terminal = None <EOL> state = '<STR_LIT:data>' <EOL> _escBuf = None <EOL> _shorts = { <EOL> '<STR_LIT:D>' : '<STR_LIT:index>' , <EOL> '<STR_LIT:M>' : '<STR_LIT>' , <EOL> '<STR_LIT:E>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:=>' : '<STR_LIT>' , <EOL> '<STR_LIT:>>' : '<STR_LIT>' , <EOL> '<STR_LIT:N>' : '<STR_LIT>' , <EOL> '<STR_LIT:O>' : '<STR_LIT>' , <EOL> '<STR_LIT:H>' : '<STR_LIT>' , <EOL> '<STR_LIT:c>' : '<STR_LIT>' } <EOL> _longs = { <EOL> '<STR_LIT:[>' : '<STR_LIT>' , <EOL> '<STR_LIT:(>' : '<STR_LIT>' , <EOL> '<STR_LIT:)>' : '<STR_LIT>' , <EOL> '<STR_LIT:#>' : '<STR_LIT>' } <EOL> _charsets = { <EOL> '<STR_LIT:A>' : CS_UK , <EOL> '<STR_LIT:B>' : CS_US , <EOL> '<STR_LIT:0>' : CS_DRAWING , <EOL> '<STR_LIT:1>' : CS_ALTERNATE , <EOL> '<STR_LIT:2>' : CS_ALTERNATE_SPECIAL } <EOL> factory = None <EOL> def __init__ ( self , terminalFactory = None , * a , ** kw ) : <EOL> """<STR_LIT>""" <EOL> if terminalFactory is not None : <EOL> self . terminalFactory = terminalFactory <EOL> self . terminalArgs = a <EOL> self . terminalKwArgs = kw <EOL> def connectionMade ( self ) : <EOL> if self . terminalFactory is not None : <EOL> self . terminal = self . terminalFactory ( * self . terminalArgs , ** self . terminalKwArgs ) <EOL> self . terminal . factory = self . factory <EOL> self . terminal . makeConnection ( self ) <EOL> def connectionLost ( self , reason ) : <EOL> if self . terminal is not None : <EOL> try : <EOL> self . terminal . connectionLost ( reason ) <EOL> finally : <EOL> del self . terminal <EOL> def dataReceived ( self , bytes ) : <EOL> """<STR_LIT>""" <EOL> toWrite = [ ] <EOL> for b in bytes : <EOL> if self . state == '<STR_LIT:data>' : <EOL> if b == '<STR_LIT>' : <EOL> if toWrite : <EOL> self . terminal . write ( '<STR_LIT>' . join ( toWrite ) ) <EOL> del toWrite [ : ] <EOL> self . state = '<STR_LIT>' <EOL> elif b == '<STR_LIT>' : <EOL> if toWrite : <EOL> self . terminal . write ( '<STR_LIT>' . join ( toWrite ) ) <EOL> del toWrite [ : ] <EOL> self . terminal . shiftOut ( ) <EOL> elif b == '<STR_LIT>' : <EOL> if toWrite : <EOL> self . terminal . write ( '<STR_LIT>' . join ( toWrite ) ) <EOL> del toWrite [ : ] <EOL> self . terminal . shiftIn ( ) <EOL> elif b == '<STR_LIT>' : <EOL> if toWrite : <EOL> self . terminal . write ( '<STR_LIT>' . join ( toWrite ) ) <EOL> del toWrite [ : ] <EOL> self . terminal . cursorBackward ( ) <EOL> else : <EOL> toWrite . append ( b ) <EOL> elif self . state == '<STR_LIT>' : <EOL> fName = self . _shorts . get ( b ) <EOL> if fName is not None : <EOL> self . state = '<STR_LIT:data>' <EOL> getattr ( self . terminal , fName ) ( ) <EOL> else : <EOL> state = self . _longs . get ( b ) <EOL> if state is not None : <EOL> self . state = state <EOL> else : <EOL> self . terminal . unhandledControlSequence ( '<STR_LIT>' + b ) <EOL> self . state = '<STR_LIT:data>' <EOL> elif self . state == '<STR_LIT>' : <EOL> if self . _escBuf is None : <EOL> self . _escBuf = [ ] <EOL> if b . isalpha ( ) or b == '<STR_LIT>' : <EOL> self . _handleControlSequence ( '<STR_LIT>' . join ( self . _escBuf ) , b ) <EOL> del self . _escBuf <EOL> self . state = '<STR_LIT:data>' <EOL> else : <EOL> self . _escBuf . append ( b ) <EOL> elif self . state == '<STR_LIT>' : <EOL> self . terminal . selectCharacterSet ( self . _charsets . get ( b , b ) , G0 ) <EOL> self . state = '<STR_LIT:data>' <EOL> elif self . state == '<STR_LIT>' : <EOL> self . terminal . selectCharacterSet ( self . _charsets . get ( b , b ) , G1 ) <EOL> self . state = '<STR_LIT:data>' <EOL> elif self . state == '<STR_LIT>' : <EOL> self . _handleHeightWidth ( b ) <EOL> self . state = '<STR_LIT:data>' <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if toWrite : <EOL> self . terminal . write ( '<STR_LIT>' . join ( toWrite ) ) <EOL> def _handleControlSequence ( self , buf , terminal ) : <EOL> f = getattr ( self . controlSequenceParser , CST . get ( terminal , terminal ) , None ) <EOL> if f is None : <EOL> self . terminal . unhandledControlSequence ( '<STR_LIT>' + buf + terminal ) <EOL> else : <EOL> f ( self , self . terminal , buf ) <EOL> class ControlSequenceParser : <EOL> def _makeSimple ( ch , fName ) : <EOL> n = '<STR_LIT>' + fName <EOL> def simple ( self , proto , handler , buf ) : <EOL> if not buf : <EOL> getattr ( handler , n ) ( <NUM_LIT:1> ) <EOL> else : <EOL> try : <EOL> m = int ( buf ) <EOL> except ValueError : <EOL> handler . unhandledControlSequence ( '<STR_LIT>' + buf + ch ) <EOL> else : <EOL> getattr ( handler , n ) ( m ) <EOL> return simple <EOL> for ( ch , fName ) in ( ( '<STR_LIT:A>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:B>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:C>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:D>' , '<STR_LIT>' ) ) : <EOL> exec ch + "<STR_LIT>" <EOL> del _makeSimple <EOL> def h ( self , proto , handler , buf ) : <EOL> try : <EOL> modes = map ( int , buf . split ( '<STR_LIT:;>' ) ) <EOL> except ValueError : <EOL> handler . unhandledControlSequence ( '<STR_LIT>' + buf + '<STR_LIT:h>' ) <EOL> else : <EOL> handler . setModes ( modes ) <EOL> def l ( self , proto , handler , buf ) : <EOL> try : <EOL> modes = map ( int , buf . split ( '<STR_LIT:;>' ) ) <EOL> except ValueError : <EOL> handler . unhandledControlSequence ( '<STR_LIT>' + buf + '<STR_LIT:l>' ) <EOL> else : <EOL> handler . resetModes ( modes ) <EOL> def r ( self , proto , handler , buf ) : <EOL> parts = buf . split ( '<STR_LIT:;>' ) <EOL> if len ( parts ) == <NUM_LIT:1> : <EOL> handler . setScrollRegion ( None , None ) <EOL> elif len ( parts ) == <NUM_LIT:2> : <EOL> try : <EOL> if parts [ <NUM_LIT:0> ] : <EOL> pt = int ( parts [ <NUM_LIT:0> ] ) <EOL> else : <EOL> pt = None <EOL> if parts [ <NUM_LIT:1> ] : <EOL> pb = int ( parts [ <NUM_LIT:1> ] ) <EOL> else : <EOL> pb = None <EOL> except ValueError : <EOL> handler . unhandledControlSequence ( '<STR_LIT>' + buf + '<STR_LIT:r>' ) <EOL> else : <EOL> handler . setScrollRegion ( pt , pb ) <EOL> else : <EOL> handler . unhandledControlSequence ( '<STR_LIT>' + buf + '<STR_LIT:r>' ) <EOL> def K ( self , proto , handler , buf ) : <EOL> if not buf : <EOL> handler . eraseToLineEnd ( ) <EOL> elif buf == '<STR_LIT:1>' : <EOL> handler . eraseToLineBeginning ( ) <EOL> elif buf == '<STR_LIT:2>' : <EOL> handler . eraseLine ( ) <EOL> else : <EOL> handler . unhandledControlSequence ( '<STR_LIT>' + buf + '<STR_LIT>' ) <EOL> def H ( self , proto , handler , buf ) : <EOL> handler . cursorHome ( ) <EOL> def J ( self , proto , handler , buf ) : <EOL> if not buf : <EOL> handler . eraseToDisplayEnd ( ) <EOL> elif buf == '<STR_LIT:1>' : <EOL> handler . eraseToDisplayBeginning ( ) <EOL> elif buf == '<STR_LIT:2>' : <EOL> handler . eraseDisplay ( ) <EOL> else : <EOL> handler . unhandledControlSequence ( '<STR_LIT>' + buf + '<STR_LIT>' ) <EOL> def P ( self , proto , handler , buf ) : <EOL> if not buf : <EOL> handler . deleteCharacter ( <NUM_LIT:1> ) <EOL> else : <EOL> try : <EOL> n = int ( buf ) <EOL> except ValueError : <EOL> handler . unhandledControlSequence ( '<STR_LIT>' + buf + '<STR_LIT:P>' ) <EOL> else : <EOL> handler . deleteCharacter ( n ) <EOL> def L ( self , proto , handler , buf ) : <EOL> if not buf : <EOL> handler . insertLine ( <NUM_LIT:1> ) <EOL> else : <EOL> try : <EOL> n = int ( buf ) <EOL> except ValueError : <EOL> handler . unhandledControlSequence ( '<STR_LIT>' + buf + '<STR_LIT:L>' ) <EOL> else : <EOL> handler . insertLine ( n ) <EOL> def M ( self , proto , handler , buf ) : <EOL> if not buf : <EOL> handler . deleteLine ( <NUM_LIT:1> ) <EOL> else : <EOL> try : <EOL> n = int ( buf ) <EOL> except ValueError : <EOL> handler . unhandledControlSequence ( '<STR_LIT>' + buf + '<STR_LIT:M>' ) <EOL> else : <EOL> handler . deleteLine ( n ) <EOL> def n ( self , proto , handler , buf ) : <EOL> if buf == '<STR_LIT>' : <EOL> x , y = handler . reportCursorPosition ( ) <EOL> proto . transport . write ( '<STR_LIT>' % ( x + <NUM_LIT:1> , y + <NUM_LIT:1> ) ) <EOL> else : <EOL> handler . unhandledControlSequence ( '<STR_LIT>' + buf + '<STR_LIT:n>' ) <EOL> def m ( self , proto , handler , buf ) : <EOL> if not buf : <EOL> handler . selectGraphicRendition ( NORMAL ) <EOL> else : <EOL> attrs = [ ] <EOL> for a in buf . split ( '<STR_LIT:;>' ) : <EOL> try : <EOL> a = int ( a ) <EOL> except ValueError : <EOL> pass <EOL> attrs . append ( a ) <EOL> handler . selectGraphicRendition ( * attrs ) <EOL> controlSequenceParser = ControlSequenceParser ( ) <EOL> def _handleHeightWidth ( self , b ) : <EOL> if b == '<STR_LIT:3>' : <EOL> self . terminal . doubleHeightLine ( True ) <EOL> elif b == '<STR_LIT:4>' : <EOL> self . terminal . doubleHeightLine ( False ) <EOL> elif b == '<STR_LIT:5>' : <EOL> self . terminal . singleWidthLine ( ) <EOL> elif b == '<STR_LIT>' : <EOL> self . terminal . doubleWidthLine ( ) <EOL> else : <EOL> self . terminal . unhandledControlSequence ( '<STR_LIT>' + b ) <EOL> __all__ = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] </s>
