o
    ¯bp  ã                   @   s0   d Z ddlmZ G dd„ dƒZG dd„ dƒZdS )z6
Generic sentence handling tools: hopefully reusable.
é    )ÚSetc                   @   sN   e Zd ZU dZeƒ Zee ed< dd„ Z	e
dd„ ƒZdd„ Zd	efd
d„ZdS )Ú_BaseSentencea-  
    A base sentence class for a particular protocol.

    Using this base class, specific sentence classes can almost automatically
    be created for a particular protocol.
    To do this, fill the ALLOWED_ATTRIBUTES class attribute using
    the C{getSentenceAttributes} class method of the producer::

        class FooSentence(BaseSentence):
            """
            A sentence for integalactic transmodulator sentences.

            @ivar transmogrificationConstant: The value used in the
                transmogrifier while producing this sentence, corrected for
                gravitational fields.
            @type transmogrificationConstant: C{Tummy}
            """
            ALLOWED_ATTRIBUTES = FooProtocol.getSentenceAttributes()

    @ivar presentAttributes: An iterable containing the names of the
        attributes that are present in this sentence.
    @type presentAttributes: iterable of C{str}

    @cvar ALLOWED_ATTRIBUTES: A set of attributes that are allowed in this
        sentence.
    @type ALLOWED_ATTRIBUTES: C{set} of C{str}
    ÚALLOWED_ATTRIBUTESc                 C   s
   || _ dS )z½
        Initializes a sentence with parsed sentence data.

        @param sentenceData: The parsed sentence data.
        @type sentenceData: C{dict} (C{str} -> C{str} or L{None})
        N)Ú_sentenceData)ÚselfÚsentenceData© r   ú?/usr/lib/python3/dist-packages/twisted/positioning/_sentence.pyÚ__init__(   s   
z_BaseSentence.__init__c                 C   s
   t | jƒS )zÑ
        An iterable containing the names of the attributes that are present in
        this sentence.

        @return: The iterable of names of present attributes.
        @rtype: iterable of C{str}
        )Úiterr   )r   r   r   r	   ÚpresentAttributes1   s   
	z_BaseSentence.presentAttributesc                 C   s8   || j v r| j |d¡S | jj}|› d|› d�}t|ƒ‚)z5
        Gets an attribute of this sentence.
        Nz sentences have no z attributes)r   r   ÚgetÚ	__class__Ú__name__ÚAttributeError)r   ÚnameÚ	classNameÚmsgr   r   r	   Ú__getattr__<   s
   
z_BaseSentence.__getattr__Úreturnc                 C   sV   | j  ¡ }dd„ t|ƒD ƒ}d |¡}| j  d¡pd}| jj}d|› d|› d|› d	�S )
z˜
        Returns a textual representation of this sentence.

        @return: A textual representation of this sentence.
        @rtype: C{str}
        c                 S   s&   g | ]\}}|d kr|› d|› �‘qS )Útypez: r   )Ú.0ÚkÚvr   r   r	   Ú
<listcomp>O   s   & z*_BaseSentence.__repr__.<locals>.<listcomp>z, r   zunknown typeú<z (z) {z}>)r   ÚitemsÚsortedÚjoinr   r   r   )r   r   ÚdataÚdataReprÚtypeReprr   r   r   r	   Ú__repr__G   s   

z_BaseSentence.__repr__N)r   Ú
__module__Ú__qualname__Ú__doc__Úsetr   r   ÚstrÚ__annotations__r
   Úpropertyr   r   r"   r   r   r   r	   r   	   s   
 	

r   c                   @   s   e Zd ZdZedd„ ƒZdS )Ú!_PositioningSentenceProducerMixina©  
    A mixin for certain protocols that produce positioning sentences.

    This mixin helps protocols that store the layout of sentences that they
    consume in a C{_SENTENCE_CONTENTS} class variable provide all sentence
    attributes that can ever occur. It does this by providing a class method,
    C{getSentenceAttributes}, which iterates over all sentence types and
    collects the possible sentence attributes.
    c                 C   s8   dh}| j  ¡ D ]}|D ]}|du rq| |¡ qq|S )aR  
        Returns a set of all attributes that might be found in the sentences
        produced by this protocol.

        This is basically a set of all the attributes of all the sentences that
        this protocol can produce.

        @return: The set of all possible sentence attribute names.
        @rtype: C{set} of C{str}
        r   N)Ú_SENTENCE_CONTENTSÚvaluesÚadd)ÚclsÚ
attributesÚattributeListÚ	attributer   r   r	   ÚgetSentenceAttributesc   s   ýz7_PositioningSentenceProducerMixin.getSentenceAttributesN)r   r#   r$   r%   Úclassmethodr2   r   r   r   r	   r*   X   s    
r*   N)r%   Útypingr   r   r*   r   r   r   r	   Ú<module>   s   O