o
    èÓáa».  ã                   @   sd  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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#eefd'd(„Z$ddeefd)d*„Z%ddeefd+d,„Z&d-d„ Z'i Z(d.d/„ Z)d0d1„ Z*defd2d3„Z+dS )4é    )	ÚCheckedPMapÚCheckedPSetÚCheckedPVectorÚCheckedTypeÚInvariantExceptionÚ_restore_pickleÚget_typeÚmaybe_parse_user_typeÚmaybe_parse_many_user_types)Úoptional)Úwrap_invariantNc                    sV   t t‡ fdd„|D ƒg ƒƒ| ˆ < t|  ¡ ƒD ]\}}t|tƒr(|| ˆ  |< | |= qd S )Nc                    s"   g | ]}t |j ˆ i ¡ ¡ ƒ‘qS © )ÚlistÚ__dict__ÚgetÚitems)Ú.0Úb©Únamer   ú:/usr/lib/python3/dist-packages/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 ƒƒ}|rt|ddƒ‚d S )Nc                 s   s   � | ]	\}}|s|V  qd S ©Nr   )r   Úis_okÚ
error_coder   r   r   Ú	<genexpr>   s   €
 ÿz*check_global_invariants.<locals>.<genexpr>c                 3   s   � | ]}|ˆ ƒV  qd S r!   r   )r   Ú	invariant©Úsubjectr   r   r$      s   € r   zGlobal invariant failed)Útupler   )r'   Ú
invariantsÚerror_codesr   r&   r   Úcheck_global_invariants   s   ÿÿr+   c                 C   s&   t |tƒr| tu r| |¡S | ||ƒS r!   )r   r   ÚPFIELD_NO_SERIALIZERÚ	serialize)Ú
serializerÚformatÚvaluer   r   r   r-   !   s   

r-   c                    sR   |j r%t‡ fdd„|j D ƒƒs't ˆ ƒ}d | j||j¡}t| ||j ||ƒ‚d S d S )Nc                 3   s   � | ]
}t ˆ t|ƒƒV  qd S r!   )r   r   ©r   Út©r0   r   r   r$   )   ó   € zcheck_type.<locals>.<genexpr>z'Invalid type for field {0}.{1}, was {2})ÚtypeÚanyr/   Ú__name__Ú
PTypeError)Údestination_clsÚfieldr   r0   Úactual_typeÚmessager   r3   r   Ú
check_type(   s
   ýr=   c                 C   s:   t |ƒtu rdS t|ƒ}t|ƒdkrdS tt|d ƒ| ƒS )NTr   F)r5   Úsetr(   ÚlenÚ
issubclassr   )Útype_clsÚ
field_typeÚtypesr   r   r   Úis_type_cls/   s   rD   c                 C   s*   |sdS t | |jƒsdS dt |j¡jv S )NFÚignore_extra)rD   r5   ÚinspectÚ	signatureÚfactoryÚ
parameters)rA   r:   rE   r   r   r   Úis_field_ignore_extra_complaint8   s
   rJ   c                   @   s$   e Zd ZdZdd„ Zedd„ ƒZdS )r   ©r5   r%   ÚinitialÚ	mandatoryÚ_factoryr.   c                 C   s(   || _ || _|| _|| _|| _|| _d S r!   rK   )Úselfr5   r%   rL   rM   rH   r.   r   r   r   Ú__init__G   s   
z_PField.__init__c                 C   s@   | j tu rt| jƒdkrtt| jƒd ƒ}t|tƒr|jS | j S )Né   r   )	rN   ÚPFIELD_NO_FACTORYr?   r5   r   r(   r@   r   Úcreate)rO   Útypr   r   r   rH   O   s
   
z_PField.factoryN)r7   Ú
__module__Ú__qualname__Ú	__slots__rP   ÚpropertyrH   r   r   r   r   r   D   s
    r   r   c                 C   ó   dS )N)TNr   )Ú_r   r   r   Ú<lambda>Z   ó    r[   c                 C   s   | S r!   r   )Úxr   r   r   r[   [   r\   c                 C   s   |S r!   r   )rZ   r0   r   r   r   r[   ]   r\   Fc           	      C   sf   t | tttfƒrtt| ƒƒ}ntt| ƒƒ}|tkr!t|ƒr!t|ƒ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
    )r5   r%   rL   rM   rH   r.   )r   r   r>   r(   r
   r	   ÚPFIELD_NO_INVARIANTÚcallabler   r   Ú_check_field_parameters)	r5   r%   rL   rM   rH   r.   rC   Úinvariant_functionr:   r   r   r   r:   `   s   ÿr:   c                    s²   ˆ j D ]}t|t ƒst|tƒstd t |ƒ¡ƒ‚qˆ jtur<tˆ jƒs<ˆ j r<t‡ fdd„ˆ j D ƒƒs<td t ˆ jƒ¡ƒ‚tˆ j	ƒsEtdƒ‚tˆ j
ƒsNtdƒ‚tˆ jƒsWtdƒ‚d S )Nz Type parameter expected, not {0}c                 3   s   � | ]	}t ˆ j|ƒV  qd S r!   )r   rL   r1   ©r:   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)r5   r   ÚstrÚ	TypeErrorr/   rL   ÚPFIELD_NO_INITIALr_   r6   r%   rH   r.   )r:   r2   r   rb   r   r`   „   s&   
€
ÿþþ


ÿr`   c                       s    e Zd ZdZ‡ fdd„Z‡  ZS )r8   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                    s2   t t| ƒj|i |¤Ž || _|| _|| _|| _d S r!   )Úsuperr8   rP   Úsource_classr:   Úexpected_typesr;   )rO   rg   r:   rh   r;   ÚargsÚkwargs©Ú	__class__r   r   rP   ¢   s
   
zPTypeError.__init__)r7   rU   rV   Ú__doc__rP   Ú__classcell__r   r   rk   r   r8   ˜   s    	r8   ÚPVectorÚPSetc                 C   ó   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²   ó   
rw   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 r!   )r   r7   Ú
capitalize)r   rT   r   r   r   r$   ¹   r4   z"_types_to_names.<locals>.<genexpr>)Újoin)rC   r   r   r   Ú_types_to_names·   s   r|   c                    sZ   t  ˆ ˆf¡}|du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”Z‡ ‡fdd„ZdS )z%_make_seq_field_type.<locals>.TheTypec                    s   t ˆ ˆt| ƒffS r!   )rw   r   ©rO   )rs   rt   r   r   Ú
__reduce__Å   s   ÿz0_make_seq_field_type.<locals>.TheType.__reduce__N)r7   rU   rV   Ú__type__Ú__invariant__r~   r   ©rs   Úitem_invariantrt   r   r   ÚTheTypeÁ   s    rƒ   )rr   r   ÚSEQ_FIELD_TYPE_SUFFIXESr|   Ú_checked_typesr7   )rs   rt   r‚   rv   rƒ   Úsuffixr   r�   r   Ú_make_seq_field_type»   s   r‡   c                    sH   t | ||ƒ‰ |rd‡ fdd„	}nˆ j}t|rtˆ ƒ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 u rd S ˆ j | ||d�S )N)Ú_factory_fieldsrE   ©rS   )Úargumentrˆ   rE   ©rƒ   r   r   rH   ß   s   z _sequence_field.<locals>.factoryT)r5   rH   rM   r%   rL   )NF)r‡   rS   r:   Úoptional_type)rs   rt   r   rL   r%   r‚   rH   r   r‹   r   Ú_sequence_fieldÎ   s   ýr�   c                 C   ó   t t| ||||d�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.
    ©r%   r‚   )r�   r   ©rt   r   rL   r%   r‚   r   r   r   Ú
pset_fieldí   ó   
þr‘   c                 C   rŽ   )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.
    r�   )r�   r   r�   r   r   r   Úpvector_field   r’   r“   c                 C   rY   )N)Try   r   )Úitemr   r   r   r[     r\   c                 C   rq   )z8Unpickling function for auto-generated PMap field types.)Ú_pmap_field_typesr   )Úkey_typeÚ
value_typeru   rv   r   r   r   Ú_restore_pmap_field_pickle  rx   r˜   c                    sZ   t  ˆ ˆf¡}|du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 r!   )r˜   Ú__key_type__Ú__value_type__r   r}   r   r   r   r~   (  s   ÿz0_make_pmap_field_type.<locals>.TheMap.__reduce__N)r7   rU   rV   r™   rš   r~   r   ©r–   r—   r   r   ÚTheMap$  s    rœ   z{0}To{1}PMap)r•   r   r   r/   r|   Ú_checked_key_typesÚ_checked_value_typesr7   )r–   r—   rv   rœ   r   r›   r   Ú_make_pmap_field_type  s   þrŸ   c                    sB   t | |ƒ‰ |r‡ fdd„}nˆ j}tdˆ ƒ |rtˆ ƒ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 u rd S ˆ   | ¡S r!   r‰   )rŠ   ©rœ   r   r   rH   B  s   
zpmap_field.<locals>.factoryT)rM   rL   r5   rH   r%   )rŸ   rS   r:   rŒ   )r–   r—   r   r%   rH   r   r    r   Ú
pmap_field3  s   
þr¡   ),Úpyrsistent._checked_typesr   r   r   r   r   r   r   r	   r
   r   rŒ   r   rF   r    r+   r-   r=   rD   rJ   Úobjectr   ÚPFIELD_NO_TYPEr^   rR   re   r,   r:   r`   rd   r8   r„   rr   rw   r|   r‡   r�   r‘   r“   Ú_validr•   r˜   rŸ   r¡   r   r   r   r   Ú<module>   sX   , 		
ÿ$þ
þ
þ
þ