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    