U
    Scy                    @   sT  d dl Z 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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l
mZ ejZe ZddddddddddddddddddgZG dd dejZG d d deZejd  d!k reZneZG d"d# d#eZG d$d dejZze W n ek
rJ   d%d& ZY nX G d'd dejZG d(d deZ ejd  d)kreefZ!nefZ!e!e"f Z#G d*d dejZ$G d+d dejZ%G d,d deZ&G d-d dej'Z(G d.d de(Z)G d/d de(Z*G d0d dej'Z+G d1d de+Z,G d2d de+Z-G d3d de-Z.G d4d deZ/dS )5    N)error)eoo)binary)integer)octets)base)
constraint)	namedtype)namedval)tag)tagmapIntegerBoolean	BitStringOctetStringNullObjectIdentifierReal
EnumeratedSequenceOfAndSetOfBase
SequenceOfSetOfSequenceAndSetBaseSequenceSetChoiceAnyNoValuenoValuec                   @   s  e Zd ZdZeeejejdZ	e
 Ze Zej 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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%dfd&d'Z&d(d) Z'd*d+ Z(d,d- Z)e*j+d. dkrd/d0 Z,d1d2 Z-n(d3d4 Z.d5d6 Z/d7d8 Z0d9d: Z1ejj2Z2d;d< Z3e*j+d. dkrVd=d> Z4d?d@ Z5dAdB Z6dCdD Z7dEdF Z8dGdH Z9dIdJ Z:dgdKdLZ;dMdN Z<dOdP Z=e*j+d.d dQkrdRdS Z>dTdU Z?dVdW Z@dXdY ZAdZd[ ZBd\d] ZCd^d_ ZDd`da ZEdbdc ZFddde ZGd%S )hr   a  Create |ASN.1| schema or value object.

    |ASN.1| class is based on :class:`~pyasn1.type.base.SimpleAsn1Type`, its
    objects are immutable and duck-type Python :class:`int` objects.

    Keyword Args
    ------------
    value: :class:`int`, :class:`str` or |ASN.1| object
        Python :class:`int` or :class:`str` literal or |ASN.1| class
        instance. If `value` is not given, schema object will be created.

    tagSet: :py:class:`~pyasn1.type.tag.TagSet`
        Object representing non-default ASN.1 tag(s)

    subtypeSpec: :py:class:`~pyasn1.type.constraint.ConstraintsIntersection`
        Object representing non-default ASN.1 subtype constraint(s). Constraints
        verification for |ASN.1| type occurs automatically on object
        instantiation.

    namedValues: :py:class:`~pyasn1.type.namedval.NamedValues`
        Object representing non-default symbolic aliases for numbers

    Raises
    ------
    ~pyasn1.error.ValueConstraintError, ~pyasn1.error.PyAsn1Error
        On constraint violation or bad initializer.

    Examples
    --------

    .. code-block:: python

        class ErrorCode(Integer):
            '''
            ASN.1 specification:

            ErrorCode ::=
                INTEGER { disk-full(1), no-disk(-1),
                          disk-not-formatted(2) }

            error ErrorCode ::= disk-full
            '''
            namedValues = NamedValues(
                ('disk-full', 1), ('no-disk', -1),
                ('disk-not-formatted', 2)
            )

        error = ErrorCode('disk-full')
       c                 K   s(   d|kr| j |d< tjj| |f| d S )NnamedValues)r    r   SimpleAsn1Type__init__selfvaluekwargs r'   4/tmp/pip-unpacked-wheel-hsjy238j/pyasn1/type/univ.pyr"   g   s    
zInteger.__init__c                 C   s   |  | j|@ S Nclone_valuer$   r%   r'   r'   r(   __and__m   s    zInteger.__and__c                 C   s   |  || j@ S r)   r*   r-   r'   r'   r(   __rand__p   s    zInteger.__rand__c                 C   s   |  | j|B S r)   r*   r-   r'   r'   r(   __or__s   s    zInteger.__or__c                 C   s   |  || jB S r)   r*   r-   r'   r'   r(   __ror__v   s    zInteger.__ror__c                 C   s   |  | j|A S r)   r*   r-   r'   r'   r(   __xor__y   s    zInteger.__xor__c                 C   s   |  || jA S r)   r*   r-   r'   r'   r(   __rxor__|   s    zInteger.__rxor__c                 C   s   |  | j|> S r)   r*   r-   r'   r'   r(   
__lshift__   s    zInteger.__lshift__c                 C   s   |  | j|? S r)   r*   r-   r'   r'   r(   
__rshift__   s    zInteger.__rshift__c                 C   s   |  | j| S r)   r*   r-   r'   r'   r(   __add__   s    zInteger.__add__c                 C   s   |  || j S r)   r*   r-   r'   r'   r(   __radd__   s    zInteger.__radd__c                 C   s   |  | j| S r)   r*   r-   r'   r'   r(   __sub__   s    zInteger.__sub__c                 C   s   |  || j S r)   r*   r-   r'   r'   r(   __rsub__   s    zInteger.__rsub__c                 C   s   |  | j| S r)   r*   r-   r'   r'   r(   __mul__   s    zInteger.__mul__c                 C   s   |  || j S r)   r*   r-   r'   r'   r(   __rmul__   s    zInteger.__rmul__c                 C   s   |  | j| S r)   r*   r-   r'   r'   r(   __mod__   s    zInteger.__mod__c                 C   s   |  || j S r)   r*   r-   r'   r'   r(   __rmod__   s    zInteger.__rmod__Nc                 C   s   |  t| j||S r)   r+   powr,   r$   r%   Zmodulor'   r'   r(   __pow__   s    zInteger.__pow__c                 C   s   |  t|| jS r)   r>   r-   r'   r'   r(   __rpow__   s    zInteger.__rpow__c                 C   s   |  | j| S r)   r*   r-   r'   r'   r(   __floordiv__   s    zInteger.__floordiv__c                 C   s   |  || j S r)   r*   r-   r'   r'   r(   __rfloordiv__   s    zInteger.__rfloordiv__r   c                 C   s,   t |trt| j| S | | j| S d S r)   
isinstancefloatr   r,   r+   r-   r'   r'   r(   __div__   s    
zInteger.__div__c                 C   s,   t |trt|| j S | || j S d S r)   rE   r-   r'   r'   r(   __rdiv__   s    
zInteger.__rdiv__c                 C   s   t | j| S r)   r   r,   r-   r'   r'   r(   __truediv__   s    zInteger.__truediv__c                 C   s   t || j S r)   rJ   r-   r'   r'   r(   __rtruediv__   s    zInteger.__rtruediv__c                 C   s   |  t| j|S r)   r+   divmodr,   r-   r'   r'   r(   
__divmod__   s    zInteger.__divmod__c                 C   s   |  t|| jS r)   rM   r-   r'   r'   r(   __rdivmod__   s    zInteger.__rdivmod__c                 C   s
   t | jS r)   intr,   r$   r'   r'   r(   __int__   s    zInteger.__int__c                 C   s
   t | jS r)   )longr,   rS   r'   r'   r(   __long__   s    zInteger.__long__c                 C   s
   t | jS r)   rG   r,   rS   r'   r'   r(   	__float__   s    zInteger.__float__c                 C   s   |  t| jS r)   )r+   absr,   rS   r'   r'   r(   __abs__   s    zInteger.__abs__c                 C   s
   t | jS r)   rQ   rS   r'   r'   r(   	__index__   s    zInteger.__index__c                 C   s   |  | j
 S r)   r*   rS   r'   r'   r(   __pos__   s    zInteger.__pos__c                 C   s   |  | j S r)   r*   rS   r'   r'   r(   __neg__   s    zInteger.__neg__c                 C   s   |  | j S r)   r*   rS   r'   r'   r(   
__invert__   s    zInteger.__invert__c                 C   s"   t | j|}|r| |S |S d S r)   )roundr,   r+   r$   nrr'   r'   r(   	__round__   s    
zInteger.__round__c                 C   s   t | jS r)   )mathfloorr,   rS   r'   r'   r(   	__floor__   s    zInteger.__floor__c                 C   s   t | jS r)   )rd   ceilr,   rS   r'   r'   r(   __ceil__   s    zInteger.__ceil__r      c                 C   s   |  t| jS r)   )r+   rd   truncr,   rS   r'   r'   r(   	__trunc__   s    zInteger.__trunc__c                 C   s
   | j |k S r)   r,   r-   r'   r'   r(   __lt__   s    zInteger.__lt__c                 C   s
   | j |kS r)   rm   r-   r'   r'   r(   __le__   s    zInteger.__le__c                 C   s
   | j |kS r)   rm   r-   r'   r'   r(   __eq__   s    zInteger.__eq__c                 C   s
   | j |kS r)   rm   r-   r'   r'   r(   __ne__   s    zInteger.__ne__c                 C   s
   | j |kS r)   rm   r-   r'   r'   r(   __gt__   s    zInteger.__gt__c                 C   s
   | j |kS r)   rm   r-   r'   r'   r(   __ge__   s    zInteger.__ge__c                 C   sd   z
t |W S  tk
r^   z| j| W  Y S  tk
rX   td|t d f Y nX Y nX d S )Nz Can't coerce %r into integer: %s   )rR   
ValueErrorr    KeyErrorr   PyAsn1Errorsysexc_infor-   r'   r'   r(   prettyIn   s    
zInteger.prettyInc                 C   s2   zt | j| W S  tk
r,   t | Y S X d S r)   )strr    rv   r-   r'   r'   r(   	prettyOut  s    zInteger.prettyOutc                 C   s   | j S r)   r    rS   r'   r'   r(   getNamedValues  s    zInteger.getNamedValues)N)r   )H__name__
__module____qualname____doc__r   
initTagSetTagtagClassUniversaltagFormatSimpletagSetr   ConstraintsIntersectionsubtypeSpecr
   NamedValuesr    r   r!   	getTypeIdtypeIdr   r"   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   rA   rB   rC   rD   rx   version_inforH   rI   rK   rL   rO   rP   __hash__rT   rV   rX   rZ   r[   r\   r]   r^   rc   rf   rh   rl   rn   ro   rp   rq   rr   rs   rz   r|   r~   r'   r'   r'   r(   r   "   sv   4



	c                   @   sN   e Zd ZdZeeejejdZ	e
jedd ZeddZe
 ZdS )r   ad  Create |ASN.1| schema or value object.

    |ASN.1| class is based on :class:`~pyasn1.type.base.SimpleAsn1Type`, its
    objects are immutable and duck-type Python :class:`int` objects.

    Keyword Args
    ------------
    value: :class:`int`, :class:`str` or |ASN.1| object
        Python :class:`int` or :class:`str` literal or |ASN.1| class
        instance. If `value` is not given, schema object will be created.

    tagSet: :py:class:`~pyasn1.type.tag.TagSet`
        Object representing non-default ASN.1 tag(s)

    subtypeSpec: :py:class:`~pyasn1.type.constraint.ConstraintsIntersection`
        Object representing non-default ASN.1 subtype constraint(s).Constraints
        verification for |ASN.1| type occurs automatically on object
        instantiation.

    namedValues: :py:class:`~pyasn1.type.namedval.NamedValues`
        Object representing non-default symbolic aliases for numbers

    Raises
    ------
    ~pyasn1.error.ValueConstraintError, ~pyasn1.error.PyAsn1Error
        On constraint violation or bad initializer.

    Examples
    --------
    .. code-block:: python

        class RoundResult(Boolean):
            '''
            ASN.1 specification:

            RoundResult ::= BOOLEAN

            ok RoundResult ::= TRUE
            ko RoundResult ::= FALSE
            '''
        ok = RoundResult(True)
        ko = RoundResult(False)
    rt   r   )Falser   )Truert   N)r   r   r   r   r   r   r   r   r   r   r   r   r   SingleValueConstraintr
   r   r    r   r   r'   r'   r'   r(   r     s   .   c                   @   s$   e Zd Zd ZZdd Zdd ZdS )SizedIntegerNc                 C   s    || _ t|t |  d| _| S Nr   )	bitLengthmaxr   leadingZeroBits)r$   r   r'   r'   r(   setBitLengthb  s    zSizedInteger.setBitLengthc                 C   s    | j d kr| t |  | j S r)   )r   r   r   rS   r'   r'   r(   __len__g  s    
zSizedInteger.__len__)r   r   r   r   r   r   r   r'   r'   r'   r(   r   _  s   r   c                   @   sX  e Zd ZdZeeejejdZ	e
 Ze Zej Ze Z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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-d.d/ Z.d0d1 Z/d2d3 Z0d4d5 Z1e2d@d8d9Z3e2dAd:d;Z4e2dBd<d=Z5d>d? Z6d7S )Cr   a  Create |ASN.1| schema or value object.

    |ASN.1| class is based on :class:`~pyasn1.type.base.SimpleAsn1Type`, its
    objects are immutable and duck-type both Python :class:`tuple` (as a tuple
    of bits) and :class:`int` objects.

    Keyword Args
    ------------
    value: :class:`int`, :class:`str` or |ASN.1| object
        Python :class:`int` or :class:`str` literal representing binary
        or hexadecimal number or sequence of integer bits or |ASN.1| object.
        If `value` is not given, schema object will be created.

    tagSet: :py:class:`~pyasn1.type.tag.TagSet`
        Object representing non-default ASN.1 tag(s)

    subtypeSpec: :py:class:`~pyasn1.type.constraint.ConstraintsIntersection`
        Object representing non-default ASN.1 subtype constraint(s). Constraints
        verification for |ASN.1| type occurs automatically on object
        instantiation.

    namedValues: :py:class:`~pyasn1.type.namedval.NamedValues`
        Object representing non-default symbolic aliases for numbers

    binValue: :py:class:`str`
        Binary string initializer to use instead of the *value*.
        Example: '10110011'.

    hexValue: :py:class:`str`
        Hexadecimal string initializer to use instead of the *value*.
        Example: 'DEADBEEF'.

    Raises
    ------
    ~pyasn1.error.ValueConstraintError, ~pyasn1.error.PyAsn1Error
        On constraint violation or bad initializer.

    Examples
    --------
    .. code-block:: python

        class Rights(BitString):
            '''
            ASN.1 specification:

            Rights ::= BIT STRING { user-read(0), user-write(1),
                                    group-read(2), group-write(3),
                                    other-read(4), other-write(5) }

            group1 Rights ::= { group-read, group-write }
            group2 Rights ::= '0011'B
            group3 Rights ::= '3'H
            '''
            namedValues = NamedValues(
                ('user-read', 0), ('user-write', 1),
                ('group-read', 2), ('group-write', 3),
                ('other-read', 4), ('other-write', 5)
            )

        group1 = Rights(('group-read', 'group-write'))
        group2 = Rights('0011')
        group3 = Rights(0x3)
    r   c                 K   s   |t krh|rhz| j|ddd}W n tk
r8   Y nX z| j|ddd}W n tk
rf   Y nX |t kr| jt k	r| j| jdd}n| jt k	r| j| jdd}d|kr| j|d< tj	j
| |f| d S )NbinValueTinternalFormathexValuer    )r   fromBinaryStringpoprv   fromHexStringdefaultBinValuedefaultHexValuer    r   r!   r"   r#   r'   r'   r(   r"     s$    


zBitString.__init__c                 C   s   |   S r)   )asBinaryrS   r'   r'   r(   __str__  s    zBitString.__str__c                 C   s.   |  |}| |kp,| j|ko,t| jt|kS r)   rz   r,   lenr$   otherr'   r'   r(   rp     s    
zBitString.__eq__c                 C   s&   |  |}| j|kp$t| jt|kS r)   r   r   r'   r'   r(   rq     s    
zBitString.__ne__c                 C   s8   |  |}t| jt|k p6t| jt|ko6| j|k S r)   rz   r   r,   r   r'   r'   r(   rn     s    
zBitString.__lt__c                 C   s8   |  |}t| jt|kp6t| jt|ko6| j|kS r)   r   r   r'   r'   r(   ro     s    
zBitString.__le__c                 C   s8   |  |}t| jt|kp6t| jt|ko6| j|kS r)   r   r   r'   r'   r(   rr     s    
zBitString.__gt__c                 C   s8   |  |}t| jt|kp6t| jt|ko6| j|kS r)   r   r   r'   r'   r(   rs     s    
zBitString.__ge__c                 C   s
   t | jS r)   r   r,   rS   r'   r'   r(   r     s    zBitString.__len__c                    sl   |j tkr0  fddt|t  D S t jd }||ksN|dk rVtd j|| ? d@ S d S )Nc                    s   g | ]} | qS r'   r'   .0xrS   r'   r(   
<listcomp>   s     z)BitString.__getitem__.<locals>.<listcomp>rt   r   zbit index out of range)	__class__slicer+   rangeindicesr   r,   
IndexError)r$   ilengthr'   rS   r(   __getitem__  s    
&zBitString.__getitem__c                 c   s,   t | j}|r(|d8 }| j|? d@ V  q
d S Nrt   r   )r$   r   r'   r'   r(   __iter__  s    
zBitString.__iter__c                 C   s   t t| S r)   )reversedtuplerS   r'   r'   r(   __reversed__  s    zBitString.__reversed__c                 C   s:   |  |}| t| jt|> |B t| jt| S r)   )rz   r+   r   r,   r   r   r-   r'   r'   r(   r6     s    
zBitString.__add__c                 C   s<   |  |}| t|t| j> | jB t| jt| S r)   )rz   r+   r   r   r,   r   r-   r'   r'   r(   r7     s    
zBitString.__radd__c                 C   s:   | j }|dkr0|t| j K }|| j O }|d8 }q| |S r   )r,   r   r+   )r$   r%   Z	bitStringr'   r'   r(   r:     s    

zBitString.__mul__c                 C   s   | | S r)   r'   r-   r'   r'   r(   r;   "  s    zBitString.__rmul__c                 C   s$   |  t| j|> t| j| S r)   )r+   r   r,   r   r   r$   countr'   r'   r(   r4   %  s    zBitString.__lshift__c                 C   s*   |  t| j|? tdt| j| S r   )r+   r   r,   r   r   r   r   r'   r'   r(   r5   (  s    zBitString.__rshift__c                 C   s   | j S r)   rm   rS   r'   r'   r(   rT   +  s    zBitString.__int__c                 C   s
   t | jS r)   rW   rS   r'   r'   r(   rX   .  s    zBitString.__float__r   c                 C   s   | j S r)   rm   rS   r'   r'   r(   rV   2  s    zBitString.__long__c                 C   s   t t|  S )zGet |ASN.1| value as a sequence of 8-bit integers.

        If |ASN.1| object length is not a multiple of 8, result
        will be left-padded with zeros.
        )r   r   Z	octs2intsasOctetsrS   r'   r'   r(   	asNumbers5  s    zBitString.asNumbersc                 C   s   t j| jt| dS )zGet |ASN.1| value as a sequence of octets.

        If |ASN.1| object length is not a multiple of 8, result
        will be left-padded with zeros.
        )r   )r   to_bytesr,   r   rS   r'   r'   r(   r   =  s    zBitString.asOctetsc                 C   s   | j S )z5Get |ASN.1| value as a single integer value.
        rm   rS   r'   r'   r(   	asIntegerE  s    zBitString.asIntegerc                 C   s.   t | jdd }dt| jt|  | S )z4Get |ASN.1| value as a text string of bits.
        r   N0)r   binr,   r   )r$   Z	binStringr'   r'   r(   r   J  s    zBitString.asBinaryFNc              	   C   s   zt |dt|d }W n0 tk
rL   td| jt d f Y nX |dk	rt t |t|> |B t|t| }|s| |}|S )Create a |ASN.1| object initialized from the hex string.

        Parameters
        ----------
        value: :class:`str`
            Text string like 'DEADBEEF'
              z%s.fromHexString() error: %srt   N	r   r   r   ru   r   rw   r   rx   ry   clsr%   r   prependr'   r'   r(   r   P  s    	"zBitString.fromHexStringc              	   C   s   zt |p
ddt|}W n0 tk
rL   td| jt d f Y nX |dk	rt t |t|> |B t|t| }|s| |}|S )Create a |ASN.1| object initialized from a string of '0' and '1'.

        Parameters
        ----------
        value: :class:`str`
            Text string like '1010111'
        r   r   z%s.fromBinaryString() error: %srt   Nr   r   r'   r'   r(   r   i  s    	"zBitString.fromBinaryStringc                 C   sf   t t||? t|d | }|dk	rVt t |t|> |B t|t| }|sb| |}|S )zCreate a |ASN.1| object initialized from a string.

        Parameters
        ----------
        value: :class:`str` (Py2) or :class:`bytes` (Py3)
            Text string like '\\x01\\xff' (Py2) or b'\\x01\\xff' (Py3)
           N)r   r   
from_bytesr   r   )r   r%   r   r   paddingr'   r'   r(   fromOctetString  s    	$zBitString.fromOctetStringc                    s  t |tr|S t|r|s,tddS |d dkr|dd  dkr^ j|dd ddS |dd  dkr j|dd ddS td	|f n j	r4|
 s4d
d |dD }z fdd|D }W n& tk
 r   td|f Y nX t|}d}|D ]}|d|| > O }q
t||d S |drV j|dd  ddS |drx j|dd  ddS  j|ddS npt |ttfr jddd |D ddS t |trt|t|S t |trt|S td|f d S )Nr   'z'Brt   Tr   z'Hz Bad BIT STRING value notation %sc                 S   s   g | ]}|  qS r'   )stripr   r'   r'   r(   r     s     z&BitString.prettyIn.<locals>.<listcomp>,c                    s   g | ]} j | qS r'   r}   )r   namerS   r'   r(   r     s     zunknown bit name(s) in %r0xr   0b c                 S   s   g | ]}|rd pdqS )1r   r'   )r   br'   r'   r(   r     s     z#Bad BitString initializer type '%s')rF   r   r   isStringTyper   r   r   r   rw   r    isdigitsplitrv   r   
startswithr   listjoinr   r   intTypes)r$   r%   namesZbitPositionsZrightmostPositionnumberZbitPositionr'   rS   r(   rz     sL    
zBitString.prettyIn)FN)FN)FNr   )7r   r   r   r   r   r   r   r   r   r   r   r   r   r
   r   r    r   r!   r   r   r   r   r   r"   r   rp   rq   rn   ro   rr   rs   r   r   r   r   r6   r7   r:   r;   r4   r5   rT   rX   rx   r   rV   r   r   r   r   classmethodr   r   r   rz   r'   r'   r'   r(   r   n  sR   B
	c                 C   s   | D ]}|s dS qdS )NFTr'   )iterableelementr'   r'   r(   all  s    r   c                   @   s4  e Zd ZdZeeejejdZ	e
 Zej Ze ZZdZefddZejd dkrdd	 Zd
d Zdd Zdd Zdd Zn(dd	 Zdd Zdd Zdd Zdd Zdd Zd7ddZe dd Z!e 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*d0d1 Z+d2d3 Z,d4d5 Z-d6S )8r   a(  Create |ASN.1| schema or value object.

    |ASN.1| class is based on :class:`~pyasn1.type.base.SimpleAsn1Type`, its
    objects are immutable and duck-type Python 2 :class:`str` or
    Python 3 :class:`bytes`. When used in Unicode context, |ASN.1| type
    assumes "|encoding|" serialisation.

    Keyword Args
    ------------
    value: :class:`unicode`, :class:`str`, :class:`bytes` or |ASN.1| object
        class:`str` (Python 2) or :class:`bytes` (Python 3), alternatively
        class:`unicode` object (Python 2) or :class:`str` (Python 3)
        representing character string to be serialised into octets
        (note `encoding` parameter) or |ASN.1| object.
        If `value` is not given, schema object will be created.

    tagSet: :py:class:`~pyasn1.type.tag.TagSet`
        Object representing non-default ASN.1 tag(s)

    subtypeSpec: :py:class:`~pyasn1.type.constraint.ConstraintsIntersection`
        Object representing non-default ASN.1 subtype constraint(s). Constraints
        verification for |ASN.1| type occurs automatically on object
        instantiation.

    encoding: :py:class:`str`
        Unicode codec ID to encode/decode :class:`unicode` (Python 2) or
        :class:`str` (Python 3) the payload when |ASN.1| object is used
        in text string context.

    binValue: :py:class:`str`
        Binary string initializer to use instead of the *value*.
        Example: '10110011'.

    hexValue: :py:class:`str`
        Hexadecimal string initializer to use instead of the *value*.
        Example: 'DEADBEEF'.

    Raises
    ------
    ~pyasn1.error.ValueConstraintError, ~pyasn1.error.PyAsn1Error
        On constraint violation or bad initializer.

    Examples
    --------
    .. code-block:: python

        class Icon(OctetString):
            '''
            ASN.1 specification:

            Icon ::= OCTET STRING

            icon1 Icon ::= '001100010011001000110011'B
            icon2 Icon ::= '313233'H
            '''
        icon1 = Icon.fromBinaryString('001100010011001000110011')
        icon2 = Icon.fromHexString('313233')
    r   z
iso-8859-1c                 K   s   |r`|t kr`z| |d}W n tk
r4   Y nX z| |d}W n tk
r^   Y nX |t kr| jt k	r| | j}n| jt k	r| | j}d|kr| j|d< tj	j
| |f| d S )Nr   r   encoding)r   r   r   rv   r   r   r   r   r   r!   r"   r#   r'   r'   r(   r"   ,  s$    


zOctetString.__init__r   r   c              	   C   s   t |tr|S t |trdz|| jW S  ttfk
r`   t d }t	
d|| jf |Y qX nZt |ttfrzddd |D W S  tk
r   t	d| jj|f Y qX nt|S d S )Nrt   z&Can't encode string '%s' with codec %sr   c                 S   s   g | ]}t |qS r'   )chrr   r'   r'   r(   r   Y  s     z(OctetString.prettyIn.<locals>.<listcomp>zBad %s initializer '%s')rF   r{   unicodeencoder   LookupErrorUnicodeEncodeErrorrx   ry   r   PyAsn1UnicodeEncodeErrorr   r   r   ru   rw   r   r   r$   r%   excr'   r'   r(   rz   H  s*    

zOctetString.prettyInc                 C   s
   t | jS r)   r{   r,   rS   r'   r'   r(   r   c  s    zOctetString.__str__c                 C   sN   z| j | jW S  tk
rH   t d }td| j | jf |Y nX d S )Nrt   z&Can't decode string '%s' with codec %s)r,   decoder   UnicodeDecodeErrorrx   ry   r   PyAsn1UnicodeDecodeErrorr$   r   r'   r'   r(   __unicode__f  s    
zOctetString.__unicode__c                 C   s
   t | jS r)   r   rS   r'   r'   r(   r   q  s    zOctetString.asOctetsc                 C   s   t dd | jD S )Nc                 S   s   g | ]}t |qS r'   )ordr   r'   r'   r(   r   u  s     z)OctetString.asNumbers.<locals>.<listcomp>r   r,   rS   r'   r'   r(   r   t  s    zOctetString.asNumbersc                 C   s   t |tr|S t |tr`z|| jW S  tk
r\   t d }t	d|| jf |Y qX nPt |t
rr| S t |tjr| t|S t |ttfr| t|S t|S d S )Nrt   z(Can't encode string '%s' with '%s' codec)rF   bytesr{   r   r   r   rx   ry   r   r   r   r   r   r!   rz   r   r   r   r'   r'   r(   rz   x  s(    


c              	   C   sT   z| j | jW S  tk
rN   t d }td| j | j| jj	f |Y nX d S )Nrt   z0Can't decode string '%s' with '%s' codec at '%s')
r,   r   r   r   rx   ry   r   r   r   r   r   r'   r'   r(   r     s    c                 C   s
   t | jS r)   r   r,   rS   r'   r'   r(   	__bytes__  s    zOctetString.__bytes__c                 C   s
   t | jS r)   r   rS   r'   r'   r(   r     s    c                 C   s
   t | jS r)   r   rS   r'   r'   r(   r     s    c                 C   s   |S r)   r'   r-   r'   r'   r(   r|     s    zOctetString.prettyOutc                 C   sb   |  | j}|| jk	r|S |  }|D ]0}|dk s:|dkr&dddd |D    S q&t| S )N    ~   r   r   c                 s   s   | ]}d | V  qdS )z%.2xNr'   r   r'   r'   r(   	<genexpr>  s     z*OctetString.prettyPrint.<locals>.<genexpr>)r|   r,   r   r   r   r   )r$   scoper%   Znumbersr   r'   r'   r(   prettyPrint  s    
zOctetString.prettyPrintc                 C   sx   d}d}g }| D ]R}|r"|d8 }nd}| | d}|dkrFt|}ntd|f |||> O }q| | t|S )r   r   r   rt      )r   r   z&Non-binary OCTET STRING initializer %s)appendrR   r   rw   r   	ints2octs)r%   ZbitNobyterb   vr'   r'   r(   r     s"    	



zOctetString.fromBinaryStringc                 C   sV   g }g }| D ]&}|r.| t|| d d}q|}q|rL| t|d d t|S )r   r   Nr   )r  rR   r   r  )r%   rb   pr  r'   r'   r(   r     s    	zOctetString.fromHexStringc                 C   s
   t | jS r)   r   rS   r'   r'   r(   r     s    zOctetString.__len__c                 C   s(   |j tkr| | j| S | j| S d S r)   r   r   r+   r,   r$   r   r'   r'   r(   r     s    
zOctetString.__getitem__c                 C   s
   t | jS r)   iterr,   rS   r'   r'   r(   r     s    zOctetString.__iter__c                 C   s
   || j kS r)   rm   r-   r'   r'   r(   __contains__  s    zOctetString.__contains__c                 C   s   |  | j| | S r)   )r+   r,   rz   r-   r'   r'   r(   r6     s    zOctetString.__add__c                 C   s   |  | || j S r)   )r+   rz   r,   r-   r'   r'   r(   r7     s    zOctetString.__radd__c                 C   s   |  | j| S r)   r*   r-   r'   r'   r(   r:     s    zOctetString.__mul__c                 C   s   | | S r)   r'   r-   r'   r'   r(   r;     s    zOctetString.__rmul__c                 C   s
   t | jS r)   rQ   rS   r'   r'   r(   rT     s    zOctetString.__int__c                 C   s
   t | jS r)   rW   rS   r'   r'   r(   rX   "  s    zOctetString.__float__c                 C   s
   t | jS r)   )r   r,   rS   r'   r'   r(   r   %  s    zOctetString.__reversed__N)r   ).r   r   r   r   r   r   r   r   r   r   r   r   r   r   r!   r   r   r   r   r   r   r"   rx   r   rz   r   r   r   r   r   r|   r   staticmethodr   r   r   r   r   r
  r6   r7   r:   r;   rT   rX   r   r'   r'   r'   r(   r     sJ   =




c                   @   sN   e Zd ZdZeeejejdZ	e
jeed Ze
 Zdd ZdS )r   ai  Create |ASN.1| schema or value object.

    |ASN.1| class is based on :class:`~pyasn1.type.base.SimpleAsn1Type`, its
    objects are immutable and duck-type Python :class:`str` objects
    (always empty).

    Keyword Args
    ------------
    value: :class:`str` or |ASN.1| object
        Python empty :class:`str` literal or any object that evaluates to :obj:`False`
        If `value` is not given, schema object will be created.

    tagSet: :py:class:`~pyasn1.type.tag.TagSet`
        Object representing non-default ASN.1 tag(s)

    Raises
    ------
    ~pyasn1.error.ValueConstraintError, ~pyasn1.error.PyAsn1Error
        On constraint violation or bad initializer.

    Examples
    --------
    .. code-block:: python

        class Ack(Null):
            '''
            ASN.1 specification:

            Ack ::= NULL
            '''
        ack = Ack('')
    rj   r   c                 C   s   |r|S t dS )Nr   )r   str2octsr-   r'   r'   r(   rz   V  s    zNull.prettyInN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r   r   rz   r'   r'   r'   r(   r   )  s   $r   c                   @   s   e Zd ZdZeeejejdZ	e
 Zej 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 )r   a5  Create |ASN.1| schema or value object.

    |ASN.1| class is based on :class:`~pyasn1.type.base.SimpleAsn1Type`, its
    objects are immutable and duck-type Python :class:`tuple` objects
    (tuple of non-negative integers).

    Keyword Args
    ------------
    value: :class:`tuple`, :class:`str` or |ASN.1| object
        Python sequence of :class:`int` or :class:`str` literal or |ASN.1| object.
        If `value` is not given, schema object will be created.

    tagSet: :py:class:`~pyasn1.type.tag.TagSet`
        Object representing non-default ASN.1 tag(s)

    subtypeSpec: :py:class:`~pyasn1.type.constraint.ConstraintsIntersection`
        Object representing non-default ASN.1 subtype constraint(s). Constraints
        verification for |ASN.1| type occurs automatically on object
        instantiation.

    Raises
    ------
    ~pyasn1.error.ValueConstraintError, ~pyasn1.error.PyAsn1Error
        On constraint violation or bad initializer.

    Examples
    --------
    .. code-block:: python

        class ID(ObjectIdentifier):
            '''
            ASN.1 specification:

            ID ::= OBJECT IDENTIFIER

            id-edims ID ::= { joint-iso-itu-t mhs-motif(6) edims(7) }
            id-bp ID ::= { id-edims 11 }
            '''
        id_edims = ID('2.6.7')
        id_bp = id_edims + (11,)
       c                 C   s   |  | j| S r)   r*   r   r'   r'   r(   r6     s    zObjectIdentifier.__add__c                 C   s   |  || j S r)   r*   r   r'   r'   r(   r7     s    zObjectIdentifier.__radd__c                 C   s   | j S r)   rm   rS   r'   r'   r(   asTuple  s    zObjectIdentifier.asTuplec                 C   s
   t | jS r)   r   rS   r'   r'   r(   r     s    zObjectIdentifier.__len__c                 C   s(   |j tkr| | j| S | j| S d S r)   r  r  r'   r'   r(   r     s    
zObjectIdentifier.__getitem__c                 C   s
   t | jS r)   r  rS   r'   r'   r(   r     s    zObjectIdentifier.__iter__c                 C   s
   || j kS r)   rm   r-   r'   r'   r(   r
    s    zObjectIdentifier.__contains__c                 C   s   | j |S r)   )r,   index)r$   Zsuboidr'   r'   r(   r    s    zObjectIdentifier.indexc                 C   s6   t | }|t |kr2| jd| |d| kr2dS dS )ar  Indicate if this |ASN.1| object is a prefix of other |ASN.1| object.

        Parameters
        ----------
        other: |ASN.1| object
            |ASN.1| object

        Returns
        -------
        : :class:`bool`
            :obj:`True` if this |ASN.1| object is a parent (e.g. prefix) of the other |ASN.1| object
            or :obj:`False` otherwise.
        NTFr   )r$   r   lr'   r'   r(   
isPrefixOf  s
    zObjectIdentifier.isPrefixOfc              
   C   s  t |trt|S t|rd|krDtd|| jjt	
 d f ztdd |dD W S  tk
r   td|| jjt	
 d f Y nX ztdd |D }W n8 ttfk
r   td|| jjt	
 d f Y nX t|t|kr|S td|| jjf d S )	N-z Malformed Object ID %s at %s: %srt   c                 S   s   g | ]}|rt |qS r'   rR   r   ZsubOidr'   r'   r(   r     s      z-ObjectIdentifier.prettyIn.<locals>.<listcomp>.c                 S   s   g | ]}|d krt |qS )r   r  r  r'   r'   r(   r     s      zMalformed Object ID %s at %s)rF   r   r   r   r   r   rw   r   r   rx   ry   r   ru   	TypeErrorr   )r$   r%   ZtupleOfIntsr'   r'   r(   rz     s,    



zObjectIdentifier.prettyInc                 C   s   d dd |D S )Nr  c                 S   s   g | ]}t |qS r'   )r{   r   r'   r'   r(   r     s     z.ObjectIdentifier.prettyOut.<locals>.<listcomp>)r   r-   r'   r'   r(   r|     s    zObjectIdentifier.prettyOutN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r!   r   r   r6   r7   r  r   r   r   r
  r  r  rz   r|   r'   r'   r'   r(   r   d  s"   ,
c                   @   sB  e Zd ZdZdZzedZedZeefZW n  e	k
rL   d ZZdZY nX e
e
e
je
jdZe Zej Zedd Zd	d
 ZdbddZedd Zedd Ze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&dcd$d%Z'd&d' Z(e)j*d d(kr.d)d* Z+d+d, Z,n d-d. Z-d/d0 Z.d1d2 Z/d3d4 Z0d5d6 Z1e)j*d d(krnd7d8 Z2d9d: Z3d;d< Z4d=d> Z5d?d@ Z6dddAdBZ7dCdD Z8dEdF Z9e)j*dd( dGkrdHdI Z:dJdK Z;dLdM Z<dNdO Z=dPdQ Z>dRdS Z?dTdU Z@e)j*d d(krdVdW ZAndXdY ZBejjCZCdZd[ ZDd\d] ZEd^d_ ZFd`da ZGdS )er   ab  Create |ASN.1| schema or value object.

    |ASN.1| class is based on :class:`~pyasn1.type.base.SimpleAsn1Type`, its
    objects are immutable and duck-type Python :class:`float` objects.
    Additionally, |ASN.1| objects behave like a :class:`tuple` in which case its
    elements are mantissa, base and exponent.

    Keyword Args
    ------------
    value: :class:`tuple`, :class:`float` or |ASN.1| object
        Python sequence of :class:`int` (representing mantissa, base and
        exponent) or :class:`float` instance or |ASN.1| object.
        If `value` is not given, schema object will be created.

    tagSet: :py:class:`~pyasn1.type.tag.TagSet`
        Object representing non-default ASN.1 tag(s)

    subtypeSpec: :py:class:`~pyasn1.type.constraint.ConstraintsIntersection`
        Object representing non-default ASN.1 subtype constraint(s). Constraints
        verification for |ASN.1| type occurs automatically on object
        instantiation.

    Raises
    ------
    ~pyasn1.error.ValueConstraintError, ~pyasn1.error.PyAsn1Error
        On constraint violation or bad initializer.

    Examples
    --------
    .. code-block:: python

        class Pi(Real):
            '''
            ASN.1 specification:

            Pi ::= REAL

            pi Pi ::= { mantissa 314159, base 10, exponent -5 }

            '''
        pi = Pi((314159, 10, -5))
    Ninfz-infr'   	   c                 C   s6   | \}}}|r,|d dkr,|d }|d7 }q
|||fS )N
   r   rt   r'   )r%   mr   er'   r'   r(   Z__normalizeBase104  s
    

zReal.__normalizeBase10c                 C   s  t |trt|dkrt |d tr@t |d tr@t |d tsPtd|f t |d trz| jrz|d | jkrz|d S |d dkrtd|d f |d dkr| 	|}|S t |tr| 	|ddfS t |tst
|rxt
|r$zt|}W n& tk
r"   td	|f Y nX | jr<|| jkr<|S d}t||krb|d9 }|d8 }q@| 	t|d|fS nt |trt|S td	|f d S )
Nr   r   rt   r   zLame Real value syntax: %s)r   r  z"Prohibited base for Real value: %sr  zBad real value syntax: %s)rF   r   r   numericTypesr   r   rw   rG   _inf_Real__normalizeBase10r   r   ru   rR   r   )r$   r%   r  r'   r'   r(   rz   <  sT    


zReal.prettyInr   c                 C   s,   z|  t| W S  tk
r&   Y dS X d S )Nz
<overflow>)r|   rG   OverflowError)r$   r   r'   r'   r(   r   d  s    zReal.prettyPrintc                 C   s   | j | jkS )zIndicate PLUS-INFINITY object value

        Returns
        -------
        : :class:`bool`
            :obj:`True` if calling object represents plus infinity
            or :obj:`False` otherwise.

        )r,   _plusInfrS   r'   r'   r(   	isPlusInfk  s    zReal.isPlusInfc                 C   s   | j | jkS )zIndicate MINUS-INFINITY object value

        Returns
        -------
        : :class:`bool`
            :obj:`True` if calling object represents minus infinity
            or :obj:`False` otherwise.
        )r,   	_minusInfrS   r'   r'   r(   
isMinusInfx  s    
zReal.isMinusInfc                 C   s   | j | jkS r)   )r,   r  rS   r'   r'   r(   isInf  s    z
Real.isInfc                 C   s   |  t| | S r)   r+   rG   r-   r'   r'   r(   r6     s    zReal.__add__c                 C   s   | | S r)   r'   r-   r'   r'   r(   r7     s    zReal.__radd__c                 C   s   |  t| | S r)   r%  r-   r'   r'   r(   r:     s    zReal.__mul__c                 C   s   | | S r)   r'   r-   r'   r'   r(   r;     s    zReal.__rmul__c                 C   s   |  t| | S r)   r%  r-   r'   r'   r(   r8     s    zReal.__sub__c                 C   s   |  |t|  S r)   r%  r-   r'   r'   r(   r9     s    zReal.__rsub__c                 C   s   |  t| | S r)   r%  r-   r'   r'   r(   r<     s    zReal.__mod__c                 C   s   |  |t|  S r)   r%  r-   r'   r'   r(   r=     s    zReal.__rmod__c                 C   s   |  tt| ||S r)   r+   r?   rG   r@   r'   r'   r(   rA     s    zReal.__pow__c                 C   s   |  t|t| S r)   r&  r-   r'   r'   r(   rB     s    zReal.__rpow__r   c                 C   s   |  t| | S r)   r%  r-   r'   r'   r(   rH     s    zReal.__div__c                 C   s   |  |t|  S r)   r%  r-   r'   r'   r(   rI     s    zReal.__rdiv__c                 C   s   |  t| | S r)   r%  r-   r'   r'   r(   rK     s    zReal.__truediv__c                 C   s   |  |t|  S r)   r%  r-   r'   r'   r(   rL     s    zReal.__rtruediv__c                 C   s   |  t| | S r)   r%  r-   r'   r'   r(   rO     s    zReal.__divmod__c                 C   s   |  |t|  S r)   r%  r-   r'   r'   r(   rP     s    zReal.__rdivmod__c                 C   s   t t| S r)   )rR   rG   rS   r'   r'   r(   rT     s    zReal.__int__c                 C   s   t t| S r)   )rU   rG   rS   r'   r'   r(   rV     s    zReal.__long__c                 C   s:   | j | jkr| j S t| j d t| j d | j d  S d S )Nr   rt   r   )r,   r  rG   r?   rS   r'   r'   r(   rX     s
    zReal.__float__c                 C   s   |  tt| S r)   )r+   rY   rG   rS   r'   r'   r(   rZ     s    zReal.__abs__c                 C   s   |  t| 
 S r)   r%  rS   r'   r'   r(   r\     s    zReal.__pos__c                 C   s   |  t|  S r)   r%  rS   r'   r'   r(   r]     s    zReal.__neg__c                 C   s$   t t| |}|r| |S |S d S r)   )r_   rG   r+   r`   r'   r'   r(   rc     s    
zReal.__round__c                 C   s   |  tt| S r)   )r+   rd   re   rG   rS   r'   r'   r(   rf     s    zReal.__floor__c                 C   s   |  tt| S r)   )r+   rd   rg   rG   rS   r'   r'   r(   rh     s    zReal.__ceil__ri   c                 C   s   |  tt| S r)   )r+   rd   rk   rG   rS   r'   r'   r(   rl     s    zReal.__trunc__c                 C   s   t | |k S r)   rG   r-   r'   r'   r(   rn     s    zReal.__lt__c                 C   s   t | |kS r)   r'  r-   r'   r'   r(   ro     s    zReal.__le__c                 C   s   t | |kS r)   r'  r-   r'   r'   r(   rp     s    zReal.__eq__c                 C   s   t | |kS r)   r'  r-   r'   r'   r(   rq     s    zReal.__ne__c                 C   s   t | |kS r)   r'  r-   r'   r'   r(   rr     s    zReal.__gt__c                 C   s   t | |kS r)   r'  r-   r'   r'   r(   rs     s    zReal.__ge__c                 C   s   t t| S r)   boolrG   rS   r'   r'   r(   __nonzero__  s    zReal.__nonzero__c                 C   s   t t| S r)   r(  rS   r'   r'   r(   __bool__  s    zReal.__bool__c                 C   s&   | j | jkrtdn
| j | S d S )Nz Invalid infinite value operation)r,   r  r   rw   r$   idxr'   r'   r(   r     s    zReal.__getitem__c                 C   s   | j S r)   )r!  rS   r'   r'   r(   isPlusInfinity  s    zReal.isPlusInfinityc                 C   s   | j S r)   )r#  rS   r'   r'   r(   isMinusInfinity  s    zReal.isMinusInfinityc                 C   s   | j S r)   )r$  rS   r'   r'   r(   
isInfinity  s    zReal.isInfinity)r   )N)r   )Hr   r   r   r   Z
binEncBaserG   r   r"  r  ru   r   r   r   r   r   r   r   r   r   r   r!   r   r   r  r  rz   r   propertyr!  r#  r$  r6   r7   r:   r;   r8   r9   r<   r=   rA   rB   rx   r   rH   rI   rK   rL   rO   rP   rT   rV   rX   rZ   r\   r]   rc   rf   rh   rl   rn   ro   rp   rq   rr   rs   r*  r+  r   r   r.  r/  r0  r'   r'   r'   r(   r     s   *


(







c                   @   s@   e Zd ZdZeeejejdZ	e
 Ze Ze ZdS )r   a  Create |ASN.1| schema or value object.

    |ASN.1| class is based on :class:`~pyasn1.type.base.SimpleAsn1Type`, its
    objects are immutable and duck-type Python :class:`int` objects.

    Keyword Args
    ------------
    value: :class:`int`, :class:`str` or |ASN.1| object
        Python :class:`int` or :class:`str` literal or |ASN.1| object.
        If `value` is not given, schema object will be created.

    tagSet: :py:class:`~pyasn1.type.tag.TagSet`
        Object representing non-default ASN.1 tag(s)

    subtypeSpec: :py:class:`~pyasn1.type.constraint.ConstraintsIntersection`
        Object representing non-default ASN.1 subtype constraint(s). Constraints
        verification for |ASN.1| type occurs automatically on object
        instantiation.

    namedValues: :py:class:`~pyasn1.type.namedval.NamedValues`
        Object representing non-default symbolic aliases for numbers

    Raises
    ------
    ~pyasn1.error.ValueConstraintError, ~pyasn1.error.PyAsn1Error
        On constraint violation or bad initializer.

    Examples
    --------

    .. code-block:: python

        class RadioButton(Enumerated):
            '''
            ASN.1 specification:

            RadioButton ::= ENUMERATED { button1(0), button2(1),
                                         button3(2) }

            selected-by-default RadioButton ::= button1
            '''
            namedValues = NamedValues(
                ('button1', 0), ('button2', 1),
                ('button3', 2)
            )

        selected_by_default = RadioButton('button1')
    r  N)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r
   r   r    r'   r'   r'   r(   r     s   3c                   @   s   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	d2ddZ
dd Zd3ddZdd Zdd Zdd ZedfddZedddfd d!Zed"d# Zed$d% Zd&d' Zd(d) Zd4d*d+Zd5d,d-Zed.d/ Zed0d1 ZdS )6r   a<  Create |ASN.1| schema or value object.

    |ASN.1| class is based on :class:`~pyasn1.type.base.ConstructedAsn1Type`,
    its objects are mutable and duck-type Python :class:`list` objects.

    Keyword Args
    ------------
    componentType : :py:class:`~pyasn1.type.base.PyAsn1Item` derivative
        A pyasn1 object representing ASN.1 type allowed within |ASN.1| type

    tagSet: :py:class:`~pyasn1.type.tag.TagSet`
        Object representing non-default ASN.1 tag(s)

    subtypeSpec: :py:class:`~pyasn1.type.constraint.ConstraintsIntersection`
        Object representing non-default ASN.1 subtype constraint(s). Constraints
        verification for |ASN.1| type can only occur on explicit
        `.isInconsistent` call.

    Examples
    --------

    .. code-block:: python

        class LotteryDraw(SequenceOf):  #  SetOf is similar
            '''
            ASN.1 specification:

            LotteryDraw ::= SEQUENCE OF INTEGER
            '''
            componentType = Integer()

        lotteryDraw = LotteryDraw()
        lotteryDraw.extend([123, 456, 789])
    c                 O   sL   |r2t d|D ]"\}}||kr(td||d< qt| _tjj| f| d S )N)componentTyper   r   z*Conflicting positional and keyword params!r2  )zipr   rw   r   _componentValuesr   ConstructedAsn1Typer"   )r$   argsr&   keyr%   r'   r'   r(   r"   y  s    

zSequenceOfAndSetOfBase.__init__c                 C   s8   z|  |W S  tjk
r2   tt d Y nX d S r   )getComponentByPositionr   rw   r   rx   ry   r,  r'   r'   r(   r     s    z"SequenceOfAndSetOfBase.__getitem__c                 C   s<   z|  || W n& tjk
r6   tt d Y nX d S r   )setComponentByPositionr   rw   r   rx   ry   r$   r-  r%   r'   r'   r(   __setitem__  s    z"SequenceOfAndSetOfBase.__setitem__c                 C   s&   | j tkrd}n
t| j }|| |< d S r   )r4  r   r   )r$   r%   posr'   r'   r(   r    s    

zSequenceOfAndSetOfBase.appendc                 C   s   t | j |S r)   )r   r4  valuesr   r-   r'   r'   r(   r     s    zSequenceOfAndSetOfBase.countc                 C   s(   |D ]}|  | q| jtkr$i | _d S r)   )r  r4  r   )r$   r=  r%   r'   r'   r(   extend  s    
zSequenceOfAndSetOfBase.extendr   Nc                 C   sj   |d krt | }t| j  \}}t|}z||||| W S  tjk
rd   tt	
 d Y nX d S r   )r   r3  r4  itemsr   r  r   rw   ru   rx   ry   )r$   r%   startstopr   r=  r'   r'   r(   r    s    zSequenceOfAndSetOfBase.indexc                 C   s   | j   d S r)   )r4  reverserS   r'   r'   r(   rB    s    zSequenceOfAndSetOfBase.reverseFc                 C   s"   t tt| j ||d| _d S )N)r7  rB  )dict	enumeratesortedr4  r=  )r$   r7  rB  r'   r'   r(   sort  s     zSequenceOfAndSetOfBase.sortc                 C   s"   | j tks| j sdS t| j d S )Nr   rt   )r4  r   r   rS   r'   r'   r(   r     s    zSequenceOfAndSetOfBase.__len__c                 c   s$   t dt| D ]}| |V  qd S r   )r   r   r8  r,  r'   r'   r(   r     s    zSequenceOfAndSetOfBase.__iter__c                 C   sR   | j  D ]B\}}|tk	r
t|tjr<|||j|d q
|||  q
d S NcloneValueFlag)r4  r?  r   rF   r   r5  r9  r+   r$   myClonerI  r-  componentValuer'   r'   r(   _cloneComponentValues  s     
z,SequenceOfAndSetOfBase._cloneComponentValuesTc              	      s   t |tr4ttt} fdd|| D S |dk rZt| }|dk rZtdzj| }W n: ttjfk
r   s  Y S 	| j| }Y nX  t
ks|jr|S  S dS )a2  Return |ASN.1| type component value by position.

        Equivalent to Python sequence subscription operation (e.g. `[]`).

        Parameters
        ----------
        idx : :class:`int`
            Component index (zero-based). Must either refer to an existing
            component or to N+1 component (if *componentType* is set). In the latter
            case a new component type gets instantiated and appended to the |ASN.1|
            sequence.

        Keyword Args
        ------------
        default: :class:`object`
            If set and requested component is a schema object, return the `default`
            object instead of the requested component.

        instantiate: :class:`bool`
            If :obj:`True` (default), inner component will be automatically instantiated.
            If :obj:`False` either existing component or the :class:`NoValue` object will be
            returned.

        Returns
        -------
        : :py:class:`~pyasn1.type.base.PyAsn1Item`
            Instantiate |ASN.1| component type or return existing component value

        Examples
        --------

        .. code-block:: python

            # can also be SetOf
            class MySequenceOf(SequenceOf):
                componentType = OctetString()

            s = MySequenceOf()

            # returns component #0 with `.isValue` property False
            s.getComponentByPosition(0)

            # returns None
            s.getComponentByPosition(0, default=None)

            s.clear()

            # returns noValue
            s.getComponentByPosition(0, instantiate=False)

            # sets component #0 to OctetString() ASN.1 schema
            # object and returns it
            s.getComponentByPosition(0, instantiate=True)

            # sets component #0 to ASN.1 value object
            s.setComponentByPosition(0, 'ABCD')

            # returns OctetString('ABCD') value object
            s.getComponentByPosition(0, instantiate=False)

            s.clear()

            # returns noValue
            s.getComponentByPosition(0, instantiate=False)
        c                    s   g | ]} | qS r'   )r8  )r   Zsubidxdefaultinstantiater$   r'   r(   r     s   zASequenceOfAndSetOfBase.getComponentByPosition.<locals>.<listcomp>r   &SequenceOf/SetOf index is out of rangeN)rF   r   r   r   r   r   rw   r4  rv   r9  r   isValue)r$   r-  rO  rP  r   rL  r'   rN  r(   r8    s*    B

z-SequenceOfAndSetOfBase.getComponentByPositionc                 C   s  t |trZttt| }|r*|| d p,d}t|D ]\}}	| || |	||| q6| S |dk rt| | }|dk rtd| j	}
| j
tkri }n| j
}||t}|tkr|
dk	r|
 }n|tkrtdnt |tjsD|
dk	r
t |
tjr
|
j|d}n8|tk	r0t |tjr0|j|d}ntd|| f nf|
dk	r|sZ|r| jrj|
jpn|
j}|||o|||o|s|
jtjkrtd||
f |||< || _
| S )a  Assign |ASN.1| type component by position.

        Equivalent to Python sequence item assignment operation (e.g. `[]`)
        or list.append() (when idx == len(self)).

        Parameters
        ----------
        idx: :class:`int`
            Component index (zero-based). Must either refer to existing
            component or to N+1 component. In the latter case a new component
            type gets instantiated (if *componentType* is set, or given ASN.1
            object is taken otherwise) and appended to the |ASN.1| sequence.

        Keyword Args
        ------------
        value: :class:`object` or :py:class:`~pyasn1.type.base.PyAsn1Item` derivative
            A Python value to initialize |ASN.1| component with (if *componentType* is set)
            or ASN.1 value object to assign to |ASN.1| component.
            If `value` is not given, schema object will be set as a component.

        verifyConstraints: :class:`bool`
             If :obj:`False`, skip constraints validation

        matchTags: :class:`bool`
             If :obj:`False`, skip component tags matching

        matchConstraints: :class:`bool`
             If :obj:`False`, skip component constraints matching

        Returns
        -------
        self

        Raises
        ------
        ~pyasn1.error.ValueConstraintError, ~pyasn1.error.PyAsn1Error
            On constraint violation or bad initializer
        IndexError
            When idx > len(self)
        r   rQ  NComponent type not definedr%   z5Non-ASN.1 value %r and undefined component type at %r-Component value is tag-incompatible: %r vs %r)rF   r   r   r   r   rD  r9  r   rw   r2  r4  r   getr+   r   Asn1Itemr!   strictConstraintsisSameTypeWithisSuperTypeOfr   r   )r$   r-  r%   verifyConstraints	matchTagsmatchConstraintsr   ZstartIdxZsubIdxZsubValuer2  componentValuescurrentValuesubtypeCheckerr'   r'   r(   r9  1  sx    ,
   





z-SequenceOfAndSetOfBase.setComponentByPositionc                 C   s   | j d k	r| j jS d S r)   )r2  tagMaprS   r'   r'   r(   componentTagMap  s    
z&SequenceOfAndSetOfBase.componentTagMapc                    s    fddt  jD S )Nc                    s   g | ]} j | qS r'   r4  r   r-  rS   r'   r(   r     s   z5SequenceOfAndSetOfBase.components.<locals>.<listcomp>)rE  r4  rS   r'   rS   r(   
components  s    
z!SequenceOfAndSetOfBase.componentsc                 C   s
   i | _ | S )zRemove all components and become an empty |ASN.1| value object.

        Has the same effect on |ASN.1| object as it does on :class:`list`
        built-in.
        rc  rS   r'   r'   r(   clear  s    zSequenceOfAndSetOfBase.clearc                 C   s
   t | _| S zRemove all components and become a |ASN.1| schema object.

        See :meth:`isValue` property for more information on the
        distinction between value and schema objects.
        )r   r4  rS   r'   r'   r(   reset  s    zSequenceOfAndSetOfBase.resetc                 C   sj   |d7 }| j jd }| js|S t| D ]>\}}|d| 7 }|tkrV| jd k	rV|d7 }q&|||7 }q&|S )Nrt   :
 z<empty>)r   r   rR  rD  r   r2  r   r$   r   representationr-  rL  r'   r'   r(   r     s    
z"SequenceOfAndSetOfBase.prettyPrintc                 C   sX   |d7 }d| j | jjf }| jd k	r@|d| 7 }|| j|7 }|d d|d   d S )Nrt   %s -> %s {
rj  
})r   r   r   r2  prettyPrintType)r$   r   rl  r'   r'   r(   rp    s    
z&SequenceOfAndSetOfBase.prettyPrintTypec                 C   sL   | j tkrdS t| j t| kr$dS | j  D ]}|tks@|js. dS q.dS )ar  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).
        FT)r4  r   r   r=  rR  r$   rL  r'   r'   r(   rR    s    
zSequenceOfAndSetOfBase.isValuec                 C   s   | j tks| jsdS | jtkr"dS i }| j D ]\}}|tkrBq0|||< q0z| | W n& tjk
r   t d }| Y S X dS a  Run necessary checks to ensure |ASN.1| object consistency.

        Default action is to verify |ASN.1| object against constraints imposed
        by `subtypeSpec`.

        Raises
        ------
        :py:class:`~pyasn1.error.PyAsn1tError` on any inconsistencies found
        FTrt   )	r2  r   r   r4  r?  r   rw   rx   ry   )r$   mappingr-  r%   r   r'   r'   r(   isInconsistent  s    


z%SequenceOfAndSetOfBase.isInconsistent)r   N)NF)r   )r   )r   r   r   r   r"   r   r;  r  r   r>  r  rB  rF  r   r   rM  r   r8  r9  r1  rb  re  rf  rh  r   rp  rR  rt  r'   r'   r'   r(   r   V  s>   "	


]
o

		

	
&c                   @   s>   e Zd ZejZeeejej	dZ
dZe Ze ZdS )r   r   Nr   r   r   r   r   r   r   r   r   tagFormatConstructedr   r2  r   r   r   r   r   r'   r'   r'   r(   r   !  s   c                   @   s>   e Zd ZejZeeejej	dZ
dZe Ze ZdS )r      Nru  r'   r'   r'   r(   r   8  s   c                   @   s   e Zd ZdZe ZG dd d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dd Zdd Zdd Zedd Zdd Zed fd!d"Zed d d fd#d$Zed fd%d&Zed d d fd'd(Zed)d* Zed+d, Zd9d.d/Zd:d0d1Z d2d3 Z!d4d5 Z"d6d7 Z#d8S );r   a  Create |ASN.1| schema or value object.

    |ASN.1| class is based on :class:`~pyasn1.type.base.ConstructedAsn1Type`,
    its objects are mutable and duck-type Python :class:`dict` objects.

    Keyword Args
    ------------
    componentType: :py:class:`~pyasn1.type.namedtype.NamedType`
        Object holding named ASN.1 types allowed within this collection

    tagSet: :py:class:`~pyasn1.type.tag.TagSet`
        Object representing non-default ASN.1 tag(s)

    subtypeSpec: :py:class:`~pyasn1.type.constraint.ConstraintsIntersection`
        Object representing non-default ASN.1 subtype constraint(s).  Constraints
        verification for |ASN.1| type can only occur on explicit
        `.isInconsistent` call.

    Examples
    --------

    .. code-block:: python

        class Description(Sequence):  #  Set is similar
            '''
            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                   @   sP   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S )zSequenceAndSetBase.DynamicNamesz9Fields names/positions mapping for component-less objectsc                 C   s   i | _ i | _d S r)   _keyToIdxMap_idxToKeyMaprS   r'   r'   r(   r"     s    z(SequenceAndSetBase.DynamicNames.__init__c                 C   s
   t | jS r)   )r   ry  rS   r'   r'   r(   r     s    z'SequenceAndSetBase.DynamicNames.__len__c                 C   s   || j kp|| jkS r)   rx  r$   itemr'   r'   r(   r
    s    z,SequenceAndSetBase.DynamicNames.__contains__c                    s    fddt t jD S )Nc                 3   s   | ]} j | V  qd S r)   )rz  rd  rS   r'   r(   r     s     z;SequenceAndSetBase.DynamicNames.__iter__.<locals>.<genexpr>)r   r   rz  rS   r'   rS   r(   r     s    z(SequenceAndSetBase.DynamicNames.__iter__c                 C   s0   z| j | W S  tk
r*   | j|  Y S X d S r)   )ry  rv   rz  r{  r'   r'   r(   r     s    z+SequenceAndSetBase.DynamicNames.__getitem__c                 C   s0   z| j | W S  tk
r*   tdY nX d S )NzType position out of range)rz  rv   r   rw   r,  r'   r'   r(   getNameByPosition  s    z1SequenceAndSetBase.DynamicNames.getNameByPositionc                 C   s6   z| j | W S  tk
r0   td|f Y nX d S )NName %s not found)ry  rv   r   rw   )r$   r   r'   r'   r(   getPositionByName  s    z1SequenceAndSetBase.DynamicNames.getPositionByNamec                 C   s    || j d| < d| | j|< d S )Nzfield-%drx  r,  r'   r'   r(   addField  s    z(SequenceAndSetBase.DynamicNames.addFieldN)r   r   r   r   r"   r   r
  r   r   r}  r  r  r'   r'   r'   r(   DynamicNames  s   r  c                 K   sD   t jj| f| t| j| _| jr*g | _nt| _| jp<|  | _	d S r)   )
r   r5  r"   r   r2  _componentTypeLenr4  r   r  _dynamicNames)r$   r&   r'   r'   r(   r"     s    zSequenceAndSetBase.__init__c                 C   sx   t |r@z| |W S  tjk
r<   tt d Y qtX n4z| |W S  tjk
rr   t	t d Y nX d S r   )
r   r   getComponentByNamer   rw   rv   rx   ry   r8  r   r,  r'   r'   r(   r     s    
zSequenceAndSetBase.__getitem__c                 C   s   t |rDz| || W q| tjk
r@   tt d Y q|X n8z| || W n& tjk
rz   t	t d Y nX d S r   )
r   r   setComponentByNamer   rw   rv   rx   ry   r9  r   r:  r'   r'   r(   r;    s    
zSequenceAndSetBase.__setitem__c                 C   s   | j r|| jkS || jkS d S r)   )r  r2  r  r$   r7  r'   r'   r(   r
    s    
zSequenceAndSetBase.__contains__c                 C   s
   t | jS r)   )r   r4  rS   r'   r'   r(   r     s    zSequenceAndSetBase.__len__c                 C   s   t | jp| jS r)   )r	  r2  r  rS   r'   r'   r(   r     s    zSequenceAndSetBase.__iter__c                 c   s(   t | jpt| jD ]}| | V  qd S r)   )r   r  r   r  r,  r'   r'   r(   r=    s    zSequenceAndSetBase.valuesc                 C   s   t | S r)   )r	  rS   r'   r'   r(   keys  s    zSequenceAndSetBase.keysc                 c   sP   t | jpt| jD ]6}| jr6| j| j| | fV  q| j| | | fV  qd S r)   )r   r  r   r  r2  r   r,  r'   r'   r(   r?    s    zSequenceAndSetBase.itemsc                 O   s0   |D ]\}}|| |< q|D ]}|| | |< qd S r)   r'   )r$   Z	iterValueZmappingValuekr  r'   r'   r(   update  s    
zSequenceAndSetBase.updatec                 C   s   g | _ |  | _| S )zRemove all components and become an empty |ASN.1| value object.

        Has the same effect on |ASN.1| object as it does on :class:`dict`
        built-in.
        )r4  r  r  rS   r'   r'   r(   rf    s    
zSequenceAndSetBase.clearc                 C   s   t | _|  | _| S rg  )r   r4  r  r  rS   r'   r'   r(   rh   	  s    
zSequenceAndSetBase.resetc                 C   s   | j S r)   rc  rS   r'   r'   r(   re  
	  s    zSequenceAndSetBase.componentsc                 C   s`   | j tkrd S t| j D ]B\}}|tk	rt|tjrJ|||j|d q|||  qd S rG  )r4  r   rD  rF   r   r5  r9  r+   rJ  r'   r'   r(   rM  	  s    
 
z(SequenceAndSetBase._cloneComponentValuesTc                 C   sZ   | j r| j|}n6z| j|}W n$ tk
rH   td|f Y nX | j|||dS )ap  Returns |ASN.1| type component by name.

        Equivalent to Python :class:`dict` subscription operation (e.g. `[]`).

        Parameters
        ----------
        name: :class:`str`
            |ASN.1| type component name

        Keyword Args
        ------------
        default: :class:`object`
            If set and requested component is a schema object, return the `default`
            object instead of the requested component.

        instantiate: :class:`bool`
            If :obj:`True` (default), inner component will be automatically
            instantiated.
            If :obj:`False` either existing component or the :class:`NoValue`
            object will be returned.

        Returns
        -------
        : :py:class:`~pyasn1.type.base.PyAsn1Item`
            Instantiate |ASN.1| component type or return existing
            component value
        r~  rO  rP  )r  r2  r  r  rv   r   rw   r8  )r$   r   rO  rP  r-  r'   r'   r(   r  	  s    z%SequenceAndSetBase.getComponentByNamec                 C   s\   | j r| j|}n6z| j|}W n$ tk
rH   td|f Y nX | |||||S )a  Assign |ASN.1| type component by name.

        Equivalent to Python :class:`dict` item assignment operation (e.g. `[]`).

        Parameters
        ----------
        name: :class:`str`
            |ASN.1| type component name

        Keyword Args
        ------------
        value: :class:`object` or :py:class:`~pyasn1.type.base.PyAsn1Item` derivative
            A Python value to initialize |ASN.1| component with (if *componentType* is set)
            or ASN.1 value object to assign to |ASN.1| component.
            If `value` is not given, schema object will be set as a component.

        verifyConstraints: :class:`bool`
             If :obj:`False`, skip constraints validation

        matchTags: :class:`bool`
             If :obj:`False`, skip component tags matching

        matchConstraints: :class:`bool`
             If :obj:`False`, skip component constraints matching

        Returns
        -------
        self
        r~  )r  r2  r  r  rv   r   rw   r9  )r$   r   r%   r[  r\  r]  r-  r'   r'   r(   r  B	  s    !    z%SequenceAndSetBase.setComponentByNamec                 C   s   z| j tkrt}n
| j | }W n tk
r6   t}Y nX |sR|tksJ|jsN|S |S |tkrd| | | j | }|tks||jr|S |S dS )a  Returns |ASN.1| type component by index.

        Equivalent to Python sequence subscription operation (e.g. `[]`).

        Parameters
        ----------
        idx: :class:`int`
            Component index (zero-based). Must either refer to an existing
            component or (if *componentType* is set) new ASN.1 schema object gets
            instantiated.

        Keyword Args
        ------------
        default: :class:`object`
            If set and requested component is a schema object, return the `default`
            object instead of the requested component.

        instantiate: :class:`bool`
            If :obj:`True` (default), inner component will be automatically
            instantiated.
            If :obj:`False` either existing component or the :class:`NoValue`
            object will be returned.

        Returns
        -------
        : :py:class:`~pyasn1.type.base.PyAsn1Item`
            a PyASN1 object

        Examples
        --------

        .. code-block:: python

            # can also be Set
            class MySequence(Sequence):
                componentType = NamedTypes(
                    NamedType('id', OctetString())
                )

            s = MySequence()

            # returns component #0 with `.isValue` property False
            s.getComponentByPosition(0)

            # returns None
            s.getComponentByPosition(0, default=None)

            s.clear()

            # returns noValue
            s.getComponentByPosition(0, instantiate=False)

            # sets component #0 to OctetString() ASN.1 schema
            # object and returns it
            s.getComponentByPosition(0, instantiate=True)

            # sets component #0 to ASN.1 value object
            s.setComponentByPosition(0, 'ABCD')

            # returns OctetString('ABCD') value object
            s.getComponentByPosition(0, instantiate=False)

            s.clear()

            # returns noValue
            s.getComponentByPosition(0, instantiate=False)
        N)r4  r   r   rR  r9  )r$   r-  rO  rP  rL  r'   r'   r(   r8  p	  s     D



z)SequenceAndSetBase.getComponentByPositionc                 C   s  | j }| j}| jtkrg }n| j}z|| }	W n8 tk
rf   t}	|rb||k rXtdtg| }Y nX |tkr|r||}t|t	j
r|j|| jd}n|	tkrtdnt|t	js6|r||}
t|
t	jr|
j|d}ntd|jj n8|	tk	r"t|	t	jr"|	j|d}ntd|jj nz|sH|sH|r|r||}
|
tk	r| jrr|
jpv|
j}|||o||o|s|| jstd||f |s|| jkr|||< n0t||kr|| | j| n
td|| _| S )	a  Assign |ASN.1| type component by position.

        Equivalent to Python sequence item assignment operation (e.g. `[]`).

        Parameters
        ----------
        idx : :class:`int`
            Component index (zero-based). Must either refer to existing
            component (if *componentType* is set) or to N+1 component
            otherwise. In the latter case a new component of given ASN.1
            type gets instantiated and appended to |ASN.1| sequence.

        Keyword Args
        ------------
        value: :class:`object` or :py:class:`~pyasn1.type.base.PyAsn1Item` derivative
            A Python value to initialize |ASN.1| component with (if *componentType* is set)
            or ASN.1 value object to assign to |ASN.1| component.
            If `value` is not given, schema object will be set as a component.

        verifyConstraints : :class:`bool`
             If :obj:`False`, skip constraints validation

        matchTags: :class:`bool`
             If :obj:`False`, skip component tags matching

        matchConstraints: :class:`bool`
             If :obj:`False`, skip component constraints matching

        Returns
        -------
        self
        zcomponent index out of rangerH  rS  rT  z%s can cast only scalar valuesz%s undefined component typerU  zComponent index out of range)r2  r  r4  r   r   r   rw   getTypeByPositionrF   r   r5  r+   isDefaultedrW  r!   r   r   rX  rY  rZ  ZopenTyper  r   r  r  )r$   r-  r%   r[  r\  r]  r2  ZcomponentTypeLenr^  r_  subComponentTyper`  r'   r'   r(   r9  	  sh    $








z)SequenceAndSetBase.setComponentByPositionc                 C   s   | j tkrdS | j}|rft|jD ]@\}}|js"|jr8q"| j sD dS | j | }|tks\|js" dS q"n | j D ]}|tks~|jsl dS qldS )a  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).

        It is sufficient for |ASN.1| objects to have all non-optional and non-defaulted
        components being value objects to be considered as a value objects as a whole.
        In other words, even having one or more optional components not turned into
        value objects, |ASN.1| object is still considered as a value object. Defaulted
        components are normally value objects by default.
        FT)r4  r   r2  rD  Z
namedTypesr  Z
isOptionalrR  )r$   r2  r-  r  rL  r'   r'   r(   rR  9
  s     "



zSequenceAndSetBase.isValuec                 C   s   | j tks| jsdS | jtkr"dS i }t| jD ]&\}}|tkrBq0| j |}|||< q0z| | W n& tjk
r   t	 d }| Y S X dS rr  )
r2  r   r   r4  rD  r}  r   rw   rx   ry   )r$   rs  r-  r%   r   r   r'   r'   r(   rt  s
  s     


z!SequenceAndSetBase.isInconsistentr   c                 C   s   |d7 }| j jd }t| jD ]\\}}|tk	r|jr|d| 7 }| jrX|| j|7 }n|| j|7 }d||	|f }q|S )zReturn an object representation string.

        Returns
        -------
        : :class:`str`
            Human-friendly object representation.
        rt   ri  rj  z%s=%s
)
r   r   rD  r4  r   rR  r2  r}  r  r   rk  r'   r'   r(   r   
  s     zSequenceAndSetBase.prettyPrintc                 C   s   |d7 }d| j | jjf }t| j p*| jD ]V\}}|d| 7 }| jr^|d| j| 7 }n|d| j| 7 }d||	|f }q.|d d|d   d S )Nrt   rm  rj  z"%s"z%s = %s
rn  ro  )
r   r   r   rD  r2  r=  r4  r}  r  rp  )r$   r   rl  r-  r2  r'   r'   r(   rp  
  s     z"SequenceAndSetBase.prettyPrintTypec                 C   s   | S r)   r'   rS   r'   r'   r(   setDefaultComponents
  s    z'SequenceAndSetBase.setDefaultComponentsc                 C   s   | j r| jS d S r)   )r  r2  rS   r'   r'   r(   getComponentType
  s    z#SequenceAndSetBase.getComponentTypec                 C   s   | j r| j| jS d S r)   )r  r2  r   r,  r'   r'   r(   r}  
  s    z$SequenceAndSetBase.getNameByPositionN)r   )r   )$r   r   r   r   r	   
NamedTypesr2  objectr  r"   r   r;  r
  r   r   r=  r  r?  r  rf  rh  r1  re  rM  r   r  r  r8  r9  rR  rt  r   rp  r  r  r}  r'   r'   r'   r(   r   O  sN   -)	


'
.^
k
9
%

c                   @   sR   e Zd ZejZeeejej	dZ
e Ze Ze Zdd Zdd ZdS )r   r   c                 C   s   | j r| j |S d S r)   )r2  ZgetTagMapNearPositionr,  r'   r'   r(   getComponentTagMapNearPosition
  s    z'Sequence.getComponentTagMapNearPositionc                 C   s   | j r| j ||S |S d S r)   )r2  ZgetPositionNearType)r$   r   r-  r'   r'   r(   getComponentPositionNearType
  s    z%Sequence.getComponentPositionNearTypeN)r   r   r   r   r   r   r   r   r   rv  r   r   r   r   r	   r  r2  r   r   r  r  r'   r'   r'   r(   r   
  s   c                   @   s|   e Zd ZejZeeejej	dZ
e Ze Ze ZdddZeddfddZeddddfdd	Zed
d ZdS )r   rw  Fc                 C   s   | S r)   r'   )r$   	innerFlagr'   r'   r(   getComponent  s    zSet.getComponentTc                 C   s:   | j | j|||d}|r2t|tr2|jddS |S dS )a8  Returns |ASN.1| type component by ASN.1 tag.

        Parameters
        ----------
        tagSet : :py:class:`~pyasn1.type.tag.TagSet`
            Object representing ASN.1 tags to identify one of
            |ASN.1| object component

        Keyword Args
        ------------
        default: :class:`object`
            If set and requested component is a schema object, return the `default`
            object instead of the requested component.

        instantiate: :class:`bool`
            If :obj:`True` (default), inner component will be automatically
            instantiated.
            If :obj:`False` either existing component or the :class:`noValue`
            object will be returned.

        Returns
        -------
        : :py:class:`~pyasn1.type.base.PyAsn1Item`
            a pyasn1 object
        r  Tr  N)r8  r2  getPositionByTyperF   r   r  )r$   r   rO  rP  r  rL  r'   r'   r(   getComponentByType  s    
 zSet.getComponentByTypec           	      C   sl   | j |}|rV| j |}|jr4| |||||S | |}|j||||||dS n| |||||S dS )a!  Assign |ASN.1| type component by ASN.1 tag.

        Parameters
        ----------
        tagSet : :py:class:`~pyasn1.type.tag.TagSet`
            Object representing ASN.1 tags to identify one of
            |ASN.1| object component

        Keyword Args
        ------------
        value: :class:`object` or :py:class:`~pyasn1.type.base.PyAsn1Item` derivative
            A Python value to initialize |ASN.1| component with (if *componentType* is set)
            or ASN.1 value object to assign to |ASN.1| component.
            If `value` is not given, schema object will be set as a component.

        verifyConstraints : :class:`bool`
            If :obj:`False`, skip constraints validation

        matchTags: :class:`bool`
            If :obj:`False`, skip component tags matching

        matchConstraints: :class:`bool`
            If :obj:`False`, skip component constraints matching

        innerFlag: :class:`bool`
            If :obj:`True`, search for matching *tagSet* recursively.

        Returns
        -------
        self
        r  N)r2  r  r  r   r9  r8  setComponentByType)	r$   r   r%   r[  r\  r]  r  r-  r2  r'   r'   r(   r  ,  s6    $    
         zSet.setComponentByTypec                 C   s   | j r| j jS d S r)   )r2  tagMapUniquerS   r'   r'   r(   rb  c  s    zSet.componentTagMapN)F)r   r   r   r   r   r   r   r   r   rv  r   r	   r  r2  r   r   r   r   r   r  r   r  r  r1  rb  r'   r'   r'   r(   r   
  s(   
 
&
7c                   @   s6  e Zd ZdZe Ze Z	e
e
ddZe Z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 d d! Z!d"d# Z"d$d% Z#e$d&fd'd(Z%e$d&d&d&fd)d*Z&e'd+d, Z(e'd-d. Z)d:d0d1Z*d;d2d3Z+e'd4d5 Z,d6d7 Z-d8d9 Z.dS )<r   a  Create |ASN.1| schema or value object.

    |ASN.1| class is based on :class:`~pyasn1.type.base.ConstructedAsn1Type`,
    its objects are mutable and duck-type Python :class:`list` objects.

    Keyword Args
    ------------
    componentType: :py:class:`~pyasn1.type.namedtype.NamedType`
        Object holding named ASN.1 types allowed within this collection

    tagSet: :py:class:`~pyasn1.type.tag.TagSet`
        Object representing non-default ASN.1 tag(s)

    subtypeSpec: :py:class:`~pyasn1.type.constraint.ConstraintsIntersection`
        Object representing non-default ASN.1 subtype constraint(s).  Constraints
        verification for |ASN.1| type can only occur on explicit
        `.isInconsistent` call.

    Examples
    --------

    .. code-block:: python

        class Afters(Choice):
            '''
            ASN.1 specification:

            Afters ::= CHOICE {
                cheese  [0] IA5String,
                dessert [1] IA5String
            }
            '''
            componentType = NamedTypes(
                NamedType('cheese', IA5String().subtype(
                    implicitTag=Tag(tagClassContext, tagFormatSimple, 0)
                ),
                NamedType('dessert', IA5String().subtype(
                    implicitTag=Tag(tagClassContext, tagFormatSimple, 1)
                )
            )

        afters = Afters()
        afters['cheese'] = 'Mascarpone'
    rt   Nc                 C   s   | j r| j | j |kS tS r)   r4  _currentIdxNotImplementedr   r'   r'   r(   rp     s    zChoice.__eq__c                 C   s   | j r| j | j |kS tS r)   r  r   r'   r'   r(   rq     s    zChoice.__ne__c                 C   s   | j r| j | j |k S tS r)   r  r   r'   r'   r(   rn     s    zChoice.__lt__c                 C   s   | j r| j | j |kS tS r)   r  r   r'   r'   r(   ro     s    zChoice.__le__c                 C   s   | j r| j | j |kS tS r)   r  r   r'   r'   r(   rr     s    zChoice.__gt__c                 C   s   | j r| j | j |kS tS r)   r  r   r'   r'   r(   rs     s    zChoice.__ge__r   r   c                 C   s   | j r
dpdS NTFrc  rS   r'   r'   r(   r*    s    zChoice.__nonzero__c                 C   s   | j r
dpdS r  rc  rS   r'   r'   r(   r+    s    zChoice.__bool__c                 C   s   | j d k	rdpdS )Nrt   r   )r  rS   r'   r'   r(   r     s    zChoice.__len__c                 C   s"   | j d krdS || j| j   kS )NFr  r2  getNamer  r'   r'   r(   r
    s    
zChoice.__contains__c                 c   s$   | j d krt| j| j   V  d S r)   )r  StopIterationr2  r  rS   r'   r'   r(   r     s    
zChoice.__iter__c                 c   s   | j d k	r| j| j  V  d S r)   )r  r4  rS   r'   r'   r(   r=    s    
zChoice.valuesc                 c   s    | j d k	r| j| j   V  d S r)   r  rS   r'   r'   r(   r    s    
zChoice.keysc                 c   s*   | j d k	r&| j| j   | | j  fV  d S r)   r  rS   r'   r'   r(   r?    s    
zChoice.itemsc                 C   s   | j d krtdd S )NComponent not chosen)r  r   rw   rS   r'   r'   r(   checkConsistency  s    
zChoice.checkConsistencyc                 C   sr   z|   }W n tjk
r"   Y nLX t|tr6|j}n|j}t|tjr^|	||j
|d n|	||
  d S rG  )r  r   rw   rF   r   effectiveTagSetr   r   r5  r  r+   )r$   rK  rI  	componentr   r'   r'   r(   rM    s    
 
zChoice._cloneComponentValuesTc                 C   s6   t j}| jd ks| j|kr,t j| |||dS | j| S )Nr  )r   r   r  r8  r4  )r$   r-  rO  rP  r   r'   r'   r(   r8     s    
zChoice.getComponentByPositionc                 C   s>   | j }t| ||||| || _ |dk	r:||kr:t| j|< | S )a  Assign |ASN.1| type component by position.

        Equivalent to Python sequence item assignment operation (e.g. `[]`).

        Parameters
        ----------
        idx: :class:`int`
            Component index (zero-based). Must either refer to existing
            component or to N+1 component. In the latter case a new component
            type gets instantiated (if *componentType* is set, or given ASN.1
            object is taken otherwise) and appended to the |ASN.1| sequence.

        Keyword Args
        ------------
        value: :class:`object` or :py:class:`~pyasn1.type.base.PyAsn1Item` derivative
            A Python value to initialize |ASN.1| component with (if *componentType* is set)
            or ASN.1 value object to assign to |ASN.1| component. Once a new value is
            set to *idx* component, previous value is dropped.
            If `value` is not given, schema object will be set as a component.

        verifyConstraints : :class:`bool`
            If :obj:`False`, skip constraints validation

        matchTags: :class:`bool`
            If :obj:`False`, skip component tags matching

        matchConstraints: :class:`bool`
            If :obj:`False`, skip component constraints matching

        Returns
        -------
        self
        N)r  r   r9  r   r4  )r$   r-  r%   r[  r\  r]  ZoldIdxr'   r'   r(   r9  	  s    %
zChoice.setComponentByPositionc                 C   s   | j r| j S |  }|jS dS )zwReturn a :class:`~pyasn1.type.tag.TagSet` object of the currently initialized component or self (if |ASN.1| is tagged).N)r   r  r  )r$   r  r'   r'   r(   r  5  s    zChoice.effectiveTagSetc                 C   s   | j rtj| S | jjS dS z"Return a :class:`~pyasn1.type.tagmap.TagMap` object mapping
            ASN.1 tags to ASN.1 objects contained within callee.
        N)r   r   ra  fgetr2  r  rS   r'   r'   r(   ra  >  s    zChoice.tagMapFc                 C   sB   | j dkrtdn(| j| j  }|r:t|tr:||S |S dS )zReturn currently assigned component of the |ASN.1| object.

        Returns
        -------
        : :py:class:`~pyasn1.type.base.PyAsn1Item`
            a PyASN1 object
        Nr  )r  r   rw   r4  rF   r   r  r$   r  cr'   r'   r(   r  H  s    

zChoice.getComponentc                 C   sL   | j dkrtdn2|r:| j| j  }t|tr:||S | j| j S dS )zReturn the name of currently assigned component of the |ASN.1| object.

        Returns
        -------
        : :py:class:`str`
            |ASN.1| component name
        Nr  )	r  r   rw   r4  rF   r   r  r2  r}  r  r'   r'   r(   r  Y  s    


zChoice.getNamec                 C   s(   | j dkrdS | j| j  }|tk	o&|jS )a~  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).
        NF)r  r4  r   rR  rq  r'   r'   r(   rR  j  s    
zChoice.isValuec                 C   s   d | _ t| S r)   )r  r   rf  rS   r'   r'   r(   rf    s    zChoice.clearc                 C   s   | j S r)   )Z	minTagSetrS   r'   r'   r(   getMinTagSet  s    zChoice.getMinTagSet)F)F)/r   r   r   r   r   TagSetr   r	   r  r2  r   r   ZValueSizeConstraintr   r   r   r   r  rp   rq   rn   ro   rr   rs   rx   r   r*  r+  r   r
  r   r=  r  r?  r  rM  r   r8  r9  r1  r  ra  r  r  rR  rf  r  r'   r'   r'   r(   r   i  sR   /

	
,

	


"c                   @   s4   e Zd ZdZe Ze Z	e
 Zedd ZdS )r   aH	  Create |ASN.1| schema or value object.

    |ASN.1| class is based on :class:`~pyasn1.type.base.SimpleAsn1Type`,
    its objects are immutable and duck-type Python 2 :class:`str` or Python 3
    :class:`bytes`. When used in Unicode context, |ASN.1| type assumes
    "|encoding|" serialisation.

    Keyword Args
    ------------
    value: :class:`unicode`, :class:`str`, :class:`bytes` or |ASN.1| object
        :class:`str` (Python 2) or :class:`bytes` (Python 3), alternatively
        :class:`unicode` object (Python 2) or :class:`str` (Python 3)
        representing character string to be serialised into octets (note
        `encoding` parameter) or |ASN.1| object.
        If `value` is not given, schema object will be created.

    tagSet: :py:class:`~pyasn1.type.tag.TagSet`
        Object representing non-default ASN.1 tag(s)

    subtypeSpec: :py:class:`~pyasn1.type.constraint.ConstraintsIntersection`
        Object representing non-default ASN.1 subtype constraint(s). Constraints
        verification for |ASN.1| type occurs automatically on object
        instantiation.

    encoding: :py:class:`str`
        Unicode codec ID to encode/decode :class:`unicode` (Python 2) or
        :class:`str` (Python 3) the payload when |ASN.1| object is used
        in text string context.

    binValue: :py:class:`str`
        Binary string initializer to use instead of the *value*.
        Example: '10110011'.

    hexValue: :py:class:`str`
        Hexadecimal string initializer to use instead of the *value*.
        Example: 'DEADBEEF'.

    Raises
    ------
    ~pyasn1.error.ValueConstraintError, ~pyasn1.error.PyAsn1Error
        On constraint violation or bad initializer.

    Examples
    --------
    .. code-block:: python

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

            Error ::= SEQUENCE {
                code      INTEGER,
                parameter ANY DEFINED BY code  -- Either INTEGER or REAL
            }
            '''
            componentType=NamedTypes(
                NamedType('code', Integer()),
                NamedType('parameter', Any(),
                          openType=OpenType('code', {1: Integer(),
                                                     2: Real()}))
            )

        error = Error()
        error['code'] = 1
        error['parameter'] = Integer(1234)
    c                 C   sH   z| j W S  tk
rB   t| j| itjjtji| | _ | j  Y S X dS r  )Z_tagMapAttributeErrorr   ZTagMapr   r   ZendOfOctetsrS   r'   r'   r(   ra    s    z
Any.tagMapN)r   r   r   r   r   r  r   r   r   r   r   r   r   r1  ra  r'   r'   r'   r(   r     s   E)0rd   rx   Zpyasn1r   Zpyasn1.codec.berr   Zpyasn1.compatr   r   r   Zpyasn1.typer   r   r	   r
   r   r   r   r   __all__r!   r   r   r   rU   ZSizedIntegerBaserR   r   r   r   	NameErrorr   r   r   rG   r  r   r   r   r5  r   r   r   r   r   r   r   r   r'   r'   r'   r(   <module>   s   
         	 y?  f  L3

   #G   N      #|  0