3
s¨Z²>  ã            	   @   sÞ   d dl Z d dlmZ ddddddd	d
dg	ZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZ	G dd„ deƒZ
G dd„ deƒZG dd	„ d	eƒZG dd„ deƒZG dd
„ d
eƒZG dd„ deƒZdS )é    N)ÚerrorÚSingleValueConstraintÚContainedSubtypeConstraintÚValueRangeConstraintÚValueSizeConstraintÚPermittedAlphabetConstraintÚInnerTypeConstraintÚConstraintsExclusionÚConstraintsIntersectionÚConstraintsUnionc               @   s¦   e Zd Zdd„ Zd&dd„Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zejd dkrjd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S )'ÚAbstractConstraintc             G   s*   t ƒ | _| j|ƒ t| jj| jfƒ| _d S )N)ÚsetÚ	_valueMapÚ
_setValuesÚhashÚ	__class__Ú__name__Ú_valuesÚ_AbstractConstraint__hash)ÚselfÚvalues© r   ú8/usr/lib/python3/dist-packages/pyasn1/type/constraint.pyÚ__init__   s    
zAbstractConstraint.__init__Nc             C   sP   | j s
d S y| j||ƒ W n0 tjk
rJ   tjd| tjƒ d f ƒ‚Y nX d S )Nz%s failed at: %ré   )r   Ú
_testValuer   ÚValueConstraintErrorÚsysÚexc_info)r   ÚvalueÚidxr   r   r   Ú__call__   s    zAbstractConstraint.__call__c             C   s@   d| j jt| ƒf }| jr8|ddjdd„ | jD ƒƒ 7 }d| S )Nz%s object at 0x%xz
 consts %sz, c             S   s   g | ]}t |ƒ‘qS r   )Úrepr)Ú.0Úxr   r   r   ú
<listcomp>+   s    z/AbstractConstraint.__repr__.<locals>.<listcomp>z<%s>)r   r   Úidr   Újoin)r   Zrepresentationr   r   r   Ú__repr__'   s    zAbstractConstraint.__repr__c             C   s   | |krdp| j |kS )NT)r   )r   Úotherr   r   r   Ú__eq__/   s    zAbstractConstraint.__eq__c             C   s
   | j |kS )N)r   )r   r)   r   r   r   Ú__ne__2   s    zAbstractConstraint.__ne__c             C   s
   | j |k S )N)r   )r   r)   r   r   r   Ú__lt__5   s    zAbstractConstraint.__lt__c             C   s
   | j |kS )N)r   )r   r)   r   r   r   Ú__le__8   s    zAbstractConstraint.__le__c             C   s
   | j |kS )N)r   )r   r)   r   r   r   Ú__gt__;   s    zAbstractConstraint.__gt__c             C   s
   | j |kS )N)r   )r   r)   r   r   r   Ú__ge__>   s    zAbstractConstraint.__ge__r   é   c             C   s   | j r
dpdS )NTF)r   )r   r   r   r   Ú__nonzero__B   s    zAbstractConstraint.__nonzero__c             C   s   | j r
dpdS )NTF)r   )r   r   r   r   Ú__bool__E   s    zAbstractConstraint.__bool__c             C   s   | j S )N)r   )r   r   r   r   Ú__hash__H   s    zAbstractConstraint.__hash__c             C   s
   || _ d S )N)r   )r   r   r   r   r   r   K   s    zAbstractConstraint._setValuesc             C   s   t j|ƒ‚d S )N)r   r   )r   r   r    r   r   r   r   N   s    zAbstractConstraint._testValuec             C   s   | j S )N)r   )r   r   r   r   ÚgetValueMapR   s    zAbstractConstraint.getValueMapc             C   s$   || kp"| j  p"|| kp"| |jƒ kS )N)r   r4   )r   ÚotherConstraintr   r   r   ÚisSuperTypeOfU   s    z AbstractConstraint.isSuperTypeOfc             C   s    || kp|  p|| kp|| j kS )N)r   )r   r5   r   r   r   ÚisSubTypeOf\   s    zAbstractConstraint.isSubTypeOf)N)r   Ú
__module__Ú__qualname__r   r!   r(   r*   r+   r,   r-   r.   r/   r   Úversion_infor1   r2   r3   r   r   r4   r6   r7   r   r   r   r   r      s$   

r   c               @   s    e Zd ZdZdd„ Zdd„ ZdS )r   a  Create a SingleValueConstraint object.

    The SingleValueConstraint satisfies any value that
    is present in the set of permitted values.

    The SingleValueConstraint object can be applied to
    any ASN.1 type.

    Parameters
    ----------
    \*values: :class:`int`
        Full set of values permitted by this constraint object.

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

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

            Divisor-Of-6 ::= INTEGER (1 | 2 | 3 | 6)
            '''
            subtypeSpec = SingleValueConstraint(1, 2, 3, 6)

        # this will succeed
        divisor_of_six = DivisorOfSix(1)

        # this will raise ValueConstraintError
        divisor_of_six = DivisorOfSix(7)
    c             C   s   || _ t|ƒ| _d S )N)r   r   Ú_set)r   r   r   r   r   r   ƒ   s    z SingleValueConstraint._setValuesc             C   s   || j krtj|ƒ‚d S )N)r;   r   r   )r   r   r    r   r   r   r   ‡   s    
z SingleValueConstraint._testValueN)r   r8   r9   Ú__doc__r   r   r   r   r   r   r   c   s   c               @   s   e Zd ZdZdd„ ZdS )r   aÂ  Create a ContainedSubtypeConstraint object.

    The ContainedSubtypeConstraint satisfies any value that
    is present in the set of permitted values and also
    satisfies included constraints.

    The ContainedSubtypeConstraint object can be applied to
    any ASN.1 type.

    Parameters
    ----------
    \*values:
        Full set of values and constraint objects permitted
        by this constraint object.

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

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

            Divisors-of-18 ::= INTEGER (INCLUDES Divisors-of-6 | 9 | 18)
            '''
            subtypeSpec = ContainedSubtypeConstraint(
                SingleValueConstraint(1, 2, 3, 6), 9, 18
            )

        # this will succeed
        divisor_of_eighteen = DivisorOfEighteen(9)

        # this will raise ValueConstraintError
        divisor_of_eighteen = DivisorOfEighteen(10)
    c             C   s>   x8| j D ].}t|tƒr"|||ƒ q|| jkrtj|ƒ‚qW d S )N)r   Ú
isinstancer   r;   r   r   )r   r   r    Ú
constraintr   r   r   r   °   s
    

z%ContainedSubtypeConstraint._testValueN)r   r8   r9   r<   r   r   r   r   r   r   Œ   s   #c               @   s    e Zd ZdZdd„ Zdd„ ZdS )r   a{  Create a ValueRangeConstraint object.

    The ValueRangeConstraint satisfies any value that
    falls in the range of permitted values.

    The ValueRangeConstraint object can only be applied
    to :class:`~pyasn1.type.univ.Integer` and
    :class:`~pyasn1.type.univ.Real` types.

    Parameters
    ----------
    start: :class:`int`
        Minimum permitted value in the range (inclusive)

    end: :class:`int`
        Maximum permitted value in the range (inclusive)

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

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

            TeenAgeYears ::= INTEGER (13 .. 19)
            '''
            subtypeSpec = ValueRangeConstraint(13, 19)

        # this will succeed
        teen_year = TeenAgeYears(18)

        # this will raise ValueConstraintError
        teen_year = TeenAgeYears(20)
    c             C   s"   || j k s|| jkrtj|ƒ‚d S )N)ÚstartÚstopr   r   )r   r   r    r   r   r   r   Ü   s    zValueRangeConstraint._testValuec             C   sd   t |ƒdkr tjd| jjf ƒ‚|\| _| _| j| jkrTtjd| jj| j| jf ƒ‚tj| |ƒ d S )Nr0   z%s: bad constraint valuesz5%s: screwed constraint values (start > stop): %s > %s)	Úlenr   ÚPyAsn1Errorr   r   r?   r@   r   r   )r   r   r   r   r   r   à   s    zValueRangeConstraint._setValuesN)r   r8   r9   r<   r   r   r   r   r   r   r   ¸   s   #c               @   s   e Zd ZdZdd„ ZdS )r   a  Create a ValueSizeConstraint object.

    The ValueSizeConstraint satisfies any value for
    as long as its size falls within the range of
    permitted sizes.

    The ValueSizeConstraint object can be applied
    to :class:`~pyasn1.type.univ.BitString`,
    :class:`~pyasn1.type.univ.OctetString` (including
    all :ref:`character ASN.1 types <type.char>`),
    :class:`~pyasn1.type.univ.SequenceOf`
    and :class:`~pyasn1.type.univ.SetOf` types.

    Parameters
    ----------
    minimum: :class:`int`
        Minimum permitted size of the value (inclusive)

    maximum: :class:`int`
        Maximum permitted size of the value (inclusive)

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

        class BaseballTeamRoster(SetOf):
            '''
            ASN.1 specification:

            BaseballTeamRoster ::= SET SIZE (1..25) OF PlayerNames
            '''
            componentType = PlayerNames()
            subtypeSpec = ValueSizeConstraint(1, 25)

        # this will succeed
        team = BaseballTeamRoster()
        team.extend(['Jan', 'Matej'])
        encode(team)

        # this will raise ValueConstraintError
        team = BaseballTeamRoster()
        team.extend(['Jan'] * 26)
        encode(team)

    Note
    ----
    Whenever ValueSizeConstraint is applied to mutable types
    (e.g. :class:`~pyasn1.type.univ.SequenceOf`,
    :class:`~pyasn1.type.univ.SetOf`), constraint
    validation only happens at the serialisation phase rather
    than schema instantiation phase (as it is with immutable
    types).
    c             C   s*   t |ƒ}|| jk s|| jkr&tj|ƒ‚d S )N)rA   r?   r@   r   r   )r   r   r    Z	valueSizer   r   r   r   &  s    zValueSizeConstraint._testValueN)r   r8   r9   r<   r   r   r   r   r   r   ð   s   5c               @   s    e Zd ZdZdd„ Zdd„ ZdS )r   aÇ  Create a PermittedAlphabetConstraint object.

    The PermittedAlphabetConstraint satisfies any character
    string for as long as all its characters are present in
    the set of permitted characters.

    The PermittedAlphabetConstraint object can only be applied
    to the :ref:`character ASN.1 types <type.char>` such as
    :class:`~pyasn1.type.char.IA5String`.

    Parameters
    ----------
    \*alphabet: :class:`str`
        Full set of characters permitted by this constraint object.

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

        class BooleanValue(IA5String):
            '''
            ASN.1 specification:

            BooleanValue ::= IA5String (FROM ('T' | 'F'))
            '''
            subtypeSpec = PermittedAlphabetConstraint('T', 'F')

        # this will succeed
        truth = BooleanValue('T')
        truth = BooleanValue('TF')

        # this will raise ValueConstraintError
        garbage = BooleanValue('TAF')
    c             C   s   || _ t|ƒ| _d S )N)r   r   r;   )r   r   r   r   r   r   O  s    z&PermittedAlphabetConstraint._setValuesc             C   s   | j j|ƒstj|ƒ‚d S )N)r;   Ú
issupersetr   r   )r   r   r    r   r   r   r   S  s    z&PermittedAlphabetConstraint._testValueN)r   r8   r9   r<   r   r   r   r   r   r   r   ,  s   "c               @   s    e Zd ZdZdd„ Zdd„ ZdS )r   z4Value must satisfy the type and presence constraintsc             C   sX   | j r| j |ƒ nB| jrT|| jkr,tj|ƒ‚| j| \}}|dkrLtj|ƒ‚||ƒ d S )NZABSENT)Ú*_InnerTypeConstraint__singleTypeConstraintÚ,_InnerTypeConstraint__multipleTypeConstraintr   r   )r   r   r    r>   Zstatusr   r   r   r   \  s    


zInnerTypeConstraint._testValuec             C   sV   i | _ d | _x8|D ]0}t|tƒr<|d |d f| j |d < q|| _qW tj| |ƒ d S )Nr   r0   r   )rE   rD   r=   Útupler   r   )r   r   Úvr   r   r   r   g  s    


zInnerTypeConstraint._setValuesN)r   r8   r9   r<   r   r   r   r   r   r   r   Y  s   c               @   s    e Zd ZdZdd„ Zdd„ ZdS )r	   af  Create a ConstraintsExclusion logic operator object.

    The ConstraintsExclusion logic operator succeeds when the
    value does *not* satisfy the operand constraint.

    The ConstraintsExclusion object can be applied to
    any constraint and logic operator object.

    Parameters
    ----------
    constraint:
        Constraint or logic operator object.

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

        class Lipogramme(IA5STRING):
            '''
            ASN.1 specification:

            Lipogramme ::=
                IA5String (FROM (ALL EXCEPT ("e"|"E")))
            '''
            subtypeSpec = ConstraintsExclusion(
                PermittedAlphabetConstraint('e', 'E')
            )

        # this will succeed
        lipogramme = Lipogramme('A work of fiction?')

        # this will raise ValueConstraintError
        lipogramme = Lipogramme('Eel')

    Warning
    -------
    The above example involving PermittedAlphabetConstraint might
    not work due to the way how PermittedAlphabetConstraint works.
    The other constraints might work with ConstraintsExclusion
    though.
    c             C   s:   y| j d ||ƒ W n tjk
r*   d S X tj|ƒ‚d S )Nr   )r   r   r   )r   r   r    r   r   r   r   ž  s
    zConstraintsExclusion._testValuec             C   s&   t |ƒdkrtjdƒ‚tj| |ƒ d S )Nr   zSingle constraint expected)rA   r   rB   r   r   )r   r   r   r   r   r   ¦  s    
zConstraintsExclusion._setValuesN)r   r8   r9   r<   r   r   r   r   r   r   r	   t  s   )c               @   s<   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )ÚAbstractConstraintSetc             C   s
   | j | S )N)r   )r   r    r   r   r   Ú__getitem__¯  s    z!AbstractConstraintSet.__getitem__c             C   s
   t | jƒS )N)Úiterr   )r   r   r   r   Ú__iter__²  s    zAbstractConstraintSet.__iter__c             C   s   | j | j|f Ž S )N)r   r   )r   r   r   r   r   Ú__add__µ  s    zAbstractConstraintSet.__add__c             C   s   | j |f| j Ž S )N)r   r   )r   r   r   r   r   Ú__radd__¸  s    zAbstractConstraintSet.__radd__c             C   s
   t | jƒS )N)rA   r   )r   r   r   r   Ú__len__»  s    zAbstractConstraintSet.__len__c             C   s8   || _ x,|D ]$}|r| jj|ƒ | jj|jƒ ƒ qW d S )N)r   r   ÚaddÚupdater4   )r   r   r>   r   r   r   r   À  s
    
z AbstractConstraintSet._setValuesN)	r   r8   r9   rI   rK   rL   rM   rN   r   r   r   r   r   rH   ­  s   rH   c               @   s   e Zd ZdZdd„ ZdS )r
   a8  Create a ConstraintsIntersection logic operator object.

    The ConstraintsIntersection logic operator only succeeds
    if *all* its operands succeed.

    The ConstraintsIntersection object can be applied to
    any constraint and logic operator objects.

    The ConstraintsIntersection object duck-types the immutable
    container object like Python :py:class:`tuple`.

    Parameters
    ----------
    \*constraints:
        Constraint or logic operator objects.

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

        class CapitalAndSmall(IA5String):
            '''
            ASN.1 specification:

            CapitalAndSmall ::=
                IA5String (FROM ("A".."Z"|"a".."z"))
            '''
            subtypeSpec = ConstraintsIntersection(
                PermittedAlphabetConstraint('A', 'Z'),
                PermittedAlphabetConstraint('a', 'z')
            )

        # this will succeed
        capital_and_small = CapitalAndSmall('Hello')

        # this will raise ValueConstraintError
        capital_and_small = CapitalAndSmall('hello')
    c             C   s   x| j D ]}|||ƒ qW d S )N)r   )r   r   r    r>   r   r   r   r   ï  s    z"ConstraintsIntersection._testValueN)r   r8   r9   r<   r   r   r   r   r   r
   È  s   &c               @   s   e Zd ZdZdd„ ZdS )r   a+  Create a ConstraintsUnion logic operator object.

    The ConstraintsUnion logic operator only succeeds if
    *at least a single* operand succeeds.

    The ConstraintsUnion object can be applied to
    any constraint and logic operator objects.

    The ConstraintsUnion object duck-types the immutable
    container object like Python :py:class:`tuple`.

    Parameters
    ----------
    \*constraints:
        Constraint or logic operator objects.

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

        class CapitalOrSmall(IA5String):
            '''
            ASN.1 specification:

            CapitalOrSmall ::=
                IA5String (FROM ("A".."Z") | FROM ("a".."z"))
            '''
            subtypeSpec = ConstraintsIntersection(
                PermittedAlphabetConstraint('A', 'Z'),
                PermittedAlphabetConstraint('a', 'z')
            )

        # this will succeed
        capital_or_small = CapitalAndSmall('Hello')

        # this will raise ValueConstraintError
        capital_or_small = CapitalOrSmall('hello!')
    c             C   sR   x8| j D ].}y|||ƒ W n tjk
r0   Y qX d S qW tjd| j |f ƒ‚d S )Nzall of %s failed for "%s")r   r   r   )r   r   r    r>   r   r   r   r     s    zConstraintsUnion._testValueN)r   r8   r9   r<   r   r   r   r   r   r   ô  s   &)r   Zpyasn1.typer   Ú__all__Úobjectr   r   r   r   r   r   r   r	   rH   r
   r   r   r   r   r   Ú<module>	   s"   O),8<-9,