B
    ÔùÓ`
-  ã               @   sd  d dl Z d dlmZmZmZmZmZmZmZm	Z	m
Z
 d dlmZ d dlmZ d dlZe jd  dk Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Zdd„ ZG dd„ deƒZdZdd„ Zdd„ Zeƒ Zdd„ Zeeedeefdd„Zdd„ ZG dd„ de ƒZ!ed ed!iZ"i Z#d"d#„ Z$d$d%„ Z%d&d'„ Z&d(d)„ Z'd5d*d+„Z(d6d,d-„Z)d.d„ Z*i Z+d/d0„ Z,d1d2„ Z-defd3d4„Z.dS )7é    N)	ÚCheckedPMapÚCheckedPSetÚCheckedPVectorÚCheckedTypeÚInvariantExceptionÚ_restore_pickleÚget_typeÚmaybe_parse_user_typeÚmaybe_parse_many_user_types)Úoptional)Úwrap_invarianté   c                sZ   t t‡ fdd„|D ƒg ƒƒ| ˆ < x4t|  ¡ ƒD ]$\}}t|tƒr.|| ˆ  |< | |= q.W d S )Nc                s"   g | ]}t |j ˆ i ¡ ¡ ƒ‘qS © )ÚlistÚ__dict__ÚgetÚitems)Ú.0Úb)Únamer   ú</tmp/pip-unpacked-wheel-by61ilhc/pyrsistent/_field_common.pyú
<listcomp>   s    zset_fields.<locals>.<listcomp>)ÚdictÚsumr   r   Ú
isinstanceÚ_PField)ÚdctÚbasesr   ÚkÚvr   )r   r   Ú
set_fields   s
     
r    c                s4   t dd„ ‡ fdd„|D ƒD ƒƒ}|r0t|ddƒ‚d S )Nc             s   s   | ]\}}|s|V  qd S )Nr   )r   Zis_okZ
error_coder   r   r   ú	<genexpr>   s    z*check_global_invariants.<locals>.<genexpr>c             3   s   | ]}|ˆ ƒV  qd S )Nr   )r   Ú	invariant)Úsubjectr   r   r!       s    r   zGlobal invariant failed)Útupler   )r#   Z
invariantsZerror_codesr   )r#   r   Úcheck_global_invariants   s    r%   c             C   s&   t |tƒr| tkr| |¡S | ||ƒS )N)r   r   ÚPFIELD_NO_SERIALIZERÚ	serialize)Ú
serializerÚformatÚvaluer   r   r   r'   %   s    
r'   c                sN   |j rJt‡ fdd„|j D ƒƒsJt ˆ ƒ}d | j||j¡}t| ||j ||ƒ‚d S )Nc             3   s   | ]}t ˆ t|ƒƒV  qd S )N)r   r   )r   Út)r*   r   r   r!   -   s    zcheck_type.<locals>.<genexpr>z'Invalid type for field {0}.{1}, was {2})ÚtypeÚanyr)   Ú__name__Ú
PTypeError)Zdestination_clsÚfieldr   r*   Úactual_typeÚmessager   )r*   r   Ú
check_type,   s    r3   c             C   s:   t |ƒtkrdS t|ƒ}t|ƒdkr(dS tt|d ƒ| ƒS )NTr   F)r,   Úsetr$   ÚlenÚ
issubclassr   )Útype_clsZ
field_typeÚtypesr   r   r   Úis_type_cls3   s    r9   c             C   sD   |sdS t | |jƒsdS tr.dt |j¡jkS dt |j¡jkS d S )NFÚignore_extra)	r9   r,   ÚPY2ÚinspectÚ
getargspecÚfactoryÚargsÚ	signatureÚ
parameters)r7   r0   r:   r   r   r   Úis_field_ignore_extra_complaint<   s    rB   c               @   s$   e Zd ZdZdd„ Zedd„ ƒZdS )r   )r,   r"   ÚinitialÚ	mandatoryÚ_factoryr(   c             C   s(   || _ || _|| _|| _|| _|| _d S )N)r,   r"   rC   rD   rE   r(   )Úselfr,   r"   rC   rD   r>   r(   r   r   r   Ú__init__N   s    z_PField.__init__c             C   s@   | j tkr:t| jƒdkr:tt| jƒd ƒ}t|tƒr:|jS | j S )Né   r   )	rE   ÚPFIELD_NO_FACTORYr5   r,   r   r$   r6   r   Úcreate)rF   Útypr   r   r   r>   V   s
    
z_PField.factoryN)r.   Ú
__module__Ú__qualname__Ú	__slots__rG   Úpropertyr>   r   r   r   r   r   K   s   r   r   c             C   s   dS )N)TNr   )Ú_r   r   r   Ú<lambda>a   s    rQ   c             C   s   | S )Nr   )Úxr   r   r   rQ   b   s    c             C   s   |S )Nr   )rP   r*   r   r   r   rQ   d   s    Fc       	      C   sf   t | tttfƒrtt| ƒƒ}ntt| ƒƒ}|tkrBt|ƒrBt|ƒn|}t	||||||d�}t
|ƒ |S )a  
    Field specification factory for :py:class:`PRecord`.

    :param type: a type or iterable with types that are allowed for this field
    :param invariant: a function specifying an invariant that must hold for the field
    :param initial: value of field if not specified when instantiating the record
    :param mandatory: boolean specifying if the field is mandatory or not
    :param factory: function called when field is set.
    :param serializer: function that returns a serialized version of the field
    )r,   r"   rC   rD   r>   r(   )r   r   r4   r$   r
   r	   ÚPFIELD_NO_INVARIANTÚcallabler   r   Ú_check_field_parameters)	r,   r"   rC   rD   r>   r(   r8   Zinvariant_functionr0   r   r   r   r0   g   s    r0   c                s¶   x4ˆ j D ]*}t|t ƒst|tƒstd t |ƒ¡ƒ‚qW ˆ jtk	r|tˆ jƒs|ˆ j r|t‡ fdd„ˆ j D ƒƒs|td t ˆ jƒ¡ƒ‚tˆ j	ƒsŽtdƒ‚tˆ j
ƒs tdƒ‚tˆ jƒs²tdƒ‚d S )Nz Type parameter expected, not {0}c             3   s   | ]}t ˆ j|ƒV  qd S )N)r   rC   )r   r+   )r0   r   r   r!   ’   s    z*_check_field_parameters.<locals>.<genexpr>zInitial has invalid type {0}zInvariant must be callablezFactory must be callablezSerializer must be callable)r,   r   ÚstrÚ	TypeErrorr)   rC   ÚPFIELD_NO_INITIALrT   r-   r"   r>   r(   )r0   r+   r   )r0   r   rU   ‹   s    




rU   c                   s    e Zd ZdZ‡ fdd„Z‡  ZS )r/   a  
    Raised when trying to assign a value with a type that doesn't match the declared type.

    Attributes:
    source_class -- The class of the record
    field -- Field name
    expected_types  -- Types allowed for the field
    actual_type -- The non matching type
    c                s.   t t| ƒj||Ž || _|| _|| _|| _d S )N)Úsuperr/   rG   Úsource_classr0   Úexpected_typesr1   )rF   rZ   r0   r[   r1   r?   Úkwargs)Ú	__class__r   r   rG   ©   s
    zPTypeError.__init__)r.   rL   rM   Ú__doc__rG   Ú__classcell__r   r   )r]   r   r/   Ÿ   s   	r/   ZPVectorZPSetc             C   s   t | |f }t||ƒS )z=Unpickling function for auto-generated PVec/PSet field types.)Ú_seq_field_typesr   )Úchecked_classÚ	item_typeÚdataÚtype_r   r   r   Ú_restore_seq_field_pickle¹   s    re   c             C   s   d  dd„ | D ƒ¡S )z4Convert a tuple of types to a human-readable string.Ú c             s   s   | ]}t |ƒj ¡ V  qd S )N)r   r.   Ú
capitalize)r   rK   r   r   r   r!   À   s    z"_types_to_names.<locals>.<genexpr>)Újoin)r8   r   r   r   Ú_types_to_names¾   s    ri   c                sX   t  ˆ ˆf¡}|dk	r|S G ‡ ‡fdd„dˆ ƒ}tˆ  }t|jƒ| |_|t ˆ ˆf< |S )zFCreate a subclass of the given checked class with the given item type.Nc                   s   e Zd Z”Z‡ ‡fdd„ZdS )z%_make_seq_field_type.<locals>.TheTypec                s   t ˆ ˆt| ƒffS )N)re   r   )rF   )ra   rb   r   r   Ú
__reduce__Ë   s    z0_make_seq_field_type.<locals>.TheType.__reduce__N)r.   rL   rM   Z__type__rj   r   )ra   rb   r   r   ÚTheTypeÈ   s   rk   )r`   r   ÚSEQ_FIELD_TYPE_SUFFIXESri   Z_checked_typesr.   )ra   rb   rd   rk   Úsuffixr   )ra   rb   r   Ú_make_seq_field_typeÂ   s    rn   c                sD   t | |ƒ‰ |rd‡ fdd„	}nˆ j}t|r2tˆ ƒnˆ |d||ƒd�S )aˆ  
    Create checked field for either ``PSet`` or ``PVector``.

    :param checked_class: ``CheckedPSet`` or ``CheckedPVector``.
    :param item_type: The required type for the items in the set.
    :param optional: If true, ``None`` can be used as a value for
        this field.
    :param initial: Initial value to pass to factory.

    :return: A ``field`` containing a checked class.
    NFc                s    | d krd S ˆ j | ||d�S d S )N)Ú_factory_fieldsr:   )rJ   )Úargumentro   r:   )rk   r   r   r>   ã   s    z _sequence_field.<locals>.factoryT)r,   r>   rD   rC   )NF)rn   rJ   r0   Úoptional_type)ra   rb   r   rC   r>   r   )rk   r   Ú_sequence_fieldÔ   s    
rr   c             C   s   t t| ||ƒS )al  
    Create checked ``PSet`` field.

    :param item_type: The required type for the items in the set.
    :param optional: If true, ``None`` can be used as a value for
        this field.
    :param initial: Initial value to pass to factory if no value is given
        for the field.

    :return: A ``field`` containing a ``CheckedPSet`` of the given type.
    )rr   r   )rb   r   rC   r   r   r   Ú
pset_fieldð   s    rs   c             C   s   t t| ||ƒS )au  
    Create checked ``PVector`` field.

    :param item_type: The required type for the items in the vector.
    :param optional: If true, ``None`` can be used as a value for
        this field.
    :param initial: Initial value to pass to factory if no value is given
        for the field.

    :return: A ``field`` containing a ``CheckedPVector`` of the given type.
    )rr   r   )rb   r   rC   r   r   r   Úpvector_field   s    rt   c             C   s   dS )N)Trf   r   )Úitemr   r   r   rQ     s    c             C   s   t | |f }t||ƒS )z8Unpickling function for auto-generated PMap field types.)Ú_pmap_field_typesr   )Úkey_typeÚ
value_typerc   rd   r   r   r   Ú_restore_pmap_field_pickle  s    ry   c                sZ   t  ˆ ˆf¡}|dk	r|S G ‡ ‡fdd„dtƒ}d t|jƒt|jƒ¡|_|t ˆ ˆf< |S )zDCreate a subclass of CheckedPMap with the given key and value types.Nc                   s   e Zd Z” Z”Zdd„ ZdS )z%_make_pmap_field_type.<locals>.TheMapc             S   s   t | j| jt| ƒffS )N)ry   Ú__key_type__Ú__value_type__r   )rF   r   r   r   rj   %  s    z0_make_pmap_field_type.<locals>.TheMap.__reduce__N)r.   rL   rM   rz   r{   rj   r   )rw   rx   r   r   ÚTheMap!  s   r|   z{0}To{1}PMap)rv   r   r   r)   ri   Z_checked_key_typesZ_checked_value_typesr.   )rw   rx   rd   r|   r   )rw   rx   r   Ú_make_pmap_field_type  s    r}   c                sB   t | |ƒ‰ |r‡ fdd„}nˆ j}tdˆ ƒ |r6tˆ ƒnˆ ||d�S )ad  
    Create a checked ``PMap`` field.

    :param key: The required type for the keys of the map.
    :param value: The required type for the values of the map.
    :param optional: If true, ``None`` can be used as a value for
        this field.
    :param invariant: Pass-through to ``field``.

    :return: A ``field`` containing a ``CheckedPMap``.
    c                s   | d krd S ˆ   | ¡S d S )N)rJ   )rp   )r|   r   r   r>   ?  s    zpmap_field.<locals>.factoryT)rD   rC   r,   r>   r"   )r}   rJ   r0   rq   )rw   rx   r   r"   r>   r   )r|   r   Ú
pmap_field0  s    
r~   )Fr   )Fr   )/ÚsysZpyrsistent._checked_typesr   r   r   r   r   r   r   r	   r
   r   rq   r   r<   Úversion_infor;   r    r%   r'   r3   r9   rB   Úobjectr   ZPFIELD_NO_TYPErS   rI   rX   r&   r0   rU   rW   r/   rl   r`   re   ri   rn   rr   rs   rt   Ú_validrv   ry   r}   r~   r   r   r   r   Ú<module>   sF   ,		#

