B
    0²ô`©*  ã               @   s¾   d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	 dd„ Z
dd	„ Zd
d„ Zdd„ ZG dd„ dƒZG dd„ dejƒZG dd„ deƒZG dd„ deƒZG dd„ dejƒZdd„ Zdd„ ZdS )zE
Dict client protocol implementation.

@author: Pavel Pergamenshchik
é    )Úbasic)ÚdeferÚprotocol)Úlog)ÚBytesIOc             C   sî   | dkrdS | dd… dkr"d}nd}d}t | ƒ}|dkrD| d¡ x¤| d¡}|dkrx|dkrÞ| d¡ || ¡ fS nf|dkrœ| d¡}|dkrÞd| fS nB|dkrÂ|dkr¸|| ¡ fS d| fS n|d	krÞ|dkrÞ|| ¡ fS ||7 }qFW dS )
zQChew one dqstring or atom from beginning of line and return (param, remaningline)ó    )Nr   r   é   ó   "é   ó   \Nó    )r   Úread)ÚlineÚmodeÚresÚioÚa© r   úE/home/dcms/DCMS/lib/python3.7/site-packages/twisted/protocols/dict.pyÚ
parseParam   s6    





r   c             C   s   t dd„ | ƒS )zMunch a string into an 'atom'c             S   s   | t ttdƒdddg ƒkS )Né!   é"   é'   é\   )ÚmapÚchrÚrange)Úxr   r   r   Ú<lambda>5   r   zmakeAtom.<locals>.<lambda>)Úfilter)r   r   r   r   ÚmakeAtom2   s    r    c             C   sR   t dƒdddg }g }x,| D ]$}t|ƒ|kr6| d¡ | |¡ qW d |¡} | S )Nr   r   r   r   r   r   )r   ÚordÚappendÚjoin)ÚsZ	mustquoteÚresultÚcr   r   r   ÚmakeWord8   s    


r'   c             C   sH   t | ƒdkr| dkrd S t | ƒdkr@| dd… dkr@| dd … } | S d S )Nr   ó   .r   r
   s   ..)Úlen)r   r   r   r   Ú	parseTextC   s
    r*   c               @   s   e Zd ZdZdd„ ZdS )Ú
DefinitionzA word definitionc             C   s   || _ || _|| _|| _d S )N)ÚnameÚdbÚdbdescÚtext)Úselfr,   r-   r.   r/   r   r   r   Ú__init__O   s    zDefinition.__init__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r1   r   r   r   r   r+   L   s   r+   c               @   s  e Zd ZdZdZdZdZdZdZdZ	dd„ Z
dd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(d)„ Zd*d+„ Zd,d-„ Zd.d/„ Zd0d1„ Z d2d3„ Z!d4d5„ Z"d6d7„ Z#d8d9„ Z$d:d;„ Z%d<d=„ Z&d>d?„ Z'dS )@Ú
DictClientzdict (RFC2229) clientNi   c             C   s   d | _ d | _d S )N)Údatar%   )r0   r   r   r   r1   `   s    zDictClient.__init__c             C   s   d| _ d| _d S )NÚconnÚcommand)Ústater   )r0   r   r   r   ÚconnectionMaded   s    zDictClient.connectionMadec             C   s,   t |ƒ| jd krtdƒ‚tj | |¡ dS )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)r0   r   r   r   r   r?   h   s    zDictClient.sendLinec             C   s¦   y|  d¡}W n tk
r"   d S X | jdkr4d}nNt|ƒdk rft d| ¡ |  d¡ | j ¡  d S t	|d d… ƒ}|dd … }t
| d || j¡| jƒ}||ƒ d S )Nzutf-8r/   é   z-DictClient got invalid line from server -- %szInvalid line from serveré   zdictCode_{}_{})ÚdecodeÚUnicodeErrorr   r)   r   ÚmsgÚprotocolErrorÚ	transportZLoseConnectionÚintÚgetattrÚformatr:   ÚdictCode_default)r0   r   ÚcodeÚmethodr   r   r   ÚlineReceivedn   s     


zDictClient.lineReceivedc             C   s&   t  d| ¡ |  d¡ | j ¡  dS )zUnknown messagez3DictClient got unexpected message from server -- %szUnexpected server messageN)r   rD   rE   rF   ÚloseConnection)r0   r   r   r   r   rJ   ‚   s    
zDictClient.dictCode_defaultc             C   s   dS )z*We are about to get kicked off, do nothingNr   )r0   r   r   r   r   ÚdictCode_221_readyˆ   s    zDictClient.dictCode_221_readyc             C   s   d| _ |  ¡  dS )zGreeting messageÚreadyN)r:   ÚdictConnected)r0   r   r   r   r   ÚdictCode_220_connŒ   s    zDictClient.dictCode_220_connc             C   s   |   d¡ | j ¡  d S )NzAccess denied)rE   rF   rN   )r0   r   r   r   ÚdictCode_530_conn‘   s    
zDictClient.dictCode_530_connc             C   s   |   d¡ | j ¡  d S )NzServer temporarily unavailable)rE   rF   rN   )r0   r   r   r   ÚdictCode_420_conn•   s    
zDictClient.dictCode_420_connc             C   s   |   d¡ | j ¡  d S )Nz(Server shutting down at operator request)rE   rF   rN   )r0   r   r   r   ÚdictCode_421_conn™   s    
zDictClient.dictCode_421_connc             C   sR   | j dkstdƒ‚d| _d| _d| _ d t| d¡ƒt| d¡ƒ¡}|  |¡ dS )zSend a dict DEFINE commandrP   z4DictClient.sendDefine called when not in ready stateNÚdefinezDEFINE {} {}zUTF-8)	r:   ÚAssertionErrorr%   r7   rI   r    Úencoder'   r?   )r0   ÚdatabaseÚwordr9   r   r   r   Ú
sendDefine   s    zDictClient.sendDefinec             C   sR   | j dkstdƒ‚d| _d| _d| _ d t|ƒt|ƒt|ƒ¡}|  | d¡¡ dS )zSend a dict MATCH commandrP   z3DictClient.sendMatch called when not in ready stateNÚmatchzMATCH {} {} {}zUTF-8)r:   rW   r%   r7   rI   r    r?   rX   )r0   rY   ÚstrategyrZ   r9   r   r   r   Ú	sendMatch­   s    
zDictClient.sendMatchc             C   s   d| _ |  d ¡ dS )zInvalid databaserP   N)r   ÚdefineFailed)r0   r   r   r   r   ÚdictCode_550_define¼   s    zDictClient.dictCode_550_definec             C   s   d| _ |  d ¡ dS )zInvalid databaserP   N)r   ÚmatchFailed)r0   r   r   r   r   ÚdictCode_550_matchÁ   s    zDictClient.dictCode_550_matchc             C   s   d| _ |  d ¡ dS )zInvalid strategyrP   N)r   ra   )r0   r   r   r   r   ÚdictCode_551_matchÆ   s    zDictClient.dictCode_551_matchc             C   s   d| _ |  d ¡ dS )zNo matchrP   N)r   r_   )r0   r   r   r   r   ÚdictCode_552_defineË   s    zDictClient.dictCode_552_definec             C   s   d| _ |  d ¡ dS )zNo matchrP   N)r   ra   )r0   r   r   r   r   ÚdictCode_552_matchÐ   s    zDictClient.dictCode_552_matchc             C   s
   g | _ dS )zn definitions retrievedN)r%   )r0   r   r   r   r   ÚdictCode_150_defineÕ   s    zDictClient.dictCode_150_definec             C   sl   d| _ t|ƒ\}}t|ƒ\}}t|ƒ\}}|r6|r6|sL|  d¡ | j ¡  n| j t|||g ƒ¡ g | _dS )zDefinition text followsr/   zInvalid server responseN)	r   r   rE   rF   rN   r%   r"   r+   r7   )r0   r   rZ   r-   r.   r   r   r   ÚdictCode_151_defineÙ   s    
zDictClient.dictCode_151_definec             C   s   d| _ g | _g | _dS )zn matches found, text followsr/   N)r   r%   r7   )r0   r   r   r   r   ÚdictCode_152_matchæ   s    zDictClient.dictCode_152_matchc             C   s<   t |ƒ}|dkr,d| _| j| jd _d| _n| j |¡ dS )z"A line of definition text receivedNr9   éÿÿÿÿ)r*   r   r7   r%   r/   r"   )r0   r   r   r   r   r   ÚdictCode_text_defineì   s    zDictClient.dictCode_text_definec             C   sD   dd„ }t |ƒ}|dkr4d| _t|| jƒ| _d| _n| j |¡ dS )zOne line of match text receivedc             S   s    t | ƒ\}}t |ƒ\}}||fS )N)r   )r$   Úp1ÚtÚp2r   r   r   Úlù   s    z)DictClient.dictCode_text_match.<locals>.lNr9   )r*   r   r   r7   r%   r"   )r0   r   rn   r   r   r   r   ÚdictCode_text_matchö   s    zDictClient.dictCode_text_matchc             C   s    | j }d| _ d| _|  |¡ dS )ÚokNrP   )r%   r:   Ú
defineDone)r0   r   rl   r   r   r   ÚdictCode_250_define  s    zDictClient.dictCode_250_definec             C   s    | j }d| _ d| _|  |¡ dS )rp   NrP   )r%   r:   Ú	matchDone)r0   r   rl   r   r   r   ÚdictCode_250_match  s    zDictClient.dictCode_250_matchc             C   s   dS )z5override to catch unexpected dict protocol conditionsNr   )r0   Úreasonr   r   r   rE     s    zDictClient.protocolErrorc             C   s   dS )zCoverride to be notified when the server is ready to accept commandsNr   )r0   r   r   r   rQ     s    zDictClient.dictConnectedc             C   s   dS )z8override to catch reasonable failure responses to DEFINENr   )r0   ru   r   r   r   r_     s    zDictClient.defineFailedc             C   s   dS )z#override to catch successful DEFINENr   )r0   r%   r   r   r   rq      s    zDictClient.defineDonec             C   s   dS )z6override to catch resonable failure responses to MATCHNr   )r0   ru   r   r   r   ra   $  s    zDictClient.matchFailedc             C   s   dS )z"override to catch successful MATCHNr   )r0   r%   r   r   r   rs   (  s    zDictClient.matchDone)(r2   r3   r4   r5   r7   r<   r:   r   r%   Úfactoryr1   r;   r?   rM   rJ   rO   rR   rS   rT   rU   r[   r^   r`   rb   rc   rd   re   rf   rg   rh   rj   ro   rr   rt   rE   rQ   r_   rq   ra   rs   r   r   r   r   r6   V   sJ   
r6   c               @   s   e Zd ZdS )ÚInvalidResponseN)r2   r3   r4   r   r   r   r   rw   -  s   rw   c               @   s@   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dS )Ú
DictLookupzNUtility class for a single dict transaction. To be used with DictLookupFactoryc             C   s(   | j js$| j j t|ƒ¡ | j  ¡  d S )N)rv   ÚdoneÚdÚerrbackrw   Ú
clientDone)r0   ru   r   r   r   rE   4  s    zDictLookup.protocolErrorc             C   s:   | j jdkr| j| j jŽ  n| j jdkr6| j| j jŽ  d S )NrV   r\   )rv   Ú	queryTyper[   Úparamr^   )r0   r   r   r   rQ   9  s    zDictLookup.dictConnectedc             C   s&   | j j g ¡ | j  ¡  | j ¡  d S )N)rv   rz   Úcallbackr|   rF   rN   )r0   ru   r   r   r   r_   ?  s    
zDictLookup.defineFailedc             C   s&   | j j |¡ | j  ¡  | j ¡  d S )N)rv   rz   r   r|   rF   rN   )r0   r%   r   r   r   rq   D  s    
zDictLookup.defineDonec             C   s&   | j j g ¡ | j  ¡  | j ¡  d S )N)rv   rz   r   r|   rF   rN   )r0   ru   r   r   r   ra   I  s    
zDictLookup.matchFailedc             C   s&   | j j |¡ | j  ¡  | j ¡  d S )N)rv   rz   r   r|   rF   rN   )r0   r%   r   r   r   rs   N  s    
zDictLookup.matchDoneN)
r2   r3   r4   r5   rE   rQ   r_   rq   ra   rs   r   r   r   r   rx   1  s   rx   c               @   s@   e Zd ZdZeZdZdd„ Zdd„ Zdd„ Z	d	d
„ Z
dd„ ZdS )ÚDictLookupFactoryz-Utility factory for a single dict transactionNc             C   s   || _ || _|| _d| _d S )Nr   )r}   r~   rz   ry   )r0   r}   r~   rz   r   r   r   r1   Z  s    zDictLookupFactory.__init__c             C   s   d| _ | `dS )zCalled by client when done.r   N)ry   rz   )r0   r   r   r   r|   `  s    zDictLookupFactory.clientDonec             C   s   | j  |¡ d S )N)rz   r{   )r0   Ú	connectorÚerrorr   r   r   ÚclientConnectionFailede  s    z(DictLookupFactory.clientConnectionFailedc             C   s   | j s| j |¡ d S )N)ry   rz   r{   )r0   r   r‚   r   r   r   ÚclientConnectionLosth  s    z&DictLookupFactory.clientConnectionLostc             C   s   |   ¡ }| |_|S )N)r   rv   )r0   ÚaddrÚpr   r   r   ÚbuildProtocoll  s    zDictLookupFactory.buildProtocol)r2   r3   r4   r5   rx   r   ry   r1   r|   rƒ   r„   r‡   r   r   r   r   r€   T  s   r€   c             C   s6   t  ¡ }td||f|ƒ}ddlm} | | ||¡ |S )z"Look up a word using a dict serverrV   r   )Úreactor)r   ÚDeferredr€   Útwisted.internetrˆ   Ú
connectTCP)ÚhostÚportrY   rZ   rz   rv   rˆ   r   r   r   rV   r  s
    rV   c             C   s8   t  ¡ }td|||f|ƒ}ddlm} | | ||¡ |S )z Match a word using a dict serverr\   r   )rˆ   )r   r‰   r€   rŠ   rˆ   r‹   )rŒ   r   rY   r]   rZ   rz   rv   rˆ   r   r   r   r\   }  s
    r\   N)r5   Ztwisted.protocolsr   rŠ   r   r   Ztwisted.pythonr   r   r   r   r    r'   r*   r+   r>   r6   Ú	Exceptionrw   rx   ZClientFactoryr€   rV   r\   r   r   r   r   Ú<module>	   s    !	
 X#