U
    Sc?                     @   s   d dl Z d dlmZ d dlmZ d dlmZ ddddgZze W n ek
r\   d	d
 ZY nX 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 )    N)error)tag)tagmap	NamedTypeOptionalNamedTypeDefaultedNamedType
NamedTypesc                 C   s   t tt | S N)boolfilter)x r   9/tmp/pip-unpacked-wheel-hsjy238j/pyasn1/type/namedtype.py<lambda>       r   c                   @   s   e Zd Zd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dd Zedd Zedd Zd d! Zd"d# ZdS )%r   a  Create named field object for a constructed ASN.1 type.

    The |NamedType| object represents a single name and ASN.1 type of a constructed ASN.1 type.

    |NamedType| objects are immutable and duck-type Python :class:`tuple` objects
    holding *name* and *asn1Object* components.

    Parameters
    ----------
    name: :py:class:`str`
        Field name

    asn1Object:
        ASN.1 type object
    FNc                 C   s    || _ || _||f| _|| _d S r	   )_NamedType__name_NamedType__type_NamedType__nameAndType_NamedType__openType)selfname
asn1ObjectopenTyper   r   r   __init__*   s    
zNamedType.__init__c                 C   s4   d| j | jf }| jr$|d| j 7 }d| jj|f S )Nz%s=%rz, open type %rz<%s object, type %s>)r   r   r   	__class____name__r   Zrepresentationr   r   r   __repr__0   s     zNamedType.__repr__c                 C   s
   | j |kS r	   r   r   otherr   r   r   __eq__9   s    zNamedType.__eq__c                 C   s
   | j |kS r	   r   r   r   r   r   __ne__<   s    zNamedType.__ne__c                 C   s
   | j |k S r	   r   r   r   r   r   __lt__?   s    zNamedType.__lt__c                 C   s
   | j |kS r	   r   r   r   r   r   __le__B   s    zNamedType.__le__c                 C   s
   | j |kS r	   r   r   r   r   r   __gt__E   s    zNamedType.__gt__c                 C   s
   | j |kS r	   r   r   r   r   r   __ge__H   s    zNamedType.__ge__c                 C   s
   t | jS r	   )hashr   r   r   r   r   __hash__K   s    zNamedType.__hash__c                 C   s
   | j | S r	   r   r   idxr   r   r   __getitem__N   s    zNamedType.__getitem__c                 C   s
   t | jS r	   )iterr   r(   r   r   r   __iter__Q   s    zNamedType.__iter__c                 C   s   | j S r	   )r   r(   r   r   r   r   T   s    zNamedType.namec                 C   s   | j S r	   )r   r(   r   r   r   r   X   s    zNamedType.asn1Objectc                 C   s   | j S r	   )r   r(   r   r   r   r   \   s    zNamedType.openTypec                 C   s   | j S r	   r   r(   r   r   r   getNameb   s    zNamedType.getNamec                 C   s   | j S r	   r   r(   r   r   r   getTypee   s    zNamedType.getType)N)r   
__module____qualname____doc__
isOptionalisDefaultedr   r   r!   r"   r#   r$   r%   r&   r)   r,   r.   propertyr   r   r   r0   r2   r   r   r   r   r      s,   
	


c                   @   s   e Zd ZejZdZdS )r   TN)r   r3   r4   r   r5   r6   r   r   r   r   r   i   s   c                   @   s   e Zd ZejZdZdS )r   TN)r   r3   r4   r   r5   r7   r   r   r   r   r   o   s   c                   @   st  e Zd Z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ejd dkrdd Zndd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) ZG d*d+ d+eZd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Z d8d9 Z!d:d; Z"d<d= Z#d>d? Z$e%d@dA Z&dBdC Z'e%dDdE Z(e%dFdG Z)e%dHdI Z*e%dJdK Z+e%dLdM Z,e%dNdO Z-dPS )Qr   a+  Create a collection of named fields for a constructed ASN.1 type.

    The NamedTypes object represents a collection of named fields of a constructed ASN.1 type.

    *NamedTypes* objects are immutable and duck-type Python :class:`dict` objects
    holding *name* as keys and ASN.1 type object as values.

    Parameters
    ----------
    *namedTypes: :class:`~pyasn1.type.namedtype.NamedType`

    Examples
    --------

    .. code-block:: python

        class Description(Sequence):
            '''
            ASN.1 specification:

            Description ::= SEQUENCE {
                surname    IA5String,
                first-name IA5String OPTIONAL,
                age        INTEGER DEFAULT 40
            }
            '''
            componentType = NamedTypes(
                NamedType('surname', IA5String()),
                OptionalNamedType('first-name', IA5String()),
                DefaultedNamedType('age', Integer(40))
            )

        descr = Description()
        descr['surname'] = 'Smith'
        descr['first-name'] = 'John'
    c                 O   s   || _ t| j | _|  | _|  | _|  | _d|kr@| 	 pBi | _
| jdd| _| jdd| _tdd | j D | _tdd | j D | _tdd t| j D | _td	d | j D | _td
d | j D | _tdd | j D | _d S )NterminalT)uniqueFc                 S   s   g | ]}|j s|jrd qS T)r7   r6   .0	namedTyper   r   r   
<listcomp>   s     z'NamedTypes.__init__.<locals>.<listcomp>c                 S   s   g | ]}|j rd qS r;   )r   r<   r   r   r   r?      s    c                 S   s    g | ]\}}|j s|js|qS r   )r6   r7   )r=   r+   ntr   r   r   r?      s       c                 S   s   g | ]
}|j qS r   r/   r<   r   r   r   r?      s     c                 S   s   g | ]
}|j qS r   r1   r<   r   r   r   r?      s     c                 S   s   g | ]}|j |jfqS r   )r   r   r<   r   r   r   r?      s     )_NamedTypes__namedTypeslen_NamedTypes__namedTypesLen_NamedTypes__computeMinTagSet_NamedTypes__minTagSet _NamedTypes__computeNameToPosMap_NamedTypes__nameToPosMap_NamedTypes__computeTagToPosMap_NamedTypes__tagToPosMap"_NamedTypes__computeAmbiguousTypes_NamedTypes__ambiguousTypes_NamedTypes__computeTagMaps_NamedTypes__uniqueTagMap_NamedTypes__nonUniqueTagMapany!_NamedTypes__hasOptionalOrDefault_NamedTypes__hasOpenTypes	frozenset	enumerate_NamedTypes__requiredComponents_NamedTypes__keystuple_NamedTypes__values_NamedTypes__items)r   
namedTypeskwargsr   r   r   r      s     


zNamedTypes.__init__c                 C   s&   d dd | jD }d| jj|f S )Nz, c                 S   s   g | ]}d | qS )z%rr   r=   r   r   r   r   r?      s     z'NamedTypes.__repr__.<locals>.<listcomp>z<%s object, types %s>)joinrA   r   r   r   r   r   r   r      s
     zNamedTypes.__repr__c                 C   s
   | j |kS r	   rA   r   r   r   r   r!      s    zNamedTypes.__eq__c                 C   s
   | j |kS r	   r]   r   r   r   r   r"      s    zNamedTypes.__ne__c                 C   s
   | j |k S r	   r]   r   r   r   r   r#      s    zNamedTypes.__lt__c                 C   s
   | j |kS r	   r]   r   r   r   r   r$      s    zNamedTypes.__le__c                 C   s
   | j |kS r	   r]   r   r   r   r   r%      s    zNamedTypes.__gt__c                 C   s
   | j |kS r	   r]   r   r   r   r   r&      s    zNamedTypes.__ge__c                 C   s
   t | jS r	   )r'   rA   r(   r   r   r   r)      s    zNamedTypes.__hash__c                 C   s6   z| j | W S  tk
r0   | j | j|   Y S X d S r	   )rA   	TypeErrorrG   r*   r   r   r   r,      s    zNamedTypes.__getitem__c                 C   s
   || j kS r	   )rG   )r   keyr   r   r   __contains__   s    zNamedTypes.__contains__c                 C   s   dd | j D S )Nc                 s   s   | ]}|d  V  qdS )r   Nr   r[   r   r   r   	<genexpr>   s     z&NamedTypes.__iter__.<locals>.<genexpr>r]   r(   r   r   r   r.      s    zNamedTypes.__iter__r      c                 C   s
   | j dkS Nr   rC   r(   r   r   r   __nonzero__   s    zNamedTypes.__nonzero__c                 C   s
   | j dkS rc   rd   r(   r   r   r   __bool__   s    zNamedTypes.__bool__c                 C   s   | j S r	   rd   r(   r   r   r   __len__   s    zNamedTypes.__len__c                 C   s   | j S r	   )rW   r(   r   r   r   values   s    zNamedTypes.valuesc                 C   s   | j S r	   )rU   r(   r   r   r   keys   s    zNamedTypes.keysc                 C   s   | j S r	   )rX   r(   r   r   r   items   s    zNamedTypes.itemsc                 C   s   | j | j S r	   )r   rA   r(   r   r   r   clone   s    zNamedTypes.clonec                   @   s   e Zd Zdd Zdd ZdS )zNamedTypes.PostponedErrorc                 C   s
   || _ d S r	   )_PostponedError__errorMsg)r   ZerrorMsgr   r   r   r      s    z"NamedTypes.PostponedError.__init__c                 C   s   t | jd S r	   )r   PyAsn1Errorrl   )r   itemr   r   r   r,      s    z%NamedTypes.PostponedError.__getitem__N)r   r3   r4   r   r,   r   r   r   r   PostponedError   s   ro   c                 C   st   i }t | jD ]`\}}|jj}t|tjr2|  S |s8q|jD ].}||krdtd||f     S |||< q>q|S )Nz Duplicate component tag %s at %s)rS   rA   r   tagMap
isinstancer   ro   presentTypes)r   ZtagToPosMapr+   r>   rp   Z_tagSetr   r   r   Z__computeTagToPosMap   s    
zNamedTypes.__computeTagToPosMapc                 C   sH   i }t | jD ]4\}}|j|kr8td|j|f   S |||j< q|S )Nz!Duplicate component name %s at %s)rS   rA   r   r   ro   )r   ZnameToPosMapr+   r>   r   r   r   Z__computeNameToPosMap  s    
z NamedTypes.__computeNameToPosMapc                 C   sv   i }d}t tt| jD ]V\}}|js.|jr:|f| }n|f}t|t| jkr\| ||< qt|tdd||< q|S )Nr   T)r9   )	reversedrV   rS   rA   r6   r7   rB   r   dict)r   ZambiguousTypesZpartialAmbiguousTypesr+   r>   r   r   r   Z__computeAmbiguousTypes  s    
z"NamedTypes.__computeAmbiguousTypesc                 C   s2   z| j | jW S  tk
r,   tdY nX dS )aT  Return ASN.1 type object by its position in fields set.

        Parameters
        ----------
        idx: :py:class:`int`
            Field index

        Returns
        -------
        :
            ASN.1 type

        Raises
        ------
        ~pyasn1.error.PyAsn1Error
            If given position is out of fields range
        Type position out of rangeN)rA   r   
IndexErrorr   rm   r*   r   r   r   getTypeByPosition  s    zNamedTypes.getTypeByPositionc                 C   s6   z| j | W S  tk
r0   td|f Y nX dS )a  Return field position by its ASN.1 type.

        Parameters
        ----------
        tagSet: :class:`~pysnmp.type.tag.TagSet`
            ASN.1 tag set distinguishing one ASN.1 type from others.

        Returns
        -------
        : :py:class:`int`
            ASN.1 type position in fields set

        Raises
        ------
        ~pyasn1.error.PyAsn1Error
            If *tagSet* is not present or ASN.1 types are not unique within callee *NamedTypes*
        zType %s not foundN)rI   KeyErrorr   rm   )r   tagSetr   r   r   getPositionByType3  s    zNamedTypes.getPositionByTypec                 C   s2   z| j | jW S  tk
r,   tdY nX dS )an  Return field name by its position in fields set.

        Parameters
        ----------
        idx: :py:class:`idx`
            Field index

        Returns
        -------
        : :py:class:`str`
            Field name

        Raises
        ------
        ~pyasn1.error.PyAsn1Error
            If given field name is not present in callee *NamedTypes*
        ru   N)rA   r   rv   r   rm   r*   r   r   r   getNameByPositionK  s    zNamedTypes.getNameByPositionc                 C   s6   z| j | W S  tk
r0   td|f Y nX dS )a|  Return field position by filed name.

        Parameters
        ----------
        name: :py:class:`str`
            Field name

        Returns
        -------
        : :py:class:`int`
            Field position in fields set

        Raises
        ------
        ~pyasn1.error.PyAsn1Error
            If *name* is not present or not unique within callee *NamedTypes*
        zName %s not foundN)rG   rx   r   rm   )r   r   r   r   r   getPositionByNamec  s    zNamedTypes.getPositionByNamec                 C   s2   z| j | jW S  tk
r,   tdY nX dS )a  Return ASN.1 types that are allowed at or past given field position.

        Some ASN.1 serialisation allow for skipping optional and defaulted fields.
        Some constructed ASN.1 types allow reordering of the fields. When recovering
        such objects it may be important to know which types can possibly be
        present at any given position in the field sets.

        Parameters
        ----------
        idx: :py:class:`int`
            Field index

        Returns
        -------
        : :class:`~pyasn1.type.tagmap.TagMap`
            Map if ASN.1 types allowed at given field position

        Raises
        ------
        ~pyasn1.error.PyAsn1Error
            If given position is out of fields range
        ru   N)rK   rp   rx   r   rm   r*   r   r   r   getTagMapNearPosition{  s    z NamedTypes.getTagMapNearPositionc                 C   s:   z|| j | | W S  tk
r4   tdY nX dS )a  Return the closest field position where given ASN.1 type is allowed.

        Some ASN.1 serialisation allow for skipping optional and defaulted fields.
        Some constructed ASN.1 types allow reordering of the fields. When recovering
        such objects it may be important to know at which field position, in field set,
        given *tagSet* is allowed at or past *idx* position.

        Parameters
        ----------
        tagSet: :class:`~pyasn1.type.tag.TagSet`
           ASN.1 type which field position to look up

        idx: :py:class:`int`
            Field position at or past which to perform ASN.1 type look up

        Returns
        -------
        : :py:class:`int`
            Field position in fields set

        Raises
        ------
        ~pyasn1.error.PyAsn1Error
            If *tagSet* is not present or not unique within callee *NamedTypes*
            or *idx* is out of fields range
        ru   N)rK   rz   rx   r   rm   )r   ry   r+   r   r   r   getPositionNearType  s    zNamedTypes.getPositionNearTypec              	   C   s\   d }| j D ]D}|j}z
|j}W n tk
r8   |j}Y nX |d ksJ||k r
|}q
|pZt S r	   )rA   r   	minTagSetAttributeErrorry   r   ZTagSet)r   r   r>   r   ry   r   r   r   Z__computeMinTagSet  s    

zNamedTypes.__computeMinTagSetc                 C   s   | j S )a  Return the minimal TagSet among ASN.1 type in callee *NamedTypes*.

        Some ASN.1 types/serialisation protocols require ASN.1 types to be
        arranged based on their numerical tag value. The *minTagSet* property
        returns that.

        Returns
        -------
        : :class:`~pyasn1.type.tagset.TagSet`
            Minimal TagSet among ASN.1 types in callee *NamedTypes*
        )rE   r(   r   r   r   r     s    zNamedTypes.minTagSetc                 C   s   i }i }d }| j D ]}|jj}t|tjr2|  S |D ]6}|rb||krbtd||| f     S |j||< q6||j |d kr|j}q|jd k	rtd| f   S qt	
|||S )Nz Non-unique tagSet %s of %s at %sz"Duplicate default ASN.1 type at %s)rA   r   rp   rq   r   ro   update	skipTypesdefaultTyper   ZTagMap)r   r:   rr   r   r   r>   rp   ry   r   r   r   Z__computeTagMaps  s"    

zNamedTypes.__computeTagMapsc                 C   s   | j S )a  Return a *TagMap* object from tags and types recursively.

        Return a :class:`~pyasn1.type.tagmap.TagMap` object by
        combining tags from *TagMap* objects of children types and
        associating them with their immediate child type.

        Example
        -------
        .. code-block:: python

           OuterType ::= CHOICE {
               innerType INTEGER
           }

        Calling *.tagMap* on *OuterType* will yield a map like this:

        .. code-block:: python

           Integer.tagSet -> Choice
        )rN   r(   r   r   r   rp     s    zNamedTypes.tagMapc                 C   s   | j S )a  Return a *TagMap* object from unique tags and types recursively.

        Return a :class:`~pyasn1.type.tagmap.TagMap` object by
        combining tags from *TagMap* objects of children types and
        associating them with their immediate child type.

        Example
        -------
        .. code-block:: python

           OuterType ::= CHOICE {
               innerType INTEGER
           }

        Calling *.tagMapUnique* on *OuterType* will yield a map like this:

        .. code-block:: python

           Integer.tagSet -> Choice

        Note
        ----

        Duplicate *TagSet* objects found in the tree of children
        types would cause error.
        )rM   r(   r   r   r   tagMapUnique  s    zNamedTypes.tagMapUniquec                 C   s   | j S r	   )rP   r(   r   r   r   hasOptionalOrDefault#  s    zNamedTypes.hasOptionalOrDefaultc                 C   s   | j S r	   )rQ   r(   r   r   r   hasOpenTypes'  s    zNamedTypes.hasOpenTypesc                 C   s
   t | jS r	   )rV   rA   r(   r   r   r   rY   +  s    zNamedTypes.namedTypesc                 C   s   | j S r	   )rT   r(   r   r   r   requiredComponents/  s    zNamedTypes.requiredComponentsN).r   r3   r4   r5   r   r   r!   r"   r#   r$   r%   r&   r)   r,   r`   r.   sysversion_infore   rf   rg   rh   ri   rj   rk   objectro   rH   rF   rJ   rw   rz   r{   r|   r}   r~   rD   r8   r   rL   rp   r   r   r   rY   r   r   r   r   r   r   u   s^   $
	!





)r   Zpyasn1r   Zpyasn1.typer   r   __all__rO   	NameErrorr   r   r   r   r   r   r   r   r   <module>   s   R