o b*@sdZddlmZddlmZmZddlmZddlm Z ddZ dd Z d d Z d d Z GdddZGdddejZGdddeZGdddeZGdddejZddZddZdS)zE Dict client protocol implementation. @author: Pavel Pergamenshchik )BytesIO)deferprotocol)basic)logcCs|dkrdS|dddkrd}nd}d}t|}|dkr"|d |d}|dkr<|dkr;|d||fSn2|dkrN|d}|dkrMd|fSn |dkr`|dkr\||fSd|fS|d krn|dkrn||fS||7}q#) zQChew one dqstring or atom from beginning of line and return (param, remaningline))Nrr"\N )rread)linemoderesioar8/usr/lib/python3/dist-packages/twisted/protocols/dict.py parseParams<       rcCstdd|S)zMunch a string into an 'atom'cSs|tttdgdvS)N!"'\)mapchrrange)xrrr6szmakeAtom..)filterrrrrmakeAtom3sr"cCsLtdgd}g}|D]}t||vr|d||q d|}|S)Nrrr r)rordappendjoin)s mustquoteresultcrrrmakeWord9s    r*cCsDt|dkr |dkr dSt|dkr |dddkr |dd}|S)Nr.rr s..)lenr!rrr parseTextDs  r-c@seZdZdZddZdS) DefinitionzA word definitioncCs||_||_||_||_dSN)namedbdbdesctext)selfr0r1r2r3rrr__init__P zDefinition.__init__N)__name__ __module__ __qualname____doc__r5rrrrr.Ms r.c@seZdZdZdZdZdZdZdZdZ ddZ ddZ dd Z d d Z d d ZddZddZddZddZddZddZddZddZddZd d!Zd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1Z d2d3Z!d4d5Z"d6d7Z#d8d9Z$d:d;Z%dd?Z'dS)@ DictClientzdict (RFC2229) clientNicCsd|_d|_dSr/)datar(r4rrrr5a zDictClient.__init__cCsd|_d|_dS)Nconncommand)staterr=rrrconnectionMadeer>zDictClient.connectionMadecCs,t||jdkr tdtj||dS)z3Throw up if the line is longer than 1022 charactersr z(DictClient tried to send a too long lineN)r, MAX_LENGTH ValueErrorr LineReceiversendLiner4rrrrrFiszDictClient.sendLinecCsz|d}Wn tyYdSw|jdkrd}n't|dkr3td||d|jdSt |dd}|dd}t |d|d|j |j }||dS) Nzutf-8r3z-DictClient got invalid line from server -- %szInvalid line from server dictCode__) decode UnicodeErrorrr,rmsg protocolError transportLoseConnectionintgetattrrAdictCode_default)r4rcodemethodrrr lineReceivedos        zDictClient.lineReceivedcCs&td||d|jdS)zUnknown messagez3DictClient got unexpected message from server -- %szUnexpected server messageN)rrNrOrPloseConnectionrGrrrrTs zDictClient.dictCode_defaultcCdS)z*We are about to get kicked off, do nothingNrrGrrrdictCode_221_readyzDictClient.dictCode_221_readycCsd|_|dS)zGreeting messagereadyN)rA dictConnectedrGrrrdictCode_220_conns zDictClient.dictCode_220_conncC|d|jdS)Nz Access deniedrOrPrXr=rrrdictCode_530_conn zDictClient.dictCode_530_conncCr_)NzServer temporarily unavailabler`r=rrrdictCode_420_connrbzDictClient.dictCode_420_conncCr_)Nz(Server shutting down at operator requestr`r=rrrdictCode_421_connrbzDictClient.dictCode_421_conncCsR|jdks Jdd|_d|_d|_dt|dt|d}||dS)zSend a dict DEFINE commandr\z4DictClient.sendDefine called when not in ready stateNdefinez DEFINE {} {}UTF-8)rAr(r<formatr"encoder*rF)r4databasewordr@rrr sendDefines   zDictClient.sendDefinecCsR|jdks Jdd|_d|_d|_dt|t|t|}||ddS)zSend a dict MATCH commandr\z3DictClient.sendMatch called when not in ready stateNmatchzMATCH {} {} {}rf)rAr(r<rgr"rFrh)r4ristrategyrjr@rrr sendMatchs zDictClient.sendMatchcCd|_|ddSzInvalid databaser\Nr defineFailedrGrrrdictCode_550_definezDictClient.dictCode_550_definecCrorpr matchFailedrGrrrdictCode_550_matchrtzDictClient.dictCode_550_matchcCro)zInvalid strategyr\NrurGrrrdictCode_551_matchrtzDictClient.dictCode_551_matchcCrozNo matchr\NrqrGrrrdictCode_552_definertzDictClient.dictCode_552_definecCroryrurGrrrdictCode_552_matchrtzDictClient.dictCode_552_matchcCs g|_dS)zn definitions retrievedN)r(rGrrrdictCode_150_defines zDictClient.dictCode_150_definecCsnd|_t|\}}t|\}}t|\}}|r|r|s'|d|jdS|jt|||gg|_dS)zDefinition text followsr3zInvalid server responseN) rrrOrPrXr(r$r.r<)r4rrjr1r2rrrdictCode_151_defines      zDictClient.dictCode_151_definecCsd|_g|_g|_dS)zn matches found, text followsr3N)rr(r<rGrrrdictCode_152_matchs zDictClient.dictCode_152_matchcCs>t|}|dkrd|_|j|jd_d|_dS|j|dS)z"A line of definition text receivedNr@)r-rr<r(r3r$)r4rrrrrdictCode_text_defines  zDictClient.dictCode_text_definecCsFdd}t|}|dkrd|_t||j|_d|_dS|j|dS)zOne line of match text receivedcSs t|\}}t|\}}||fSr/)r)r&p1tp2rrrls  z)DictClient.dictCode_text_match..lNr@)r-rrr<r(r$)r4rrrrrrdictCode_text_matchs zDictClient.dictCode_text_matchcC |j}d|_d|_||dSokNr\)r(rA defineDoner4rrrrrdictCode_250_definezDictClient.dictCode_250_definecCrr)r(rA matchDonerrrrdictCode_250_match rzDictClient.dictCode_250_matchcCrY)z5override to catch unexpected dict protocol conditionsNrr4reasonrrrrOr[zDictClient.protocolErrorcCrY)zCoverride to be notified when the server is ready to accept commandsNrr=rrrr]r[zDictClient.dictConnectedcCrY)z8override to catch reasonable failure responses to DEFINENrrrrrrrr[zDictClient.defineFailedcCrY)z#override to catch successful DEFINENrr4r(rrrrr[zDictClient.defineDonecCrY)z7override to catch reasonable failure responses to MATCHNrrrrrrv#r[zDictClient.matchFailedcCrY)z"override to catch successful MATCHNrrrrrr'r[zDictClient.matchDone)(r7r8r9r:r<rCrArr(factoryr5rBrFrWrTrZr^rarcrdrkrnrsrwrxrzr{r|r}r~rrrrrOr]rrrrvrrrrrr;WsL   r;c@s eZdZdS)InvalidResponseN)r7r8r9rrrrr,src@s@eZdZdZddZddZddZdd Zd d Zd d Z dS) DictLookupzNUtility class for a single dict transaction. To be used with DictLookupFactorycCs,|jjs|jjt||jdSdSr/)rdonederrbackr clientDonerrrrrO3szDictLookup.protocolErrorcCs@|jjdkr|j|jjdS|jjdkr|j|jjdSdS)Nrerl)r queryTyperkparamrnr=rrrr]8s  zDictLookup.dictConnectedcC&|jjg|j|jdSr/rrcallbackrrPrXrrrrrr> zDictLookup.defineFailedcC&|jj||j|jdSr/rrrrrrCrzDictLookup.defineDonecCrr/rrrrrrvHrzDictLookup.matchFailedcCrr/rrrrrrMrzDictLookup.matchDoneN) r7r8r9r:rOr]rrrrvrrrrrr0s rc@s@eZdZdZeZdZddZddZddZ d d Z d d Z dS) DictLookupFactoryz-Utility factory for a single dict transactionNcCs||_||_||_d|_dS)Nr)rrrr)r4rrrrrrr5Yr6zDictLookupFactory.__init__cCsd|_|`dS)zCalled by client when done.rN)rrr=rrrr_szDictLookupFactory.clientDonecCs|j|dSr/)rrr4 connectorerrorrrrclientConnectionFaileddsz(DictLookupFactory.clientConnectionFailedcCs|js |j|dSdSr/)rrrrrrrclientConnectionLostgsz&DictLookupFactory.clientConnectionLostcCs|}||_|Sr/)rr)r4addrprrr buildProtocolkszDictLookupFactory.buildProtocol) r7r8r9r:rrrr5rrrrrrrrrSs rcCs6t}td||f|}ddlm}|||||S)z"Look up a word using a dict serverrerreactorrDeferredrtwisted.internetr connectTCP)hostportrirjrrrrrrreqs  recCs8t}td|||f|}ddlm}|||||S)z Match a word using a dict serverrlrrr)rrrirmrjrrrrrrrl|s  rlN)r:rrrrrtwisted.protocolsrtwisted.pythonrrr"r*r-r.rEr; Exceptionrr ClientFactoryrrerlrrrrs"   !   V#