B
    ÔùÓ`æ%  ã               @   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 dd„ ZG dd„ deƒZeƒ Zdd	„ ZG d
d„ deed�ZG dd„ deƒZdS )é    )Ú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   ú6/tmp/pip-unpacked-wheel-by61ilhc/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   Ú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 )N)r   Z	invariantÚappendÚsetattr)ÚclsÚfieldr   ÚvalueÚresultÚinvariant_errorsZis_okZ
error_coder   r   r   Ú_check_and_set_attr   s
    r-   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                sV  t t| ƒ | ¡}| dd ¡}| dd ¡}g }g }xÐ| j ¡ D ]Â\}}||kr®|d ks\||krŒtt||ƒr||j|| |d�}	q”| || ¡}	n|| }	t| |||	||ƒ ||= q<|j	t
k	rät|j	ƒrÊ| 	¡ n|j	}
t| |||
||ƒ q<|jr<| d | j|¡¡ q<W |�s|�r"tt|ƒt|ƒdƒ‚|�r@td d |¡| j¡ƒ‚t|| jƒ d|_|S )	NÚ_factory_fieldsÚignore_extra)r0   z{0}.{1}zField invariant failedz0'{0}' are not among the specified fields for {1}z, T)r   r.   r   Úpopr   Úitemsr   Úfactoryr-   Úinitialr	   ÚcallableÚ	mandatoryr&   Úformatr"   r   r   ÚAttributeErrorÚjoinr   r   r   )r(   Úkwargsr+   Úfactory_fieldsr0   Zmissing_fieldsr,   r   r)   r*   r4   )r!   r   r   r   .   s8    
zPClass.__new__c             O   sd   |r|d ||d < t |ƒ}x2| jD ](}||kr$t| |tƒ}|tk	r$|||< q$W | 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   r/   )Úsetr   ÚgetattrÚ_MISSING_VALUEr!   )ÚselfÚargsr:   r;   r   r*   r   r   r   r<   R   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>)r/   r0   )Ú
isinstancer   )r(   r:   r/   r0   r   )r:   r   Úcreater   s
    	
zPClass.createc             C   sD   i }x:| j D ]0}t| |tƒ}|tk	rt| j | j||ƒ||< qW |S )z‚
        Serialize the current PClass using custom serializer functions for fields where
        such have been supplied.
        )r   r=   r>   r
   Ú
serializer)r?   r7   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   )r?   Ztransformationsr   r   r   r   �   s    zPClass.transformc             C   s@   t || jƒr<x*| jD ] }t| |tƒt||tƒkrdS qW dS tS )NFT)rC   r!   r   r=   r>   ÚNotImplemented)r?   Úotherr   r   r   r   Ú__eq__™   s    zPClass.__eq__c             C   s
   | |k S )Nr   )r?   rG   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 )N)r=   r>   )r   r   )r?   r   r   r   ¨   s    z"PClass.__hash__.<locals>.<genexpr>)Úhashr   r   )r?   r   )r?   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=   r8   r7   r   r.   Ú__setattr__)r?   r   r*   )r!   r   r   rL   ª   s    zPClass.__setattr__c             C   s   t d |¡ƒ‚d S )Nz-Can't delete attribute, key={0}, use remove())r8   r7   )r?   r   r   r   r   Ú__delattr__°   s    zPClass.__delattr__c             C   s4   i }x*| j D ] }t| |tƒ}|tk	r|||< qW |S )N)r   r=   r>   )r?   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})z, c             s   s"   | ]\}}d   |t|ƒ¡V  qdS )z{0}={1}N)r7   Úrepr)r   rA   Úvr   r   r   r   ¾   s    z"PClass.__repr__.<locals>.<genexpr>)r7   r!   r"   r9   rN   r2   )r?   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 )N)Úhasattrr=   )r   r   )r?   r   r   r   Â   s    z$PClass.__reduce__.<locals>.<genexpr>)Údictr   r   r!   )r?   Údatar   )r?   r   Ú
__reduce__À   s    zPClass.__reduce__c             C   s   t | |  ¡ ƒS )z5
        Returns an evolver for this object.
        )Ú_PClassEvolverrN   )r?   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.
        )rW   Ú
persistent)r?   r   rW   r   r   r   ÚremoveË   s    zPClass.remove)NF)N)r"   r#   r$   Ú__doc__r   r<   ÚclassmethodrD   r
   r   rH   rI   rK   rL   rM   rN   rQ   rU   rW   rY   r%   r   r   )r!   r   r.   %   s"   $ 
	
	r.   )Ú	metaclassc                   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 )rV   )Ú_pclass_evolver_originalÚ_pclass_evolver_dataÚ_pclass_evolver_data_is_dirtyr/   c             C   s   || _ || _d| _tƒ | _d S )NF)r]   r^   r_   r<   r/   )r?   ÚoriginalZinitial_dictr   r   r   Ú__init__Ø   s    z_PClassEvolver.__init__c             C   s
   | j | S )N)r^   )r?   Úitemr   r   r   Ú__getitem__Þ   s    z_PClassEvolver.__getitem__c             C   s2   | j  |t¡|k	r.|| j |< | j |¡ d| _| S )NT)r^   Úgetr>   r/   Úaddr_   )r?   r   r*   r   r   r   r<   á   s
    
z_PClassEvolver.setc             C   s   |   ||¡ d S )N)r<   )r?   r   r*   r   r   r   Ú__setitem__é   s    z_PClassEvolver.__setitem__c             C   s4   || j kr(| j |= | j |¡ d| _| S t|ƒ‚d S )NT)r^   r/   Údiscardr_   r8   )r?   rb   r   r   r   rY   ì   s    
z_PClassEvolver.removec             C   s   |   |¡ d S )N)rY   )r?   rb   r   r   r   Ú__delitem__õ   s    z_PClassEvolver.__delitem__c             C   s&   | j r | jjf d| ji| j—ŽS | jS )Nr/   )r_   r]   r!   r/   r^   )r?   r   r   r   rX   ø   s    
z_PClassEvolver.persistentc                s.   || j kr|  ||¡ ntt| ƒ ||¡ d S )N)r   r<   r   rV   rL   )r?   r   r*   )r!   r   r   rL   ÿ   s    
z_PClassEvolver.__setattr__c             C   s   | | S )Nr   )r?   rb   r   r   r   Ú__getattr__  s    z_PClassEvolver.__getattr__)r"   r#   r$   r   ra   rc   r<   rf   rY   rh   rX   rL   ri   r%   r   r   )r!   r   rV   Õ   s   	rV   N)Zpyrsistent._checked_typesr   r   r   r   Zpyrsistent._field_commonr   r   r   r	   r
   r   Zpyrsistent._transformationsr   r   Útyper   Úobjectr>   r-   r.   rV   r   r   r   r   Ú<module>   s    	 1