o
    ßý°jS  ã                   @   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__ÚsyncÚno_real_dsaÚpooledÚ
can_streamr   Úline_separatorÚ
all_base64ÚstreamÚdictÚorderÚ_header_addedÚrandomÚseed)ÚselfÚldap_connection© r#   úŽ/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/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Ú_usageÚopen_socketsÚclosed)r!   ÚaddressÚuse_sslÚ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)   Úclosed_socketsr'   r+   ©r!   r#   r#   r$   Ú_close_socketD   s   z"LdifProducerStrategy._close_socketc                 C   sP   d| j _d| j _d| j _d| _| jrt| jtƒr$| jjr&|  tƒ ¡ d S d S d S r   )	r&   Ú	listeningr+   Úboundr   r   Ú
isinstancer   Ú
set_streamr1   r#   r#   r$   Ú_start_listenK   s   ÿz"LdifProducerStrategy._start_listenc                 C   s&   | j  ¡  d| j_d| j_d| j_d S )NFT)r   Úcloser&   r3   r4   r+   r1   r#   r#   r$   Ú_stop_listenS   s   
z!LdifProducerStrategy._stop_listenc                 C   ó   d S ©Nr#   r1   r#   r#   r$   Ú	receivingY   ó   zLdifProducerStrategy.receivingNc                 C   sŠ   t  dt¡}tƒ }t|ƒ|d< tƒ  ||¡|d< t|ƒ}|dur$||d< t 	|||¡| j
_|| j
jd< | jdu r<tƒ | _| j
j| j|< |S )zA
        Build the LDAPMessage without sending to server
        r   Ú	messageIDÚ
protocolOpNÚcontrols)r   Úrandintr   r	   r
   r   ÚsetComponentByNamer   r   Údecode_requestr&   ÚrequestÚ_outstandingr   )r!   Úmessage_typerD   r@   Ú
message_idÚldap_messageÚmessage_controlsr#   r#   r$   Úsend\   s   
zLdifProducerStrategy.sendc                 C   sœ   d | j _d | j _| jrL|| jv rL| j |¡}t| j jd || j| j 	| j jd ¡ƒ}| j
r<|r<| j js<|  | j |¡¡ t|ƒ}| j |¡| j _| j jS d S )NÚtype)r&   ÚresponseÚresultrE   Úpopr   rD   r   r   Úgetr   r+   Úaccumulate_streamr   Újoinr   )r!   rG   rD   Ú
ldif_linesr#   r#   r$   Úpost_send_single_responseo   s   (z.LdifProducerStrategy.post_send_single_responsec                 C   s   t dƒ‚)Nz5LDIF-CONTENT cannot be produced for Search operationsr   )r!   rG   r#   r#   r$   Úpost_send_search}   s   z%LdifProducerStrategy.post_send_searchc                 C   r:   r;   r#   )r!   rG   Útimeoutr#   r#   r$   Ú_get_response€   r=   z"LdifProducerStrategy._get_responsec                 C   sd   | j s"| j ¡ dkr"d| _ 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$   rP   ƒ   s
    z&LdifProducerStrategy.accumulate_streamc                 C   s   | j S r;   )r   r1   r#   r#   r$   Ú
get_streamŠ   s   zLdifProducerStrategy.get_streamc              	   C   sF   d}z|  ¡ s	d}W n ttfy   d}Y nw |rtdƒ‚|| _d S )NFTzstream must be writable)ÚwritableÚ
ValueErrorÚAttributeErrorr   r   )r!   ÚvalueÚerrorr#   r#   r$   r6   �   s   €ÿ
zLdifProducerStrategy.set_stream)FFr;   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r/   r2   r7   r9   r<   rJ   rS   rT   rV   rP   r\   r6   r#   r#   r#   r$   r   &   s    	

r   )re   Úior   Úosr   r   Úcore.exceptionsr   Ú
utils.convr   Úprotocol.rfc4511r	   r
   r   r   Úprotocol.rfc2849r   r   Úprotocol.convertr   Úbaser   r   r#   r#   r#   r$   Ú<module>   s    