o
    j!.                     @   s   d 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mZmZ ddlmZmZ dd	lmZ dd
lmZ ddlmZ ddlmZmZmZmZmZmZ G dd deZ dS )
    )linesep   )AttrDef   )LDAPKeyErrorLDAPObjectErrorLDAPAttributeErrorLDAPSchemaError)STRING_TYPESSEQUENCE_TYPESServer
Connection)
SchemaInfoconstant_to_class_kind)find_attribute_validator)CaseInsensitiveWithAliasDict)get_config_parameter)loglog_enabledERRORBASICPROTOCOLEXTENDEDc                   @   s   e Zd Z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d Zdd Zdd Zdd Zeeur<dd Zndd Zdd Zd)d d!Zd*d"d#Zd$d% Zd&d' ZdS )+	ObjectDefaC  Represent an object in the LDAP server. AttrDefs are stored in a dictionary; the key is the friendly name defined in AttrDef.

    AttrDefs can be added and removed using the += and -= operators

    ObjectDef can be accessed either as a sequence and a dictionary. When accessed the whole AttrDef instance is returned

    Nc                    s  |d u rg }t |ts|g}|d u rg }t |ts|g}t | jd< || jd< g | jd< t  tr; js7 jr;    d urt  trH j	 n#t  trR j
j	 nt  trXn rkd}ttrgttd||  t| d u rd}ttr|ttd||  t| | jd< | jr fdd	|D } fd
d	|D }|D ]	}|r| | q|D ]	}|r| | q|| jd< || jd< ttrttd|  d S d S )N_attributes_custom_validator	_oid_infozunable to read schema%s for <%s>zschema not present_schemac                       g | ]
} j | jd  qS r   object_classesname.0r$   schema /root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/ldap3/abstract/objectDef.py
<listcomp>V       z&ObjectDef.__init__.<locals>.<listcomp>c                    r    r!   r"   r%   r'   r)   r*   r+   W   r,   _object_class_auxiliary_classzinstantiated ObjectDef: <%r>)
isinstancer   r   __dict__r   _deferred_bind_deferred_open_fire_deferredr   r(   serverr   r   r   r   r
   r   _populate_attr_defsr   )selfobject_classr(   custom_validatorauxiliary_classerror_messageobject_namer)   r'   r*   __init__.   s^   












zObjectDef.__init__c                 C   s   || j jv rO| j j| }| jd |d t|j d t|j  |jr0|jD ]}| 	| q(|j
D ]}| |d q3|jD ]}|| jvrL| |d q?d S d| }ttr^ttd||  t|)Nr   z (z) TFz'object class '%s' not defined in schemar   )r   r#   r0   appendr   kindstroidsuperiorr5   must_containadd_from_schemamay_containr   r   r   r   r   )r6   r;   object_schemasupattribute_namer:   r)   r)   r*   r5   f   s"   ,



zObjectDef._populate_attr_defsc                    s    j rdd j  t }ndt } jr"|dd j t 7 }n|dt 7 }|dddd  jD  t 7 }|d	dt fd
d jD  t 7 }|ddt fdd jD  t 7 }|S )NzOBJ : z, zOBJ : <None>zAUX : zAUX : <None>zOID: c                 S   s   g | ]}|qS r)   r)   )r&   r@   r)   r)   r*   r+      s    z&ObjectDef.__repr__.<locals>.<listcomp>zMUST: c                    s   g | ]
} j | jr|qS r)   r   	mandatoryr&   attrr6   r)   r*   r+      r,   zMAY : c                    s   g | ]
} j | js|qS r)   rH   rJ   rL   r)   r*   r+      r,   )r-   joinr   r.   r   sortedr   )r6   rr)   rL   r*   __repr__y   s   "**zObjectDef.__repr__c                 C   s   |   S N)rP   rL   r)   r)   r*   __str__   s   zObjectDef.__str__c                 C   s
   |  |S rQ   )__getattr__r6   itemr)   r)   r*   __getitem__      
zObjectDef.__getitem__c                 C   s   d |  }d| jv r/z| j| W S  ty.   d| }ttr*ttd||  t	|w d}ttr<ttd||  t	|)N r   key '%s' not presentr   z)internal _attributes property not defined)
rM   splitlowerr0   r   KeyErrorr   r   r   r   )r6   rU   r:   r)   r)   r*   rS      s   
zObjectDef.__getattr__c                 C   s&   d| }t trttd||  t|)Nzobject '%s' is read onlyr   )r   r   r   r   )r6   keyvaluer:   r)   r)   r*   __setattr__   s   zObjectDef.__setattr__c                 C   s   |  | | S rQ   )add_attributer6   otherr)   r)   r*   __iadd__   s   
zObjectDef.__iadd__c                 C   s2   t |tr| |j | S t |tr| | | S rQ   )r/   r   remove_attributer]   r   ra   r)   r)   r*   __isub__   s   


zObjectDef.__isub__c                 c   s    | j D ]}| j | V  qd S rQ   )r   )r6   	attributer)   r)   r*   __iter__   s   
zObjectDef.__iter__c                 C   s
   t | jS rQ   )lenr   rL   r)   r)   r*   __len__   rW   zObjectDef.__len__c                 C      dS NTr)   rL   r)   r)   r*   __bool__      zObjectDef.__bool__c                 C   rj   rk   r)   rL   r)   r)   r*   __nonzero__   rm   zObjectDef.__nonzero__c                 C   s&   z|  | W dS  ty   Y dS w )NFT)rV   r\   rT   r)   r)   r*   __contains__   s   zObjectDef.__contains__Fc                 C   sf   t |}t| j|| j|_||_| jr,| jjr,|| jjv r,| jj| j|_| jj| |_| 	| d S rQ   )
r   r   r   r   validaterI   attribute_typessingle_valueoid_infor`   )r6   rG   rI   attr_defr)   r)   r*   rC      s   zObjectDef.add_from_schemac                    s(  dd t dD }t tr|   dS t trr j |vrn j| jvrY | j j<  jr? j jkr?| j	 j j  j
rM fdd j
jD nd}|rY| j	 j|  jspt| j j| j}|| j j _dS dS dS t tr D ]}| | qydS d}ttrttd||  t|)zAdd an AttrDef to the ObjectDef. Can be called with the += operator.
        :param definition: the AttrDef object to add, can also be a string containing the name of attribute to add. Can be a list of both

        c                 S   s   g | ]}|  qS r)   )r[   )r&   vr)   r)   r*   r+      s    z+ObjectDef.add_attribute.<locals>.<listcomp>#ATTRIBUTES_EXCLUDED_FROM_OBJECT_DEFc                    s"   g | ]} j  | kr|qS r)   )r]   r[   r%   
definitionr)   r*   r+      s   " Nz*unable to add element to object definitionr   )r   r/   r   rC   r   r]   r[   r   r$   	set_aliasrs   rp   r   r   r   r   r`   r   r   r   r   )r6   rx   (conf_attributes_excluded_from_object_defother_names	validatorelementr:   r)   rw   r*   r`      s2   

 	
zObjectDef.add_attributec                 C   s   d}t |trd|  }n
t |tr|j }|rC| jD ]}|| kr/| j|=  dS q d| }tt	r?t
t	d||  t|dtt| }tt	rVt
t	d||  t|)zRemove an AttrDef from the ObjectDef. Can be called with the -= operator.
        :param item: the AttrDef to remove, can also be a string containing the name of attribute to remove

        NrX   rY   r   z$key type must be str or AttrDef not )r/   r   rM   rZ   r[   r   r]   r   r   r   r   r   r?   typer	   )r6   rU   r]   rK   r:   r)   r)   r*   rd      s&   



zObjectDef.remove_attributec                 C   s$   d| j d< d| j d< t | j d< dS )z,Empty the ObjectDef attribute list

        Nr7   r9   r   )r0   dictrL   r)   r)   r*   clear_attributes  s   

zObjectDef.clear_attributes)NNNN)FrQ   )__name__
__module____qualname____doc__r<   r5   rP   rR   rV   rS   r_   rc   re   rg   ri   r?   bytesrl   rn   ro   rC   r`   rd   r   r)   r)   r)   r*   r   &   s*    
8


	r   N)!r   osr   attrDefr   core.exceptionsr   r   r	   r
   rX   r   r   r   r   protocol.rfc4512r   r   protocol.formatters.standardr   utils.ciDictr   utils.configr   	utils.logr   r   r   r   r   r   objectr   r)   r)   r)   r*   <module>   s     