o
    b
                     @   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                   @      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
   6/usr/lib/python3/dist-packages/twisted/python/roots.pyr          r   c                   @   s$   e Zd ZdZdZdd Zdd ZdS )RequestzI 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      t dt| j )z.Add some data to the response to this request.z%s.writeNotImplementedErrorr   qual	__class__)selfdatar
   r
   r   write"      zRequest.writec                 C   r   )zOThe response to this request is finished; flush all data to the network stream.z	%s.finishr   r   r
   r
   r   finish&   r   zRequest.finish)r   r   r   r	   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   r   )zw
        I produce a stream of bytes for the request, by calling request.write()
        and request.finish().
        z	%s.renderr   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 ) 
CollectionzI represent a static collection of entities.

    I contain methods designed to represent collections that can be dynamically
    created.
    Nc                 C   s   |dur	|| _ dS 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      zCollection.getStaticEntityc                 C      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ur|S | ||}|du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   r&   zCollection.delEntityc                 C   r   )z>Store an entity for 'name', based on the content of 'request'.z%s.storeEntityr   r   r   r   r(   r
   r
   r   storeEntityx   r   zCollection.storeEntityc                 C   r   )z?Remove an entity for 'name', based on the content of 'request'.z%s.removeEntityr0   r(   r
   r
   r   removeEntity|   r   zCollection.removeEntityc                 C   
   | j  S )zlRetrieve a list of all name, entity pairs that I store references to.

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

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

        See getEntity.
        )r5   r8   r   r
   r
   r   listEntities   s   zCollection.listEntitiesc                 C   r3   )zkRetrieve a list of the names of entities that I store references to.

        See getStaticEntity.
        )r    keysr   r
   r
   r   listStaticNames   r6   zCollection.listStaticNamesc                 C   r7   )zlRetrieve a list of the names of entities that I store references to.

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

        See getEntity.
        )r<   r   r
   r
   r   	listNames   s   zCollection.listNamesN)r   r   r   r	   r!   r%   r)   r+   r.   r/   r1   r2   r5   r8   r:   r<   r=   r>   r
   r
   r
   r   r   >   s     
r   c                   @   r   )ConstraintViolationz2An exception raised when a constraint is violated.Nr   r
   r
   r
   r   r@      r   r@   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   r'   )zA 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      zConstrained.nameConstraintc                 C   r'   )zA 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.
        rB   r
   r   r-   r
   r
   r   entityConstraint   rD   zConstrained.entityConstraintc                 C   s   t | || d S r?   )r   r.   r,   r
   r
   r   reallyPutEntity   s   zConstrained.reallyPutEntityc                 C   s4   |  |r| |r| || dS tdtd)zfStore an entity if it meets both constraints.

        Otherwise raise a ConstraintViolation.
        zEntity constraint violated.zName constraint violated.N)rC   rF   rG   r@   r,   r
   r
   r   r.      s
   

zConstrained.putEntityN)r   r   r   r	   rC   rF   rG   r.   r
   r
   r
   r   rA      s    rA   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 )NrB   lockedr   r
   r
   r   lock   s   
zLocked.lockc                 C   s   | j  S r?   rI   rE   r
   r
   r   rF         zLocked.entityConstraintN)r   r   r   r	   rJ   rK   rF   r
   r
   r
   r   rH      s
    rH   c                   @   s,   e Zd ZdZeZdd Zdd Zdd ZdS )	
HomogenouszA 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)NrB   z of incorrect type ())
isinstance
entityTyper@   rE   r
   r
   r   rF      s   zHomogenous.entityConstraintc                 C   r'   )NNamer
   r   r
   r
   r   getNameType   s   zHomogenous.getNameTypec                 C   s   | j jS r?   )rP   r   r   r
   r
   r   getEntityType   rL   zHomogenous.getEntityTypeN)	r   r   r   r	   objectrP   rF   rR   rS   r
   r
   r
   r   rM      s    rM   N)r	   twisted.pythonr   r   r   r   r   r   	Exceptionr@   rA   rH   rM   r
   r
   r
   r   <module>   s   m$