o
    èÓáax  ã                   @   s„   d dl mZmZmZmZ d dlmZmZmZm	Z	m
Z
mZ d dlmZmZ G dd„ deƒZG dd„ deeed�ZG d	d
„ d
ejƒZdS )é    )ÚCheckedTypeÚ_restore_pickleÚInvariantExceptionÚstore_invariants)Ú
set_fieldsÚ
check_typeÚis_field_ignore_extra_complaintÚPFIELD_NO_INITIALÚ	serializeÚcheck_global_invariants)ÚPMapÚpmapc                       s   e Zd Z‡ fdd„Z‡  ZS )Ú_PRecordMetac                    sv   t ||dd� t||ddƒ tdd„ |d  ¡ D ƒƒ|d< tdd„ |d  ¡ D ƒƒ|d	< d
|d< tt| ƒ | |||¡S )NÚ_precord_fields)ÚnameÚ_precord_invariantsÚ__invariant__c                 s   s   � | ]
\}}|j r|V  qd S ©N)Ú	mandatory)Ú.0r   Úfield© r   ú5/usr/lib/python3/dist-packages/pyrsistent/_precord.pyÚ	<genexpr>   ó   € z'_PRecordMeta.__new__.<locals>.<genexpr>Ú_precord_mandatory_fieldsc                 s   s(   � | ]\}}|j tur||j fV  qd S r   )Úinitialr	   )r   Úkr   r   r   r   r      s   €& Ú_precord_initial_valuesr   Ú	__slots__)r   r   ÚsetÚitemsÚdictÚsuperr   Ú__new__)Úmcsr   ÚbasesÚdct©Ú	__class__r   r   r$   	   s   ÿÿz_PRecordMeta.__new__)Ú__name__Ú
__module__Ú__qualname__r$   Ú__classcell__r   r   r(   r   r      s    r   c                       s\   e Zd ZdZ‡ fdd„Z‡ fdd„Zdd„ Zdd	„ Zeddd„ƒZ	dd„ Z
ddd„Z‡  ZS )ÚPRecordau  
    A PRecord is a PMap with a fixed set of specified fields. Records are declared as python classes inheriting
    from PRecord. Because it is a PMap it has full support for all Mapping methods such as iteration and element
    access using subscript notation.

    More documentation and examples of PRecord usage is available at https://github.com/tobgu/pyrsistent
    c                    s®   d|v rd|v rt t| ƒ | |d |d ¡S | dd ¡}| dd¡}|}| jr8tdd„ | j ¡ D ƒƒ}| |¡ t| t	t
| jƒd�||d	�}| ¡ D ]\}}|||< qJ| ¡ S )
NÚ_precord_sizeÚ_precord_bucketsÚ_factory_fieldsÚ_ignore_extraFc                 s   s*   � | ]\}}|t |ƒr|ƒ n|fV  qd S r   )Úcallable©r   r   Úvr   r   r   r   -   s   € ÿz"PRecord.__new__.<locals>.<genexpr>)Úpre_size©r1   r2   )r#   r.   r$   Úpopr   r"   r!   ÚupdateÚ_PRecordEvolverr   Úlenr   Ú
persistent)ÚclsÚkwargsÚfactory_fieldsÚignore_extraÚinitial_valuesÚer   r5   r(   r   r   r$   !   s   ÿ

zPRecord.__new__c                    s(   |rt t| ƒ |d |d ¡S |  |¡S )a  
        Set a field in the record. This set function differs slightly from that in the PMap
        class. First of all it accepts key-value pairs. Second it accepts multiple key-value
        pairs to perform one, atomic, update of multiple fields.
        r   é   )r#   r.   r    r9   )ÚselfÚargsr>   r(   r   r   r    7   s   	
zPRecord.setc                 C   s   t | j| ƒS )z4
        Returns an evolver of this object.
        )r:   r)   ©rD   r   r   r   ÚevolverE   s   zPRecord.evolverc                 C   s$   d  | jjd dd„ |  ¡ D ƒ¡¡S )Nz{0}({1})z, c                 s   s$   � | ]\}}d   |t|ƒ¡V  qdS )z{0}={1}N)ÚformatÚreprr4   r   r   r   r   M   s   €" z#PRecord.__repr__.<locals>.<genexpr>)rH   r)   r*   Újoinr!   rF   r   r   r   Ú__repr__K   s   
ÿzPRecord.__repr__NFc                    s:   t ˆ | ƒrˆ S |r‡ fdd„| jD ƒ‰ | d||dœˆ ¤ŽS )a<  
        Factory method. Will create a new PRecord of the current type and assign the values
        specified in kwargs.

        :param ignore_extra: A boolean which when set to True will ignore any keys which appear in kwargs that are not
                             in the set of fields on the PRecord.
        c                    s   i | ]}|ˆ v r|ˆ | “qS r   r   )r   r   ©r>   r   r   Ú
<dictcomp>\   s    z"PRecord.create.<locals>.<dictcomp>r7   Nr   )Ú
isinstancer   )r=   r>   r1   r@   r   rL   r   ÚcreateO   s
   
	zPRecord.createc                 C   s   t | jt| ƒffS r   )r   r)   r"   rF   r   r   r   Ú
__reduce__`   s   zPRecord.__reduce__c                    s   t ‡ ‡fdd„ˆ ¡ D ƒƒS )zƒ
        Serialize the current PRecord using custom serializer functions for fields where
        such have been supplied.
        c                 3   s,   � | ]\}}|t ˆj| jˆ |ƒfV  qd S r   )r
   r   Ú
serializerr4   ©rH   rD   r   r   r   i   s   €* z$PRecord.serialize.<locals>.<genexpr>)r"   r!   )rD   rH   r   rR   r   r
   d   s   zPRecord.serialize©NFr   )r*   r+   r,   Ú__doc__r$   r    rG   rK   ÚclassmethodrO   rP   r
   r-   r   r   r(   r   r.      s    r.   )Ú	metaclassc                       sB   e Zd ZdZd‡ fdd„	Zdd„ Z‡ fdd	„Z‡ fd
d„Z‡  ZS )r:   )Ú_destination_clsÚ_invariant_error_codesÚ_missing_fieldsr1   r2   NFc                    s2   t t| ƒ |¡ || _g | _g | _|| _|| _d S r   )r#   r:   Ú__init__rW   rX   rY   r1   r2   )rD   r=   Úoriginal_pmapr1   r2   r(   r   r   rZ   o   s   
z_PRecordEvolver.__init__c                 C   s   |   ||¡ d S r   )r    )rD   ÚkeyÚoriginal_valuer   r   r   Ú__setitem__w   s   z_PRecordEvolver.__setitem__c              
      sö   | j j |¡}|rq| jd u s|| jv rOztt|| jƒr$|j|| jd�}n| |¡}W n& tyN } z|  j	|j
7  _	|  j|j7  _| W  Y d }~S d }~ww |}t| j |||ƒ | |¡\}}|sh| j	 |¡ tt| ƒ ||¡S td || j j¡ƒ‚)N)r@   z/'{0}' is not among the specified fields for {1})rW   r   Úgetr1   r   r.   r2   Úfactoryr   rX   Úinvariant_errorsrY   Úmissing_fieldsr   Ú	invariantÚappendr#   r:   r    ÚAttributeErrorrH   r*   )rD   r\   r]   r   ÚvaluerB   Úis_okÚ
error_coder(   r   r   r    z   s*   
€€ýz_PRecordEvolver.setc                    s¨   | j ‰ |  ¡ }tt| ƒ ¡ }|st|ˆ ƒsˆ |j|jd�}n|}ˆ jr:|  j	t
‡ fdd„ˆ jt| ¡ ƒ D ƒƒ7  _	| js@| j	rLtt
| jƒt
| j	ƒdƒ‚t|ˆ jƒ |S )N)r0   r/   c                 3   s   � | ]
}d   ˆ j|¡V  qdS )z{0}.{1}N)rH   r*   )r   Úf©r=   r   r   r   ž   r   z-_PRecordEvolver.persistent.<locals>.<genexpr>zField invariant failed)rW   Úis_dirtyr#   r:   r<   rN   Ú_bucketsÚ_sizer   rY   Útupler    ÚkeysrX   r   r   r   )rD   rk   ÚpmÚresultr(   rj   r   r<   ”   s    ÿÿz_PRecordEvolver.persistentrS   )	r*   r+   r,   r   rZ   r^   r    r<   r-   r   r   r(   r   r:   l   s    r:   N)Úpyrsistent._checked_typesr   r   r   r   Úpyrsistent._field_commonr   r   r   r	   r
   r   Úpyrsistent._pmapr   r   Útyper   r.   Ú_Evolverr:   r   r   r   r   Ú<module>   s     S