U
    c6{i
  ã                   @   sŽ   d Z ddlmZ G dd„ deƒZG dd„ dƒZG dd„ dƒZG d	d
„ d
ƒZG dd„ deƒZ	G dd„ deƒZ
G dd„ de
ƒZG dd„ de
ƒZdS )zf
Twisted Python Roots: an abstract hierarchy representation for Twisted.

Maintainer: Glyph Lefkowitz
é    )Úreflectc                   @   s   e Zd ZdZdS )ÚNotSupportedErrorzv
    An exception meaning that the tree-manipulation operation
    you're attempting to perform is not supported.
    N©Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r	   r	   ú8/tmp/pip-unpacked-wheel-hfbvu0bz/twisted/python/roots.pyr      s   r   c                   @   s$   e Zd ZdZdZdd„ Zdd„ ZdS )ÚRequestzãI am an abstract representation of a request for an entity.

    I also function as the response.  The request is responded to by calling
    self.write(data) until there is no data left and then calling
    self.finish().
    Nc                 C   s   t dt | j¡ ƒ‚dS )z.Add some data to the response to this request.z%s.writeN©ÚNotImplementedErrorr   ÚqualÚ	__class__)ÚselfÚdatar	   r	   r
   Úwrite"   s    zRequest.writec                 C   s   t dt | j¡ ƒ‚dS )zOThe response to this request is finished; flush all data to the network stream.z	%s.finishNr   ©r   r	   r	   r
   Úfinish&   s    zRequest.finish)r   r   r   r   ZwireProtocolr   r   r	   r	   r	   r
   r      s   	r   c                   @   s   e Zd ZdZdd„ ZdS )ÚEntityaZ  I am a terminal object in a hierarchy, with no children.

    I represent a null interface; certain non-instance objects (strings and
    integers, notably) are Entities.

    Methods on this class are suggested to be implemented, but are not
    required, and will be emulated on a per-protocol basis for types which do
    not handle them.
    c                 C   s   t dt | j¡ ƒ‚dS )zw
        I produce a stream of bytes for the request, by calling request.write()
        and request.finish().
        z	%s.renderNr   ©r   Úrequestr	   r	   r
   Úrender6   s    zEntity.renderN)r   r   r   r   r   r	   r	   r	   r
   r   +   s   
r   c                   @   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dd„ Zdd„ Zdd„ ZdS ) Ú
Collectionz�I represent a static collection of entities.

    I contain methods designed to represent collections that can be dynamically
    created.
    Nc                 C   s   |dk	r|| _ ni | _ dS )zInitialize me.N©Úentities)r   r   r	   r	   r
   Ú__init__E   s    zCollection.__init__c                 C   s   | j  |¡S )zqGet an entity that was added to me using putEntity.

        This method will return 'None' if it fails.
        )r   Úget©r   Únamer	   r	   r
   ÚgetStaticEntityL   s    zCollection.getStaticEntityc                 C   s   dS )znSubclass this to generate an entity on demand.

        This method should return 'None' if it fails.
        Nr	   ©r   r   r   r	   r	   r
   ÚgetDynamicEntityS   s    zCollection.getDynamicEntityc                 C   s2   |   |¡}|dk	r|S |  ||¡}|dk	r.|S dS )a  Retrieve an entity from me.

        I will first attempt to retrieve an entity statically; static entities
        will obscure dynamic ones.  If that fails, I will retrieve the entity
        dynamically.

        If I cannot retrieve an entity, I will return 'None'.
        N)r    r"   )r   r   r   Úentr	   r	   r
   Ú	getEntityY   s    	
zCollection.getEntityc                 C   s   || j |< dS )zlStore a static reference on 'name' for 'entity'.

        Raises a KeyError if the operation fails.
        Nr   ©r   r   Úentityr	   r	   r
   Ú	putEntityj   s    zCollection.putEntityc                 C   s   | j |= dS )zaRemove a static reference for 'name'.

        Raises a KeyError if the operation fails.
        Nr   r   r	   r	   r
   Ú	delEntityq   s    zCollection.delEntityc                 C   s   t dt | j¡ ƒ‚dS )z>Store an entity for 'name', based on the content of 'request'.z%s.storeEntityN©r   r   r   r   r!   r	   r	   r
   ÚstoreEntityx   s    zCollection.storeEntityc                 C   s   t dt | j¡ ƒ‚dS )z?Remove an entity for 'name', based on the content of 'request'.z%s.removeEntityNr)   r!   r	   r	   r
   ÚremoveEntity|   s    zCollection.removeEntityc                 C   s
   | j  ¡ S )zlRetrieve a list of all name, entity pairs that I store references to.

        See getStaticEntity.
        )r   Úitemsr   r	   r	   r
   ÚlistStaticEntities€   s    zCollection.listStaticEntitiesc                 C   s   g S )zaA list of all name, entity that I can generate on demand.

        See getDynamicEntity.
        r	   r   r	   r	   r
   ÚlistDynamicEntities‡   s    zCollection.listDynamicEntitiesc                 C   s   |   ¡ |  |¡ S )zURetrieve a list of all name, entity pairs I contain.

        See getEntity.
        )r-   r.   r   r	   r	   r
   ÚlistEntitiesŽ   s    zCollection.listEntitiesc                 C   s
   | j  ¡ S )zkRetrieve a list of the names of entities that I store references to.

        See getStaticEntity.
        )r   Úkeysr   r	   r	   r
   ÚlistStaticNames•   s    zCollection.listStaticNamesc                 C   s   g S )zlRetrieve a list of the names of entities that I store references to.

        See getDynamicEntity.
        r	   r   r	   r	   r
   ÚlistDynamicNamesœ   s    zCollection.listDynamicNamesc                 C   s   |   ¡ S )zZRetrieve a list of all names for entities that I contain.

        See getEntity.
        )r1   r   r	   r	   r
   Ú	listNames£   s    zCollection.listNames)N)r   r   r   r   r   r    r"   r$   r'   r(   r*   r+   r-   r.   r/   r1   r2   r3   r	   r	   r	   r
   r   >   s   
r   c                   @   s   e Zd ZdZdS )ÚConstraintViolationz2An exception raised when a constraint is violated.Nr   r	   r	   r	   r
   r4   «   s   r4   c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚConstrainedz?A collection that has constraints on its names and/or entities.c                 C   s   dS )zôA method that determines whether an entity may be added to me with a given name.

        If the constraint is satisfied, return 1; if the constraint is not
        satisfied, either return 0 or raise a descriptive ConstraintViolation.
        é   r	   r   r	   r	   r
   ÚnameConstraint²   s    zConstrained.nameConstraintc                 C   s   dS )zâA method that determines whether an entity may be added to me.

        If the constraint is satisfied, return 1; if the constraint is not
        satisfied, either return 0 or raise a descriptive ConstraintViolation.
        r6   r	   ©r   r&   r	   r	   r
   ÚentityConstraintº   s    zConstrained.entityConstraintc                 C   s   t  | ||¡ d S ©N)r   r'   r%   r	   r	   r
   ÚreallyPutEntityÂ   s    zConstrained.reallyPutEntityc                 C   s8   |   |¡r,|  |¡r"|  ||¡ q4tdƒ‚ntdƒ‚dS )zfStore an entity if it meets both constraints.

        Otherwise raise a ConstraintViolation.
        zEntity constraint violated.zName constraint violated.N)r7   r9   r;   r4   r%   r	   r	   r
   r'   Å   s
    


zConstrained.putEntityN)r   r   r   r   r7   r9   r;   r'   r	   r	   r	   r
   r5   ¯   s
   r5   c                   @   s$   e Zd ZdZdZdd„ Zdd„ ZdS )ÚLockedz5A collection that can be locked from adding entities.r   c                 C   s
   d| _ d S )Nr6   ©Úlockedr   r	   r	   r
   ÚlockØ   s    zLocked.lockc                 C   s   | j  S r:   r=   r8   r	   r	   r
   r9   Û   s    zLocked.entityConstraintN)r   r   r   r   r>   r?   r9   r	   r	   r	   r
   r<   Ó   s   r<   c                   @   s,   e Zd ZdZeZdd„ Zdd„ Zdd„ ZdS )	Ú
Homogenousz¡A homogenous collection of entities.

    I will only contain entities that are an instance of the class or type
    specified by my 'entityType' attribute.
    c                 C   s*   t || jƒrdS t|› d| j› d�ƒ‚d S )Nr6   z of incorrect type (ú))Ú
isinstanceÚ
entityTyper4   r8   r	   r	   r
   r9   è   s    zHomogenous.entityConstraintc                 C   s   dS )NÚNamer	   r   r	   r	   r
   ÚgetNameTypeî   s    zHomogenous.getNameTypec                 C   s   | j jS r:   )rC   r   r   r	   r	   r
   ÚgetEntityTypeñ   s    zHomogenous.getEntityTypeN)	r   r   r   r   ÚobjectrC   r9   rE   rF   r	   r	   r	   r
   r@   ß   s
   r@   N)r   Ztwisted.pythonr   r   r   r   r   r   Ú	Exceptionr4   r5   r<   r@   r	   r	   r	   r
   Ú<module>   s   m$