U
    gØ\ú%  ã                   @   s    d dl Z 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 dd„ ZG dd„ deƒZeƒ Zd	d
„ Ze  e¡G dd„ deƒƒZG dd„ deƒZdS )é    N)ÚInvariantExceptionÚCheckedTypeÚ_restore_pickleÚstore_invariants)Ú
set_fieldsÚ
check_typeÚis_field_ignore_extra_complaintÚPFIELD_NO_INITIALÚ	serializeÚcheck_global_invariants©Ú	transformc                 C   s   t | ƒdko| d tkS )Né   r   )Úlenr   )Úbases© r   ú4/usr/lib/python3/dist-packages/pyrsistent/_pclass.pyÚ
_is_pclass	   s    r   c                       s   e Zd Z‡ fdd„Z‡  ZS )Ú
PClassMetac                    sh   t ||dd� t||ddƒ dtdd„ |d D ƒƒ |d< t|ƒrR|d  d	7  < tt| ƒ | |||¡S )
NÚ_pclass_fields)ÚnameÚ_pclass_invariantsZ__invariant__)Ú_pclass_frozenc                 s   s   | ]
}|V  qd S ©Nr   ©Ú.0Úkeyr   r   r   Ú	<genexpr>   s     z%PClassMeta.__new__.<locals>.<genexpr>Ú	__slots__)Ú__weakref__)r   r   Útupler   Úsuperr   Ú__new__)Zmcsr   r   Zdct©Ú	__class__r   r   r"      s    zPClassMeta.__new__)Ú__name__Ú
__module__Ú__qualname__r"   Ú__classcell__r   r   r#   r   r      s   r   c                 C   s<   t | |||ƒ | |¡\}}|s,| |¡ nt|||ƒ d S r   )r   Z	invariantÚappendÚsetattr)ÚclsÚfieldr   ÚvalueÚresultÚinvariant_errorsZis_okZ
error_coder   r   r   Ú_check_and_set_attr   s
    r0   c                       sœ   e Zd ZdZ‡ fdd„Zdd„ Zed"dd	„ƒZd#d
d„Zdd„ Z	dd„ Z
dd„ Zdd„ Z‡ fdd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Z‡  ZS )$ÚPClassa­  
    A PClass is a python class with a fixed set of specified fields. PClasses are declared as python classes inheriting
    from PClass. It is defined the same way that PRecords are and behaves like a PRecord in all aspects except that it
    is not a PMap and hence not a collection but rather a plain Python object.


    More documentation and examples of PClass usage is available at https://github.com/tobgu/pyrsistent
    c                    sR  t t| ƒ | ¡}| dd ¡}| dd ¡}g }g }| j ¡ D ]Â\}}||kr¬|d ksZ||krŠtt||ƒrz|j|| |d�}	q’| || ¡}	n|| }	t| |||	||ƒ ||= q:|j	t
k	rât|j	ƒrÈ| 	¡ n|j	}
t| |||
||ƒ q:|jr:| d | j|¡¡ q:|�s
|�rtt|ƒt|ƒdƒ‚|�r<td d |¡| j¡ƒ‚t|| jƒ d|_|S )	NÚ_factory_fieldsÚignore_extra)r3   z{0}.{1}zField invariant failedz0'{0}' are not among the specified fields for {1}ú, T)r!   r1   r"   Úpopr   Úitemsr   Úfactoryr0   Úinitialr	   ÚcallableZ	mandatoryr)   Úformatr%   r   r    ÚAttributeErrorÚjoinr   r   r   )r+   Úkwargsr.   Úfactory_fieldsr3   Zmissing_fieldsr/   r   r,   r-   r8   r#   r   r   r"   0   sH    
     ÿ ÿzPClass.__new__c                 O   s`   |r|d ||d < t |ƒ}| jD ](}||kr"t| |tƒ}|tk	r"|||< q"| jf d|i|—ŽS )a  
        Set a field in the instance. Returns a new instance with the updated value. The original instance remains
        unmodified. Accepts key-value pairs or single string representing the field name and a value.

        >>> from pyrsistent import PClass, field
        >>> class AClass(PClass):
        ...     x = field()
        ...
        >>> a = AClass(x=1)
        >>> a2 = a.set(x=2)
        >>> a3 = a.set('x', 3)
        >>> a
        AClass(x=1)
        >>> a2
        AClass(x=2)
        >>> a3
        AClass(x=3)
        r   r   r2   )Úsetr   ÚgetattrÚ_MISSING_VALUEr$   )ÚselfÚargsr=   r>   r   r-   r   r   r   r?   T   s    

z
PClass.setNFc                    s:   t ˆ | ƒrˆ S |r&‡ fdd„| jD ƒ‰ | f ||dœˆ —ŽS )a:  
        Factory method. Will create a new PClass 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 PClass.
        c                    s   i | ]}|ˆ kr|ˆ | “qS r   r   )r   Úk©r=   r   r   Ú
<dictcomp>�   s       z!PClass.create.<locals>.<dictcomp>)r2   r3   )Ú
isinstancer   )r+   r=   r2   r3   r   rE   r   Úcreatet   s
    	
zPClass.createc                 C   s@   i }| j D ]0}t| |tƒ}|tk	r
t| j | j||ƒ||< q
|S )z‚
        Serialize the current PClass using custom serializer functions for fields where
        such have been supplied.
        )r   r@   rA   r
   Z
serializer)rB   r:   r.   r   r-   r   r   r   r
   …   s    
zPClass.serializec                 G   s
   t | |ƒS )aT  
        Apply transformations to the currency PClass. For more details on transformations see
        the documentation for PMap. Transformations on PClasses do not support key matching
        since the PClass is not a collection. Apart from that the transformations available
        for other persistent types work as expected.
        r   )rB   Ztransformationsr   r   r   r   ’   s    zPClass.transformc                 C   s>   t || jƒr:| jD ]"}t| |tƒt||tƒkr dS qdS tS )NFT)rG   r$   r   r@   rA   ÚNotImplemented)rB   Úotherr   r   r   r   Ú__eq__›   s    
zPClass.__eq__c                 C   s
   | |k S r   r   )rB   rJ   r   r   r   Ú__ne__¥   s    zPClass.__ne__c                    s   t t‡ fdd„ˆ jD ƒƒƒS )Nc                 3   s   | ]}|t ˆ |tƒfV  qd S r   )r@   rA   r   ©rB   r   r   r   ª   s     z"PClass.__hash__.<locals>.<genexpr>)Úhashr    r   rM   r   rM   r   Ú__hash__¨   s    zPClass.__hash__c                    s2   t | ddƒrtd ||¡ƒ‚tt| ƒ ||¡ d S )Nr   Fz'Can't set attribute, key={0}, value={1})r@   r;   r:   r!   r1   Ú__setattr__©rB   r   r-   r#   r   r   rP   ¬   s    zPClass.__setattr__c                 C   s   t d |¡ƒ‚d S )Nz-Can't delete attribute, key={0}, use remove())r;   r:   )rB   r   r   r   r   Ú__delattr__²   s    zPClass.__delattr__c                 C   s0   i }| j D ] }t| |tƒ}|tk	r
|||< q
|S r   )r   r@   rA   )rB   r.   r   r-   r   r   r   Ú_to_dictµ   s    

zPClass._to_dictc                 C   s(   d  | jjd dd„ |  ¡  ¡ D ƒ¡¡S )Nz{0}({1})r4   c                 s   s"   | ]\}}d   |t|ƒ¡V  qdS )z{0}={1}N)r:   Úrepr)r   rD   Úvr   r   r   r   À   s     z"PClass.__repr__.<locals>.<genexpr>)r:   r$   r%   r<   rS   r6   rM   r   r   r   Ú__repr__¾   s    
ÿzPClass.__repr__c                    s&   t ‡ fdd„ˆ jD ƒƒ}tˆ j|ffS )Nc                 3   s&   | ]}t ˆ |ƒr|tˆ |ƒfV  qd S r   )Úhasattrr@   r   rM   r   r   r   Ä   s     
 z$PClass.__reduce__.<locals>.<genexpr>)Údictr   r   r$   )rB   Údatar   rM   r   Ú
__reduce__Â   s    zPClass.__reduce__c                 C   s   t | |  ¡ ƒS )z5
        Returns an evolver for this object.
        )Ú_PClassEvolverrS   rM   r   r   r   ÚevolverÇ   s    zPClass.evolverc                 C   s   |   ¡ }||= | ¡ S )z‰
        Remove attribute given by name from the current instance. Raises AttributeError if the
        attribute doesn't exist.
        )r\   Ú
persistent)rB   r   r\   r   r   r   ÚremoveÍ   s    zPClass.remove)NF)N)r%   r&   r'   Ú__doc__r"   r?   ÚclassmethodrH   r
   r   rK   rL   rO   rP   rR   rS   rV   rZ   r\   r^   r(   r   r   r#   r   r1   &   s"   $ 
	
	r1   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	dd„ Z
‡ fdd„Zdd„ Z‡  ZS )r[   )Ú_pclass_evolver_originalÚ_pclass_evolver_dataÚ_pclass_evolver_data_is_dirtyr2   c                 C   s   || _ || _d| _tƒ | _d S )NF)ra   rb   rc   r?   r2   )rB   ZoriginalZinitial_dictr   r   r   Ú__init__Ú   s    z_PClassEvolver.__init__c                 C   s
   | j | S r   )rb   ©rB   Úitemr   r   r   Ú__getitem__à   s    z_PClassEvolver.__getitem__c                 C   s2   | j  |t¡|k	r.|| j |< | j |¡ d| _| S ©NT)rb   ÚgetrA   r2   Úaddrc   rQ   r   r   r   r?   ã   s
    
z_PClassEvolver.setc                 C   s   |   ||¡ d S r   )r?   rQ   r   r   r   Ú__setitem__ë   s    z_PClassEvolver.__setitem__c                 C   s4   || j kr(| j |= | j |¡ d| _| S t|ƒ‚d S rh   )rb   r2   Údiscardrc   r;   re   r   r   r   r^   î   s    
z_PClassEvolver.removec                 C   s   |   |¡ d S r   )r^   re   r   r   r   Ú__delitem__÷   s    z_PClassEvolver.__delitem__c                 C   s&   | j r | jjf d| ji| j—ŽS | jS )Nr2   )rc   ra   r$   r2   rb   rM   r   r   r   r]   ú   s
    ÿz_PClassEvolver.persistentc                    s.   || j kr|  ||¡ ntt| ƒ ||¡ d S r   )r   r?   r!   r[   rP   rQ   r#   r   r   rP     s    
z_PClassEvolver.__setattr__c                 C   s   | | S r   r   re   r   r   r   Ú__getattr__  s    z_PClassEvolver.__getattr__)r%   r&   r'   r   rd   rg   r?   rk   r^   rm   r]   rP   rn   r(   r   r   r#   r   r[   ×   s   	r[   )ZsixZpyrsistent._checked_typesr   r   r   r   Zpyrsistent._field_commonr   r   r   r	   r
   r   Zpyrsistent._transformationsr   r   Útyper   ÚobjectrA   r0   Zadd_metaclassr1   r[   r   r   r   r   Ú<module>   s    	 1