U
    ScrW                     @   s   d dl Z d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ ddd	d
gZ	G dd de
ZG dd deZeZG dd de
Ze ZG dd	 d	eZeZG dd
 d
eZeZdS )    N)error)calling)
constraint)tag)tagmapAsn1ItemAsn1TypeSimpleAsn1TypeConstructedAsn1Typec                   @   s   e Zd ZedddZdS )r      c                 C   s4   zt  j|7  _W n tk
r,   |t _Y nX t jS N)r   Z_typeCounterAttributeError)cls	increment r   4/tmp/pip-unpacked-wheel-hsjy238j/pyasn1/type/base.py	getTypeId   s
    zAsn1Item.getTypeIdN)r   )__name__
__module____qualname__classmethodr   r   r   r   r   r      s   c                   @   s   e Zd ZdZe Ze Z	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dZd$ddZedd Zd%ddZdd Zdd Zdd Zdd  Zd!d" ZdS )&r   a>  Base class for all classes representing ASN.1 types.

    In the user code, |ASN.1| class is normally used only for telling
    ASN.1 objects from others.

    Note
    ----
    For as long as ASN.1 is concerned, a way to compare ASN.1 types
    is to use :meth:`isSameTypeWith` and :meth:`isSuperTypeOf` methods.
    Nc                 K   s.   | j | jd}|| | j| || _d S )NtagSetsubtypeSpec)r   r   update__dict__	_readOnlyselfkwargsreadOnlyr   r   r   __init__3   s    
zAsn1Type.__init__c                 C   s2   |d dkr$|| j kr$td| || j|< d S )Nr   _z!read-only instance attribute "%s")r   r   PyAsn1Errorr   )r   namevaluer   r   r   __setattr__?   s    zAsn1Type.__setattr__c                 C   s   |   S r   )prettyPrintr   r   r   r   __str__E   s    zAsn1Type.__str__c                 C   s   | j S r   )r   r(   r   r   r   r    H   s    zAsn1Type.readOnlyc                 C   s   | j S )z3For |ASN.1| type is equivalent to *tagSet*
        r   r(   r   r   r   effectiveTagSetL   s    zAsn1Type.effectiveTagSetc                 C   s   t | j| iS )zvReturn a :class:`~pyasn1.type.tagmap.TagMap` object mapping ASN.1 tags to ASN.1 objects within callee object.
        )r   ZTagMapr   r(   r   r   r   tagMapR   s    zAsn1Type.tagMapTc                 C   s,   | |kp*| s| j |j ko*| p*| j|jkS )a?  Examine |ASN.1| type for equality with other ASN.1 type.

        ASN.1 tags (:py:mod:`~pyasn1.type.tag`) and constraints
        (:py:mod:`~pyasn1.type.constraint`) are examined when carrying
        out ASN.1 types comparison.

        Python class inheritance relationship is NOT considered.

        Parameters
        ----------
        other: a pyasn1 type object
            Class instance representing ASN.1 type.

        Returns
        -------
        : :class:`bool`
            :obj:`True` if *other* is |ASN.1| type,
            :obj:`False` otherwise.
        r   r   otherZ	matchTagsZmatchConstraintsr   r   r   isSameTypeWithX   s    zAsn1Type.isSameTypeWithc                 C   s(   | p&| j |j o&| p&| j|jS )al  Examine |ASN.1| type for subtype relationship with other ASN.1 type.

        ASN.1 tags (:py:mod:`~pyasn1.type.tag`) and constraints
        (:py:mod:`~pyasn1.type.constraint`) are examined when carrying
        out ASN.1 types comparison.

        Python class inheritance relationship is NOT considered.

        Parameters
        ----------
            other: a pyasn1 type object
                Class instance representing ASN.1 type.

        Returns
        -------
            : :class:`bool`
                :obj:`True` if *other* is a subtype of |ASN.1| type,
                :obj:`False` otherwise.
        )r   ZisSuperTagSetOfr   isSuperTypeOfr-   r   r   r   r0   p   s    zAsn1Type.isSuperTypeOfc                  G   s   | D ]}|t k	r dS qdS )NFT)noValue)valuesr%   r   r   r   	isNoValue   s    zAsn1Type.isNoValuer   c                 C   s
   t  d S r   )NotImplementedErrorr   Zscoper   r   r   r'      s    zAsn1Type.prettyPrintc                 C   s   | j S r   r*   r(   r   r   r   	getTagSet   s    zAsn1Type.getTagSetc                 C   s   | j S r   )r+   r(   r   r   r   getEffectiveTagSet   s    zAsn1Type.getEffectiveTagSetc                 C   s   | j S r   )r,   r(   r   r   r   	getTagMap   s    zAsn1Type.getTagMapc                 C   s   | j S r   r   r(   r   r   r   getSubtypeSpec   s    zAsn1Type.getSubtypeSpecc                 C   s   | j S r   )isValuer(   r   r   r   hasValue   s    zAsn1Type.hasValue)TT)TT)r   )r   r   r   __doc__r   ZTagSetr   r   ConstraintsIntersectionr   ZtypeIdr!   r&   r)   propertyr    r+   r,   r/   r0   staticmethodr3   r'   r6   r7   r8   r:   r<   r   r   r   r   r      s.   






c                   @   s4   e Zd ZdZedZdZdd Zdd Zdd	 Z	dS )
NoValueaz  Create a singleton instance of NoValue class.

    The *NoValue* sentinel object represents an instance of ASN.1 schema
    object as opposed to ASN.1 value object.

    Only ASN.1 schema-related operations can be performed on ASN.1
    schema objects.

    Warning
    -------
    Any operation attempted on the *noValue* object will raise the
    *PyAsn1Error* exception.
    )	__slots____getattribute____getattr__r&   __delattr__	__class__r!   __del____new____repr__r   __objclass__Zim_class
__sizeof__
__reduce____reduce_ex____getnewargs____getinitargs____getstate____setstate__Nc                    s\    j d krVdd } fddttttfD }t|D ]}t ||| q4t  _  j S )Nc                    s    fdd}|S )Nc                    s   t d  d S )N/Attempted "%s" operation on ASN.1 schema objectr   r#   )r   argskwr$   r   r   plug   s    z.NoValue.__new__.<locals>.getPlug.<locals>.plugr   )r$   rW   r   rV   r   getPlug   s    z NoValue.__new__.<locals>.getPlugc                    sL   g | ]D}t |D ]6}| jkr|d r|d rtt||r|qqS )__)dirskipMethods
startswithendswithr   callablegetattr).0typr$   r   r   r   
<listcomp>   s   
 


z#NoValue.__new__.<locals>.<listcomp>)		_instancestrintlistdictsetsetattrobjectrH   )r   rX   Zop_namesr$   r   rb   r   rH      s    


zNoValue.__new__c                 C   s(   || j krtd| td| d S )NzAttribute %s not presentrR   )r[   r   r   r#   )r   attrr   r   r   rD      s    
zNoValue.__getattr__c                 C   s   d| j j S )Nz<%s object>)rF   r   r(   r   r   r   rI      s    zNoValue.__repr__)
r   r   r   r=   ri   r[   rd   rH   rD   rI   r   r   r   r   rA      s   rA   c                   @   s   e Zd ZdZeZef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rldd Zndd Zdd Zedd ZefddZefddZd d! Zd"d# Zd)d$d%Zd*d&d'Zd(S )+r	   a  Base class for all simple classes representing ASN.1 types.

    ASN.1 distinguishes types by their ability to hold other objects.
    Scalar types are known as *simple* in ASN.1.

    In the user code, |ASN.1| class is normally used only for telling
    ASN.1 objects from others.

    Note
    ----
    For as long as ASN.1 is concerned, a way to compare ASN.1 types
    is to use :meth:`isSameTypeWith` and :meth:`isSuperTypeOf` methods.
    c                 K   sz   t j| f| |tkr| j}nR| |}z| | W n8 tjk
rn   t	 \}}}|d|| j
jf Y nX || _d S )Nz%s at %s)r   r!   r1   defaultValueprettyInr   r   r#   sysexc_inforF   r   _value)r   r%   r   ZexTypeZexValueZexTbr   r   r   r!     s    
zSimpleAsn1Type.__init__c                 C   s   d| j j| jrdpdf }| j D ]\}}|r$|d||f 7 }q$| jr|  }t|dkrx|d d d |dd   }|d	| 7 }d
| S )N%s %s objectr%   schemaz, %s %s       z...i, payload [%s]<%s>)rF   r   r;   r    itemsr'   lenr   Zrepresentationrl   r%   r   r   r   rI     s     zSimpleAsn1Type.__repr__c                 C   s   | |krdp| j |kS )NTrq   r   r.   r   r   r   __eq__%  s    zSimpleAsn1Type.__eq__c                 C   s
   | j |kS r   r{   r|   r   r   r   __ne__(  s    zSimpleAsn1Type.__ne__c                 C   s
   | j |k S r   r{   r|   r   r   r   __lt__+  s    zSimpleAsn1Type.__lt__c                 C   s
   | j |kS r   r{   r|   r   r   r   __le__.  s    zSimpleAsn1Type.__le__c                 C   s
   | j |kS r   r{   r|   r   r   r   __gt__1  s    zSimpleAsn1Type.__gt__c                 C   s
   | j |kS r   r{   r|   r   r   r   __ge__4  s    zSimpleAsn1Type.__ge__r      c                 C   s   | j r
dpdS NTFr{   r(   r   r   r   __nonzero__8  s    zSimpleAsn1Type.__nonzero__c                 C   s   | j r
dpdS r   r{   r(   r   r   r   __bool__;  s    zSimpleAsn1Type.__bool__c                 C   s
   t | jS r   )hashrq   r(   r   r   r   __hash__>  s    zSimpleAsn1Type.__hash__c                 C   s
   | j tk	S )az  Indicate that |ASN.1| object represents ASN.1 value.

        If *isValue* is :obj:`False` then this object represents just
        ASN.1 schema.

        If *isValue* is :obj:`True` then, in addition to its ASN.1 schema
        features, this object can also be used like a Python built-in object
        (e.g. :class:`int`, :class:`str`, :class:`dict` etc.).

        Returns
        -------
        : :class:`bool`
            :obj:`False` if object represents just ASN.1 schema.
            :obj:`True` if object represents ASN.1 schema and can be used as a normal value.

        Note
        ----
        There is an important distinction between PyASN1 schema and value objects.
        The PyASN1 schema objects can only participate in ASN.1 schema-related
        operations (e.g. defining or testing the structure of the data). Most
        obvious uses of ASN.1 schema is to guide serialisation codecs whilst
        encoding/decoding serialised ASN.1 contents.

        The PyASN1 value objects can **additionally** participate in many operations
        involving regular Python objects (e.g. arithmetic, comprehension etc).
        )rq   r1   r(   r   r   r   r;   A  s    zSimpleAsn1Type.isValuec                 K   s8   |t kr|s| S | j}| j }|| | j|f|S )a`  Create a modified version of |ASN.1| schema or value object.

        The `clone()` method accepts the same set arguments as |ASN.1|
        class takes on instantiation except that all arguments
        of the `clone()` method are optional.

        Whatever arguments are supplied, they are used to create a copy
        of `self` taking precedence over the ones used to instantiate `self`.

        Note
        ----
        Due to the immutable nature of the |ASN.1| object, if no arguments
        are supplied, no new |ASN.1| object will be created and `self` will
        be returned instead.
        )r1   rq   r    copyr   rF   )r   r%   r   initializersr   r   r   clone_  s    

zSimpleAsn1Type.clonec                 K   s   |t kr|s| S | j}| j }|dd}|dk	rD| j||d< |dd}|dk	rh| j||d< | D ]\}}||  |7  < qp| j	|f|S )a  Create a specialization of |ASN.1| schema or value object.

        The subtype relationship between ASN.1 types has no correlation with
        subtype relationship between Python types. ASN.1 type is mainly identified
        by its tag(s) (:py:class:`~pyasn1.type.tag.TagSet`) and value range
        constraints (:py:class:`~pyasn1.type.constraint.ConstraintsIntersection`).
        These ASN.1 type properties are implemented as |ASN.1| attributes.  

        The `subtype()` method accepts the same set arguments as |ASN.1|
        class takes on instantiation except that all parameters
        of the `subtype()` method are optional.

        With the exception of the arguments described below, the rest of
        supplied arguments they are used to create a copy of `self` taking
        precedence over the ones used to instantiate `self`.

        The following arguments to `subtype()` create a ASN.1 subtype out of
        |ASN.1| type:

        Other Parameters
        ----------------
        implicitTag: :py:class:`~pyasn1.type.tag.Tag`
            Implicitly apply given ASN.1 tag object to `self`'s
            :py:class:`~pyasn1.type.tag.TagSet`, then use the result as
            new object's ASN.1 tag(s).

        explicitTag: :py:class:`~pyasn1.type.tag.Tag`
            Explicitly apply given ASN.1 tag object to `self`'s
            :py:class:`~pyasn1.type.tag.TagSet`, then use the result as
            new object's ASN.1 tag(s).

        subtypeSpec: :py:class:`~pyasn1.type.constraint.ConstraintsIntersection`
            Add ASN.1 constraints object to one of the `self`'s, then
            use the result as new object's ASN.1 constraints.

        Returns
        -------
        :
            new instance of |ASN.1| schema or value object

        Note
        ----
        Due to the immutable nature of the |ASN.1| object, if no arguments
        are supplied, no new |ASN.1| object will be created and `self` will
        be returned instead.
        implicitTagNr   explicitTag)
r1   rq   r    r   popr   tagImplicitlytagExplicitlyrx   rF   )r   r%   r   r   r   r   argoptionr   r   r   subtypez  s    /
zSimpleAsn1Type.subtypec                 C   s   |S r   r   r   r%   r   r   r   rn     s    zSimpleAsn1Type.prettyInc                 C   s   t |S r   )re   r   r   r   r   	prettyOut  s    zSimpleAsn1Type.prettyOutc                 C   s   |  | jS r   )r   rq   r5   r   r   r   r'     s    zSimpleAsn1Type.prettyPrintc                 C   s   d| j | jjf S )Nz%s -> %s)r   rF   r   r5   r   r   r   prettyPrintType  s    zSimpleAsn1Type.prettyPrintTypeN)r   )r   )r   r   r   r=   r1   rm   r!   rI   r}   r~   r   r   r   r   ro   version_infor   r   r   r?   r;   r   r   rn   r   r'   r   r   r   r   r   r	      s,   

D
c                   @   s   e Zd ZdZdZdZe 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edd Zdd Zd d! Zd"d# Zd$d% Zd1d'd(Zd)d* Zd+d, Zd-d. Zd/d0 Z dS )2r
   a  Base class for all constructed classes representing ASN.1 types.

    ASN.1 distinguishes types by their ability to hold other objects.
    Those "nesting" types are known as *constructed* in ASN.1.

    In the user code, |ASN.1| class is normally used only for telling
    ASN.1 objects from others.

    Note
    ----
    For as long as ASN.1 is concerned, a way to compare ASN.1 types
    is to use :meth:`isSameTypeWith` and :meth:`isSuperTypeOf` methods.
    FNc                 K   s6   | j | jd}| jf |}|| tj| f| d S )N)componentTypesizeSpec)r   r   _moveSizeSpecr   r   r!   r   r   r   r   r!     s    
zConstructedAsn1Type.__init__c                 K   s>   | d| j}|r:| d| j}|r*|}n||7 }||d< |S )Nr   r   )r   r   r   )r   r   r   r   r   r   r   r     s    z!ConstructedAsn1Type._moveSizeSpecc                 C   sx   d| j j| jrdpdf }| j D ] \}}|tk	r$|d||f 7 }q$| jrp| jrp|dddd | jD  7 }d	| S )
Nrr   r%   rs   z, %s=%rrv   z, c                 S   s   g | ]}t |qS r   )repr)r`   xr   r   r   rc   !  s     z0ConstructedAsn1Type.__repr__.<locals>.<listcomp>rw   )rF   r   r;   r    rx   r1   
componentsjoinrz   r   r   r   rI     s     zConstructedAsn1Type.__repr__c                 C   s   | |kp| j |kS r   r   r|   r   r   r   r}   %  s    zConstructedAsn1Type.__eq__c                 C   s
   | j |kS r   r   r|   r   r   r   r~   (  s    zConstructedAsn1Type.__ne__c                 C   s
   | j |k S r   r   r|   r   r   r   r   +  s    zConstructedAsn1Type.__lt__c                 C   s
   | j |kS r   r   r|   r   r   r   r   .  s    zConstructedAsn1Type.__le__c                 C   s
   | j |kS r   r   r|   r   r   r   r   1  s    zConstructedAsn1Type.__gt__c                 C   s
   | j |kS r   r   r|   r   r   r   r   4  s    zConstructedAsn1Type.__ge__r   r   c                 C   s
   t | jS r   boolr   r(   r   r   r   r   8  s    zConstructedAsn1Type.__nonzero__c                 C   s
   t | jS r   r   r(   r   r   r   r   ;  s    zConstructedAsn1Type.__bool__c                 C   s   t dd S NzMethod not implementedrS   r(   r   r   r   r   >  s    zConstructedAsn1Type.componentsc                 C   s   d S r   r   )r   ZmyClonecloneValueFlagr   r   r   _cloneComponentValuesB  s    z)ConstructedAsn1Type._cloneComponentValuesc                 K   s@   | dd}| j }|| | jf |}|r<| || |S )a	  Create a modified version of |ASN.1| schema object.

        The `clone()` method accepts the same set arguments as |ASN.1|
        class takes on instantiation except that all arguments
        of the `clone()` method are optional.

        Whatever arguments are supplied, they are used to create a copy
        of `self` taking precedence over the ones used to instantiate `self`.

        Possible values of `self` are never copied over thus `clone()` can
        only create a new schema object.

        Returns
        -------
        :
            new instance of |ASN.1| type/value

        Note
        ----
        Due to the mutable nature of the |ASN.1| object, even if no arguments
        are supplied, a new |ASN.1| object will be created and returned.
        r   F)r   r    r   r   rF   r   )r   r   r   r   r   r   r   r   r   E  s    

zConstructedAsn1Type.clonec           	      K   s   | j  }|dd}|dd}|dk	r:| j||d< |dd}|dk	r^| j||d< | D ]\}}||  |7  < qf| jf |}|r| || |S )a  Create a specialization of |ASN.1| schema object.

        The `subtype()` method accepts the same set arguments as |ASN.1|
        class takes on instantiation except that all parameters
        of the `subtype()` method are optional.

        With the exception of the arguments described below, the rest of
        supplied arguments they are used to create a copy of `self` taking
        precedence over the ones used to instantiate `self`.

        The following arguments to `subtype()` create a ASN.1 subtype out of
        |ASN.1| type.

        Other Parameters
        ----------------
        implicitTag: :py:class:`~pyasn1.type.tag.Tag`
            Implicitly apply given ASN.1 tag object to `self`'s
            :py:class:`~pyasn1.type.tag.TagSet`, then use the result as
            new object's ASN.1 tag(s).

        explicitTag: :py:class:`~pyasn1.type.tag.Tag`
            Explicitly apply given ASN.1 tag object to `self`'s
            :py:class:`~pyasn1.type.tag.TagSet`, then use the result as
            new object's ASN.1 tag(s).

        subtypeSpec: :py:class:`~pyasn1.type.constraint.ConstraintsIntersection`
            Add ASN.1 constraints object to one of the `self`'s, then
            use the result as new object's ASN.1 constraints.


        Returns
        -------
        :
            new instance of |ASN.1| type/value

        Note
        ----
        Due to the mutable nature of the |ASN.1| object, even if no arguments
        are supplied, a new |ASN.1| object will be created and returned.
        r   Fr   Nr   r   )	r    r   r   r   r   r   rx   rF   r   )	r   r   r   r   r   r   r   r   r   r   r   r   r   h  s    *
zConstructedAsn1Type.subtypec                 C   s   t dd S r   rS   )r   idxr   r   r   getComponentByPosition  s    z*ConstructedAsn1Type.getComponentByPositionTc                 C   s   t dd S r   rS   )r   r   r%   ZverifyConstraintsr   r   r   setComponentByPosition  s    z*ConstructedAsn1Type.setComponentByPositionc                 O   s4   t |D ]\}}|| |< q|D ]}|| | |< q| S r   )	enumerate)r   rT   r   r   r%   kr   r   r   setComponents  s
    
z!ConstructedAsn1Type.setComponentsc                 C   s   d S r   r   r(   r   r   r   setDefaultComponents  s    z(ConstructedAsn1Type.setDefaultComponentsc                 C   s   | j S r   )r   r(   r   r   r   getComponentType  s    z$ConstructedAsn1Type.getComponentTypec                 C   s   |  |  d S r   r9   r(   r   r   r   verifySizeSpec  s    z"ConstructedAsn1Type.verifySizeSpec)T)!r   r   r   r=   ZstrictConstraintsr   r   r>   r   r!   r   rI   r}   r~   r   r   r   r   ro   r   r   r   r?   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r
     s6   

#@
	)ro   Zpyasn1r   Zpyasn1.compatr   Zpyasn1.typer   r   r   __all__rk   r   r   ZAsn1ItemBaserA   r1   r	   ZAbstractSimpleAsn1Itemr
   ZAbstractConstructedAsn1Itemr   r   r   r   <module>   s(   
 	J W c