B
    à%Q[9@  ã               @   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y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   úYC:\Users\HIRONO~1\AppData\Local\Temp\pip-install-6i93dh7p\pyasn1\pyasn1\type\namedtype.pyÚ<lambda>   s    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 )N)Ú_NamedType__nameÚ_NamedType__typeÚ_NamedType__nameAndTypeÚ_NamedType__openType)ÚselfÚnameÚ
asn1ObjectÚopenTyper   r   r   Ú__init__*   s    
zNamedType.__init__c             C   s:   d| j | jf }| jr$|d| j 7 }d| jjt| ƒ|f S )Nz%s=%rz openType: %rz<%s object at 0x%x type %s>)r   r   r   Ú	__class__Ú__name__Úid)r   Úrepresentationr   r   r   Ú__repr__0   s    zNamedType.__repr__c             C   s
   | j |kS )N)r   )r   Úotherr   r   r   Ú__eq__8   s    zNamedType.__eq__c             C   s
   | j |kS )N)r   )r   r   r   r   r   Ú__ne__;   s    zNamedType.__ne__c             C   s
   | j |k S )N)r   )r   r   r   r   r   Ú__lt__>   s    zNamedType.__lt__c             C   s
   | j |kS )N)r   )r   r   r   r   r   Ú__le__A   s    zNamedType.__le__c             C   s
   | j |kS )N)r   )r   r   r   r   r   Ú__gt__D   s    zNamedType.__gt__c             C   s
   | j |kS )N)r   )r   r   r   r   r   Ú__ge__G   s    zNamedType.__ge__c             C   s
   t | jƒS )N)Úhashr   )r   r   r   r   Ú__hash__J   s    zNamedType.__hash__c             C   s
   | j | S )N)r   )r   Úidxr   r   r   Ú__getitem__M   s    zNamedType.__getitem__c             C   s
   t | jƒS )N)Úiterr   )r   r   r   r   Ú__iter__P   s    zNamedType.__iter__c             C   s   | j S )N)r   )r   r   r   r   r   S   s    zNamedType.namec             C   s   | j S )N)r   )r   r   r   r   r   W   s    zNamedType.asn1Objectc             C   s   | j S )N)r   )r   r   r   r   r   [   s    zNamedType.openTypec             C   s   | j S )N)r   )r   r   r   r   ÚgetNamea   s    zNamedType.getNamec             C   s   | j S )N)r   )r   r   r   r   ÚgetTyped   s    zNamedType.getType)N)r   Ú
__module__Ú__qualname__Ú__doc__Ú
isOptionalÚisDefaultedr   r   r   r   r    r!   r"   r#   r%   r'   r)   Úpropertyr   r   r   r*   r+   r   r   r   r   r      s&   
c               @   s   e Zd ZejZdZdS )r   TN)r   r,   r-   r   r.   r/   r   r   r   r   r   h   s   c               @   s   e Zd ZejZdZdS )r   TN)r   r,   r-   r   r.   r0   r   r   r   r   r   n   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r„d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)r0   r/   )Ú.0Ú	namedTyper   r   r   ú
<listcomp>¢   s    z'NamedTypes.__init__.<locals>.<listcomp>c             S   s   g | ]}|j rd ‘qS )T)r   )r4   r5   r   r   r   r6   ¤   s    c             S   s    g | ]\}}|j s|js|‘qS r   )r/   r0   )r4   r&   Úntr   r   r   r6   ¨   s    c             S   s   g | ]
}|j ‘qS r   )r   )r4   r5   r   r   r   r6   ª   s    c             S   s   g | ]
}|j ‘qS r   )r   )r4   r5   r   r   r   r6   «   s    c             S   s   g | ]}|j |jf‘qS r   )r   r   )r4   r5   r   r   r   r6   ¬   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t| ƒ|f S )Nz, c             S   s   g | ]}d | ‘qS )z%rr   )r4   r   r   r   r   r6   ¯   s    z'NamedTypes.__repr__.<locals>.<listcomp>z<%s object at 0x%x types %s>)Újoinr8   r   r   r   )r   r   r   r   r   r   ®   s    zNamedTypes.__repr__c             C   s
   | j |kS )N)r8   )r   r   r   r   r   r   ²   s    zNamedTypes.__eq__c             C   s
   | j |kS )N)r8   )r   r   r   r   r   r   µ   s    zNamedTypes.__ne__c             C   s
   | j |k S )N)r8   )r   r   r   r   r   r    ¸   s    zNamedTypes.__lt__c             C   s
   | j |kS )N)r8   )r   r   r   r   r   r!   »   s    zNamedTypes.__le__c             C   s
   | j |kS )N)r8   )r   r   r   r   r   r"   ¾   s    zNamedTypes.__gt__c             C   s
   | j |kS )N)r8   )r   r   r   r   r   r#   Á   s    zNamedTypes.__ge__c             C   s
   t | jƒS )N)r$   r8   )r   r   r   r   r%   Ä   s    zNamedTypes.__hash__c             C   s0   y
| j | S  tk
r*   | j | j|  S X d S )N)r8   Ú	TypeErrorr>   )r   r&   r   r   r   r'   Ç   s    
zNamedTypes.__getitem__c             C   s
   || j kS )N)r>   )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   )r4   r   r   r   r   ú	<genexpr>Ò   s    z&NamedTypes.__iter__.<locals>.<genexpr>)r8   )r   r   r   r   r)   Ñ   s    zNamedTypes.__iter__r   é   c             C   s
   | j dkS )Nr   )r:   )r   r   r   r   Ú__nonzero__Õ   s    zNamedTypes.__nonzero__c             C   s
   | j dkS )Nr   )r:   )r   r   r   r   Ú__bool__Ø   s    zNamedTypes.__bool__c             C   s   | j S )N)r:   )r   r   r   r   Ú__len__Û   s    zNamedTypes.__len__c             C   s   | j S )N)rN   )r   r   r   r   Úvaluesà   s    zNamedTypes.valuesc             C   s   | j S )N)rL   )r   r   r   r   Úkeysã   s    zNamedTypes.keysc             C   s   | j S )N)rO   )r   r   r   r   Úitemsæ   s    zNamedTypes.itemsc             C   s   | j | jŽ S )N)r   r8   )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 )N)Ú_PostponedError__errorMsg)r   ZerrorMsgr   r   r   r   í   s    z"NamedTypes.PostponedError.__init__c             C   s   t  | j¡‚d S )N)r   ÚPyAsn1Errorr_   )r   Úitemr   r   r   r'   ð   s    z%NamedTypes.PostponedError.__getitem__N)r   r,   r-   r   r'   r   r   r   r   ÚPostponedErrorì   s   rb   c             C   sp   i }xft | jƒD ]X\}}|jj}t|tjƒr0|S |s6qx0|jD ]&}||kr\t d||f ¡S |||< q>W qW |S )Nz Duplicate component tag %s at %s)rJ   r8   r   ÚtagMapÚ
isinstancer   rb   ÚpresentTypes)r   ZtagToPosMapr&   r5   rc   Z_tagSetr   r   r   Z__computeTagToPosMapó   s    zNamedTypes.__computeTagToPosMapc             C   sH   i }x>t | jƒD ]0\}}|j|kr6t d|j|f ¡S |||j< qW |S )Nz!Duplicate component name %s at %s)rJ   r8   r   r   rb   )r   ZnameToPosMapr&   r5   r   r   r   Z__computeNameToPosMap  s    
z NamedTypes.__computeNameToPosMapc             C   sz   i }d}xlt tt| jƒƒƒD ]V\}}|js0|jr<|f| }n|f}t|ƒt| jƒkr^| ||< qt|tdd�Ž||< qW |S )Nr   T)r2   )	ÚreversedrM   rJ   r8   r/   r0   r9   r   Údict)r   ZambigiousTypesZpartialAmbigiousTypesr&   r5   r   r   r   Z__computeAmbiguousTypes  s    
z"NamedTypes.__computeAmbiguousTypesc             C   s0   y| j | jS  tk
r*   t d¡‚Y nX dS )a_  Return ASN.1 type object by its position in fields set.

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

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

        Raises
        ------
        : :class:`~pyasn1.error.PyAsn1Error`
            If given position is out of fields range
        zType position out of rangeN)r8   r   Ú
IndexErrorr   r`   )r   r&   r   r   r   ÚgetTypeByPosition  s    zNamedTypes.getTypeByPositionc             C   s4   y
| j | S  tk
r.   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
        ------
        : :class:`~pyasn1.error.PyAsn1Error`
            If *tagSet* is not present or ASN.1 types are not unique within callee *NamedTypes*
        zType %s not foundN)r@   ÚKeyErrorr   r`   )r   ÚtagSetr   r   r   ÚgetPositionByType1  s    
zNamedTypes.getPositionByTypec             C   s0   y| j | jS  tk
r*   t d¡‚Y nX dS )ay  Return field name by its position in fields set.

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

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

        Raises
        ------
        : :class:`~pyasn1.error.PyAsn1Error`
            If given field name is not present in callee *NamedTypes*
        zType position out of rangeN)r8   r   rh   r   r`   )r   r&   r   r   r   ÚgetNameByPositionI  s    zNamedTypes.getNameByPositionc             C   s4   y
| j | S  tk
r.   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
        ------
        : :class:`~pyasn1.error.PyAsn1Error`
            If *name* is not present or not unique within callee *NamedTypes*
        zName %s not foundN)r>   rj   r   r`   )r   r   r   r   r   ÚgetPositionByNamea  s    
zNamedTypes.getPositionByNamec             C   s0   y| j | j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
        ------
        : :class:`~pyasn1.error.PyAsn1Error`
            If given position is out of fields range
        zType position out of rangeN)rB   rc   rj   r   r`   )r   r&   r   r   r   ÚgetTagMapNearPositiony  s    z NamedTypes.getTagMapNearPositionc             C   s8   y|| j |  |¡ S  tk
r2   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
        ------
        : :class:`~pyasn1.error.PyAsn1Error`
            If *tagSet* is not present or not unique within callee *NamedTypes*
            or *idx* is out of fields range
        zType position out of rangeN)rB   rl   rj   r   r`   )r   rk   r&   r   r   r   ÚgetPositionNearType–  s    zNamedTypes.getPositionNearTypec          	   C   s`   d }xN| j D ]D}|j}y
|j}W n tk
r:   |j}Y nX |d ksL||k r|}qW |p^t ¡ S )N)r8   r   Ú	minTagSetÚAttributeErrorrk   r   ZTagSet)r   rq   r5   r   rk   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*
        )r<   )r   r   r   r   rq   Ç  s    zNamedTypes.minTagSetc             C   s°   i }i }d }x”| j D ]Š}|jj}t|tjƒr0|S x6|D ].}|rZ||krZt d||| f ¡S |j||< q6W | |j¡ |d kr„|j}q|jd k	rt d| f ¡S qW t	 
|||¡S )Nz Non-unique tagSet %s of %s at %sz"Duplicate default ASN.1 type at %s)r8   r   rc   rd   r   rb   ÚupdateÚ	skipTypesÚdefaultTyper   ZTagMap)r   r3   re   rt   ru   r5   rc   rk   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
        )rE   )r   r   r   r   rc   ë  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.
        )rD   )r   r   r   r   ÚtagMapUnique  s    zNamedTypes.tagMapUniquec             C   s   | j S )N)rG   )r   r   r   r   ÚhasOptionalOrDefault!  s    zNamedTypes.hasOptionalOrDefaultc             C   s   | j S )N)rH   )r   r   r   r   ÚhasOpenTypes%  s    zNamedTypes.hasOpenTypesc             C   s
   t | jƒS )N)rM   r8   )r   r   r   r   rP   )  s    zNamedTypes.namedTypesc             C   s   | j S )N)rK   )r   r   r   r   ÚrequiredComponents-  s    zNamedTypes.requiredComponentsN).r   r,   r-   r.   r   r   r   r   r    r!   r"   r#   r%   r'   rU   r)   ÚsysÚversion_inforX   rY   rZ   r[   r\   r]   r^   Úobjectrb   r?   r=   rA   ri   rl   rm   rn   ro   rp   r;   r1   rq   rC   rc   rv   rw   rx   rP   ry   r   r   r   r   r   t   sP   $
	!)rz   Zpyasn1r   Zpyasn1.typer   r   Ú__all__rF   Ú	NameErrorr|   r   r   r   r   r   r   r   r   Ú<module>   s   Q