)L idZddlmZddlZddlmZmZddlmZdgZ ddZ e dZ e d Z e d Z e d Ze d Ze d Ze dZe dZe dZe dZe dZe dZe dZe d Ze d Ze dZe dZe dZe dZe dZe dZe dZe dZ e dZ!e dZ"e dZ#GddZ$y)z Parser for the Telnet protocol. (Not a complete implementation of the telnet specification, but sufficient for a command line interface.) Inspired by `Twisted.conch.telnet`. ) annotationsN)Callable Generator)loggerTelnetProtocolParserct|fSN)bytes)numbers l/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/prompt_toolkit/contrib/telnet/protocol.pyint2byters & "ceZdZdZ ddZddZddZddZddZddZ ddZ dd Z dd Z dd Z dd Zdd Zy)ra Parser for the Telnet protocol. Usage:: def data_received(data): print(data) def size_received(rows, columns): print(rows, columns) p = TelnetProtocolParser(data_received, size_received) p.feed(binary_data) c||_||_||_|j|_|jj dyr )data_received_callbacksize_received_callbackttype_received_callback_parse_coroutine_parsersend)selfr%r&r's r __init__zTelnetProtocolParser.__init__Gs> '=#&<#'>$,,.  $rc&|j|yr )r%r+datas r received_dataz"TelnetProtocolParser.received_dataTs ##D)rc0tjd|y)zReceived telnet DO command.zDO %rNrinfor.s r do_receivedz TelnetProtocolParser.do_receivedWs GT"rc0tjd|y)zReceived telnet DONT command.zDONT %rNr2r.s r dont_receivedz"TelnetProtocolParser.dont_received[ It$rc0tjd|y)zReceived telnet WILL command.zWILL %rNr2r.s r will_receivedz"TelnetProtocolParser.will_received_r7rc0tjd|y)zReceived telnet WONT command.zWONT %rNr2r.s r wont_receivedz"TelnetProtocolParser.wont_receivedcr7rc |tk(r|j|y|tk(r|j|y|tk(r|j |y|t k(r|j|ytjd||y)Nzcommand received %r %r) DOr4DONTr6WILLr9WONTr;rr3)r+commandr/s r command_receivedz%TelnetProtocolParser.command_receivedgsi b=   T " _   t $ _   t $ _   t $ KK0'4 @rct|dk(r,tjd|\}}|j||yt j dy)z5 Received NAWS. (Window dimensions.) z!HHzWrong number of NAWS bytesN)lenstructunpackr&rwarning)r+r/columnsrowss r nawszTelnetProtocolParser.nawswsB t9>#MM%6MGT  ' 'g 6 NN7 8rc|dd|dd}}|tk(r#|jd}|j|ytjdy)z) Received terminal type. rrNasciiz.Received a non-IS terminal type Subnegotiation)ISdecoder'rrH)r+r/subcmdttypes r rQzTelnetProtocolParser.ttypesIAay$qr( R<KK(E  ( ( / NNK Lrc|dd|dd}}|tk(r|j|y|tk(r|j|yt j dt |y)z% Got negotiate data. rrNzNegotiate (%r got bytes))NAWSrKTTYPErQrr3rE)r+r/rApayloads r negotiatezTelnetProtocolParser.negotiatesS !9d12h d? IIg    JJw  KK2CI >rc #hK d}|tdk(rn|tk(rd}|tk(r|j|n|ttt t tttttf vr|j|dn|tttt fvrd}|j||n}|t"k(rtg} d}|tk(r d}|t$k(rn$|j'|n|j'|?|j)dj+|n|j|.w)z_ Parser state machine. Every 'yield' expression returns the next byte. Nrr)rIACr0NOPDMBRKIPAOAYTECELGArBr=r>r?r@SBSEappendrVjoin)r+dd2d3r/d4s r r(z%TelnetProtocolParser._parse_coroutines AHQKc9&&r*CS"b#r2rBB))"c2BdD11B))"b12XD"9!&B!Rx % $ B KKONN388D>2""1%SsD0D2cZ|D]&}|jjt|(y)z* Feed data to the parser. N)r)r*r)r+r/bs r feedzTelnetProtocolParser.feeds) +A LL  hqk * +rN)r%zCallable[[bytes], None]r&zCallable[[int, int], None]r'zCallable[[str], None]returnNone)r/r rmrn)rAr r/r rmrn)rmzGenerator[None, bytes, None])__name__ __module__ __qualname____doc__r,r0r4r6r9r;rBrKrQrVr(rlrr rr8sl   7  !;  "7    *#%%%A 9 M ?.&`+r)r intrmr )%rr __future__rrFtypingrrlogr__all__rrYSGArXr=r>LINEMODErbr?r@MODErcECHOrSSUPPRESS_GO_AHEADrTSENDrNrZr[r\r]r^r_r`rarrsrr rs"# &   qkqksm c]} B< c]}}{ c]{| B<QK { a[ c]sm c] c]sm c] c] c]Y+Y+r