U
    
W[                     @   s   d Z ddlmZmZ ddlmZ ddlmZ G dd deZ	eG dd dZ
eG d	d
 d
Ze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
    )absolute_importdivision)reflect)	_oldStylec                   @   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   6/usr/lib/python3/dist-packages/twisted/python/roots.pyr      s   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   s   t dt| j dS )z7Add 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 )zXThe 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   render<   s    zEntity.renderN)r   r	   r
   r   r   r   r   r   r   r   1   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k	r|| _ ni | _ dS )zInitialize me.
        Nentities)r   r   r   r   r   __init__M   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   getStaticEntityU   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   getDynamicEntity\   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   Zentr   r   r   	getEntityb   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   	putEntitys   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   	delEntityz   s    zCollection.delEntityc                 C   s   t dt| j dS )zGStore an entity for 'name', based on the content of 'request'.
        z%s.storeEntityNr   r   r   r   r$   r   r   r   storeEntity   s    zCollection.storeEntityc                 C   s   t dt| j dS )zHRemove 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/   r0   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.
        )r3   r   r   r   r   	listNames   s    zCollection.listNames)N)r   r	   r
   r   r   r#   r%   r&   r)   r*   r,   r-   r/   r0   r1   r3   r4   r5   r   r   r   r   r   E   s   
r   c                   @   s   e Zd ZdZdS )ConstraintViolationz7An exception raised when a constraint is violated.
    Nr   r   r   r   r   r6      s   r6   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 )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   s    zConstrained.nameConstraintc                 C   s   dS )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.
        r8   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)r9   r;   r=   r6   r'   r   r   r   r)      s
    


zConstrained.putEntityN)r   r	   r
   r   r9   r;   r=   r)   r   r   r   r   r7      s
   r7   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 )Nr8   lockedr   r   r   r   lock   s    zLocked.lockc                 C   s   | j  S r<   r?   r:   r   r   r   r;      s    zLocked.entityConstraintN)r   r	   r
   r   r@   rA   r;   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 )	
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f d S )Nr8   z%s of incorrect type (%s))
isinstance
entityTyper6   r:   r   r   r   r;      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<   )rD   r   r   r   r   r   getEntityType   s    zHomogenous.getEntityTypeN)	r   r	   r
   r   objectrD   r;   rF   rG   r   r   r   r   rB      s
   rB   N)r   Z
__future__r   r   Ztwisted.pythonr   Ztwisted.python._oldstyler   r   r   r   r   r   	Exceptionr6   r7   r>   rB   r   r   r   r   <module>   s   r$