B
    à%Q[F  ã               @   s�   d Z ddlmZ ddlmZ ddlZddlmZ ddlm	Z	 ddl
mZmZmZmZ dd	lmZmZ dd
lmZ ddlmZ G dd„ deƒZdS )Ú
é    )ÚStringIO)ÚlinesepNé   )ÚLDAPLDIFError)Úprepare_for_stream)ÚLDAPMessageÚ	MessageIDÚ
ProtocolOpÚLDAP_MAX_INT)Úoperation_to_ldifÚadd_ldif_header)Úbuild_controls_listé   )ÚBaseStrategyc               @   s|   e Zd ZdZdd„ Zddd„Zdd„ Zd	d
„ Zdd„ Zdd„ Z	ddd„Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS ) ÚLdifProducerStrategyaé  
    This strategy is used to create the LDIF stream for the Add, Delete, Modify, ModifyDn operations.
    You send the request and get the request in the ldif-change representation of the operation.
    NO OPERATION IS SENT TO THE LDAP SERVER!
    Connection.request will contain the result LDAP message in a dict form
    Connection.response will contain the ldif-change format of the requested operation if available
    You don't need a real server to connect to for this strategy
    c             C   sP   t  | |¡ d| _d| _d| _d| _t| _d| _d | _	t
ƒ | _d| _t ¡  d S )NTF)r   Ú__init__ZsyncZno_real_dsaZpooledZ
can_streamr   Úline_separatorÚ
all_base64ÚstreamÚdictÚorderÚ_header_addedÚrandomÚseed)ÚselfZldap_connection© r   ú^C:\Users\HIRONO~1\AppData\Local\Temp\pip-install-6i93dh7p\ldap3\ldap3\strategy\ldifProducer.pyr   0   s    zLdifProducerStrategy.__init__Fc             C   s.   t | j_| jjr"| jj jd7  _d| j_d S )Nr   F)ÚNotImplementedÚ
connectionÚsocketÚusageÚ_usageZopen_socketsÚclosed)r   ÚaddressZuse_sslZunix_socketr   r   r   Ú_open_socket=   s    z!LdifProducerStrategy._open_socketc             C   s.   | j jr| j j jd7  _d | j _d| j _d S )Nr   T)r   r!   r"   Zclosed_socketsr    r#   )r   r   r   r   Ú_close_socketD   s    z"LdifProducerStrategy._close_socketc             C   s@   d| j _d| j _d| _| jr0t| jtƒr<| jjr<|  tƒ ¡ d S )NTF)r   Ú	listeningr#   r   r   Ú
isinstancer   Ú
set_stream)r   r   r   r   Ú_start_listenK   s
    z"LdifProducerStrategy._start_listenc             C   s   | j  ¡  d| j_d| j_d S )NFT)r   Úcloser   r'   r#   )r   r   r   r   Ú_stop_listenR   s    
z!LdifProducerStrategy._stop_listenc             C   s   d S )Nr   )r   r   r   r   Ú	receivingW   s    zLdifProducerStrategy.receivingNc             C   sx   t  dt¡}tƒ }t|ƒ|d< tƒ  ||¡|d< t|ƒ}|dk	rH||d< t 	|||¡| j
_|| j
jd< | j
j| j|< |S )zA
        Build the LDAPMessage without sending to server
        r   Z	messageIDZ
protocolOpNÚcontrols)r   Úrandintr   r   r	   r
   ZsetComponentByNamer   r   Zdecode_requestr   ÚrequestÚ_outstanding)r   Zmessage_typer0   r.   Ú
message_idZldap_messageZmessage_controlsr   r   r   ÚsendZ   s    zLdifProducerStrategy.sendc             C   sœ   d | j _d | j _| jr˜|| jkr˜| j |¡}t| j jd || j| j 	| j jd ¡ƒ}| j
rx|rx| j jsx|  | j |¡¡ t|ƒ}| j |¡| j _| j jS d S )NÚtype)r   ÚresponseÚresultr1   Úpopr   r0   r   r   Úgetr   r#   Úaccumulate_streamr   Újoinr   )r   r2   r0   Z
ldif_linesr   r   r   Úpost_send_single_responsek   s    (z.LdifProducerStrategy.post_send_single_responsec             C   s   t dƒ‚d S )Nz5LDIF-CONTENT cannot be produced for Search operations)r   )r   r2   r   r   r   Úpost_send_searchy   s    z%LdifProducerStrategy.post_send_searchc             C   s   d S )Nr   )r   r2   r   r   r   Ú_get_response|   s    z"LdifProducerStrategy._get_responsec             C   sd   | j sD| j ¡ dkrDd| _ tdgƒd }| j t|| j | j ƒ¡ | j t|| j | j ƒ¡ d S )Nr   Tú-)r   r   Útellr   Úwriter   r   )r   ÚfragmentÚheaderr   r   r   r9      s
    z&LdifProducerStrategy.accumulate_streamc             C   s   | j S )N)r   )r   r   r   r   Ú
get_stream†   s    zLdifProducerStrategy.get_streamc          	   C   sH   d}y|  ¡ sd}W n ttfk
r0   d}Y nX |r>tdƒ‚|| _d S )NFTzstream must be writable)ÚwritableÚ
ValueErrorÚAttributeErrorr   r   )r   ÚvalueÚerrorr   r   r   r)   ‰   s    
zLdifProducerStrategy.set_stream)FF)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r%   r&   r*   r,   r-   r3   r;   r<   r=   r9   rC   r)   r   r   r   r   r   &   s   

r   )rL   Úior   Úosr   r   Zcore.exceptionsr   Z
utils.convr   Zprotocol.rfc4511r   r	   r
   r   Zprotocol.rfc2849r   r   Zprotocol.convertr   Úbaser   r   r   r   r   r   Ú<module>   s   