U  W[*ã@s¾dZddlmZddlmZmZddlmZddlm Z dd„Z dd „Z d d „Z d d „Z Gdd„dƒZGdd„dejƒZGdd„deƒZGdd„deƒZGdd„dejƒZdd„Zdd„ZdS)zE Dict client protocol implementation. @author: Pavel Pergamenshchik é)Úbasic)ÚdeferÚprotocol)Úlog)ÚBytesIOcCsê|dkr dS|dd…dkr"d}nd}d}t|ƒ}|dkrD| d¡| d¡}|dkrv|dkrÜ| d¡|| ¡fSnf|dkrš| d¡}|dkrÜd|fSnB|dkrÀ|dkr¶|| ¡fSd|fSn|d krÜ|dkrÜ|| ¡fS||7}qDdS) zQChew one dqstring or atom from beginning of line and return (param, remaningline)ó)Nrréó"éó\Nó )rÚread)ÚlineÚmodeÚresÚioÚa©rú8/usr/lib/python3/dist-packages/twisted/protocols/dict.pyÚ parseParams4        rcCstdd„|ƒS)zMunch a string into an 'atom'cSs|tttdƒdddgƒkS)Né!é"é'é\)ÚmapÚchrÚrange)ÚxrrrÚ5rzmakeAtom..)Úfilter©rrrrÚmakeAtom2sr!cCsNtdƒdddg}g}|D]$}t|ƒ|kr4| d¡| |¡qd |¡}|S)Nrrrrr r)rÚordÚappendÚjoin)ÚsZ mustquoteÚresultÚcrrrÚmakeWord9s    r(cCsHt|ƒdkr|dkrdSt|ƒdkr@|dd…dkr@|dd…}|SdS)Nró.rr s..)Úlenr rrrÚ parseTextEs  r+c@seZdZdZdd„ZdS)Ú DefinitionzA word definitioncCs||_||_||_||_dS©N)ÚnameÚdbÚdbdescÚtext)Úselfr.r/r0r1rrrÚ__init__QszDefinition.__init__N)Ú__name__Ú __module__Ú __qualname__Ú__doc__r3rrrrr,Osr,c@seZdZdZdZdZdZdZdZdZ dd„Z dd„Z dd „Z d d „Z d d „Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zdd„Zd d!„Zd"d#„Zd$d%„Zd&d'„Zd(d)„Zd*d+„Zd,d-„Zd.d/„Zd0d1„Z d2d3„Z!d4d5„Z"d6d7„Z#d8d9„Z$d:d;„Z%dd?„Z'dS)@Ú DictClientzdict (RFC2229) clientNicCsd|_d|_dSr-)Údatar&©r2rrrr3cszDictClient.__init__cCsd|_d|_dS)NZconnÚcommand)Ústaterr:rrrÚconnectionMadehszDictClient.connectionMadecCs,t|ƒ|jdkrtdƒ‚tj ||¡dS)z3Throw up if the line is longer than 1022 charactersr z(DictClient tried to send a too long lineN)r*Ú MAX_LENGTHÚ ValueErrorrÚ LineReceiverÚsendLine©r2rrrrrAmszDictClient.sendLinecCs¨z| d¡}Wntk r$YdSX|jdkr6d}nNt|ƒdkrht d|¡| d¡|j ¡dSt |dd…ƒ}|dd…}t |d||j f|j ƒ}||ƒdS)Nzutf-8r1éz-DictClient got invalid line from server -- %szInvalid line from serverézdictCode_%s_%s) ÚdecodeÚ UnicodeErrorrr*rÚmsgÚ protocolErrorÚ transportZLoseConnectionÚintÚgetattrr<ÚdictCode_default)r2rÚcodeÚmethodrrrÚ lineReceivedts     zDictClient.lineReceivedcCs&t d|¡| d¡|j ¡dS)zUnknown messagez3DictClient got unexpected message from server -- %szUnexpected server messageN)rrGrHrIÚloseConnectionrBrrrrL‡s zDictClient.dictCode_defaultcCsdS)z*We are about to get kicked off, do nothingNrrBrrrÚdictCode_221_readyŽszDictClient.dictCode_221_readycCsd|_| ¡dS)zGreeting messageÚreadyN)r<Ú dictConnectedrBrrrÚdictCode_220_conn“szDictClient.dictCode_220_conncCs| d¡|j ¡dS)Nz Access denied©rHrIrPr:rrrÚdictCode_530_conn™s zDictClient.dictCode_530_conncCs| d¡|j ¡dS)NzServer temporarily unavailablerUr:rrrÚdictCode_420_connžs zDictClient.dictCode_420_conncCs| d¡|j ¡dS)Nz(Server shutting down at operator requestrUr:rrrÚdictCode_421_conn£s zDictClient.dictCode_421_conncCsR|jdkstdƒ‚d|_d|_d|_dt| d¡ƒt| d¡ƒf}| |¡dS)zSend a dict DEFINE commandrRz4DictClient.sendDefine called when not in ready stateNÚdefinez DEFINE %s %súUTF-8)r<ÚAssertionErrorr&r9r!Úencoder(rA)r2ÚdatabaseÚwordr;rrrÚ sendDefine¨s  zDictClient.sendDefinecCsR|jdkstdƒ‚d|_d|_d|_dt|ƒt|ƒt|ƒf}| | d¡¡dS)zSend a dict MATCH commandrRz3DictClient.sendMatch called when not in ready stateNÚmatchzMATCH %s %s %srZ)r<r[r&r9r!rAr\)r2r]Ústrategyr^r;rrrÚ sendMatch²s zDictClient.sendMatchcCsd|_| d¡dS©zInvalid databaserRN©rÚ defineFailedrBrrrÚdictCode_550_define»szDictClient.dictCode_550_definecCsd|_| d¡dSrc©rÚ matchFailedrBrrrÚdictCode_550_matchÁszDictClient.dictCode_550_matchcCsd|_| d¡dS)zInvalid strategyrRNrgrBrrrÚdictCode_551_matchÇszDictClient.dictCode_551_matchcCsd|_| d¡dS©zNo matchrRNrdrBrrrÚdictCode_552_defineÍszDictClient.dictCode_552_definecCsd|_| d¡dSrkrgrBrrrÚdictCode_552_matchÓszDictClient.dictCode_552_matchcCs g|_dS)zn definitions retrievedN)r&rBrrrÚdictCode_150_defineÙszDictClient.dictCode_150_definecCsld|_t|ƒ\}}t|ƒ\}}t|ƒ\}}|r6|r6|sL| d¡|j ¡n|j t|||gƒ¡g|_dS)zDefinition text followsr1zInvalid server responseN) rrrHrIrPr&r#r,r9)r2rr^r/r0rrrÚdictCode_151_defineÞs      zDictClient.dictCode_151_definecCsd|_g|_g|_dS)zn matches found, text followsr1N)rr&r9rBrrrÚdictCode_152_matchìszDictClient.dictCode_152_matchcCs<t|ƒ}|dkr,d|_|j|jd_d|_n |j |¡dS)z"A line of definition text receivedNr;éÿÿÿÿ)r+rr9r&r1r#)r2rrrrrÚdictCode_text_defineós zDictClient.dictCode_text_definecCsDdd„}t|ƒ}|dkr4d|_t||jƒ|_d|_n |j |¡dS)zOne line of match text receivedcSs t|ƒ\}}t|ƒ\}}||fSr-)r)r%Zp1ÚtZp2rrrÚls  z)DictClient.dictCode_text_match..lNr;)r+rrr9r&r#)r2rrtrrrrÚdictCode_text_matchþszDictClient.dictCode_text_matchcCs |j}d|_d|_| |¡dS©ÚokNrR)r&r<Ú defineDone©r2rrsrrrÚdictCode_250_define szDictClient.dictCode_250_definecCs |j}d|_d|_| |¡dSrv)r&r<Ú matchDoneryrrrÚdictCode_250_matchszDictClient.dictCode_250_matchcCsdS)z5override to catch unexpected dict protocol conditionsNr©r2ÚreasonrrrrHszDictClient.protocolErrorcCsdS)zCoverride to be notified when the server is ready to accept commandsNrr:rrrrS"szDictClient.dictConnectedcCsdS)z8override to catch reasonable failure responses to DEFINENrr}rrrre'szDictClient.defineFailedcCsdS)z#override to catch successful DEFINENr©r2r&rrrrx,szDictClient.defineDonecCsdS)z6override to catch resonable failure responses to MATCHNrr}rrrrh1szDictClient.matchFailedcCsdS)z"override to catch successful MATCHNrrrrrr{6szDictClient.matchDone)(r4r5r6r7r9r>r<rr&Úfactoryr3r=rArOrLrQrTrVrWrXr_rbrfrirjrlrmrnrorprrrurzr|rHrSrerxrhr{rrrrr8YsJ   r8c@s eZdZdS)ÚInvalidResponseN)r4r5r6rrrrr<src@s@eZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dS)Ú DictLookupzNUtility class for a single dict transaction. To be used with DictLookupFactorycCs(|jjs$|jj t|ƒ¡|j ¡dSr-)r€ÚdoneÚdÚerrbackrÚ clientDoner}rrrrHDszDictLookup.protocolErrorcCs>|jjdkrt|j|jjƒn|jjdkr:t|j|jjƒdS)NrYr`)r€Ú queryTypeZapplyr_Úparamrbr:rrrrSJs  zDictLookup.dictConnectedcCs&|jj g¡|j ¡|j ¡dSr-©r€r„Úcallbackr†rIrPr}rrrreQs zDictLookup.defineFailedcCs&|jj |¡|j ¡|j ¡dSr-r‰rrrrrxWs zDictLookup.defineDonecCs&|jj g¡|j ¡|j ¡dSr-r‰r}rrrrh]s zDictLookup.matchFailedcCs&|jj |¡|j ¡|j ¡dSr-r‰rrrrr{cs zDictLookup.matchDoneN) r4r5r6r7rHrSrerxrhr{rrrrr‚Asr‚c@s@eZdZdZeZdZdd„Zdd„Zdd„Z d d „Z d d „Z dS) ÚDictLookupFactoryz-Utility factory for a single dict transactionNcCs||_||_||_d|_dS)Nr)r‡rˆr„rƒ)r2r‡rˆr„rrrr3oszDictLookupFactory.__init__cCsd|_|`dS)zCalled by client when done.rN)rƒr„r:rrrr†vszDictLookupFactory.clientDonecCs|j |¡dSr-)r„r…©r2Z connectorÚerrorrrrÚclientConnectionFailed|sz(DictLookupFactory.clientConnectionFailedcCs|js|j |¡dSr-)rƒr„r…rŒrrrÚclientConnectionLost€sz&DictLookupFactory.clientConnectionLostcCs| ¡}||_|Sr-)rr€)r2ZaddrÚprrrÚ buildProtocol…szDictLookupFactory.buildProtocol) r4r5r6r7r‚rrƒr3r†rŽrr‘rrrrr‹jsr‹cCs6t ¡}td||f|ƒ}ddlm}| |||¡|S)z"Look up a word using a dict serverrYr©Úreactor©rZDeferredr‹Útwisted.internetr“Z connectTCP)ÚhostÚportr]r^r„r€r“rrrrYŒs  rYcCs8t ¡}td|||f|ƒ}ddlm}| |||¡|S)z Match a word using a dict serverr`rr’r”)r–r—r]rar^r„r€r“rrrr`—s  r`N)r7Ztwisted.protocolsrr•rrZtwisted.pythonrrrrr!r(r+r,r@r8Ú Exceptionrr‚Z ClientFactoryr‹rYr`rrrrÚs    "   d)"