o
    a%                     @   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   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|r)|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|| d S 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                    sL  t t| | }|dd }|dd }g }g }| j D ]a\}}||v rV|d u s-||v rEtt||r=|j|| |d}	n||| }	n|| }	t| |||	|| ||= q|j	t
urqt|j	rd|	 n|j	}
t| |||
|| q|jr~|d| j| q|s|rtt|t|d|rt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"   .   s>   
zPClass.__new__c                 O   s`   |r
|d ||d < t |}| jD ]}||vr%t| |t}|tur%|||< q| jd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   Nr   )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  | d||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 | ]}| v r| | qS r   r   )r   kr=   r   r   
<dictcomp>   s    z!PClass.create.<locals>.<dictcomp>)r2   r3   Nr   )
isinstancer   )r+   r=   r2   r3   r   rE   r   creater   s
   
	zPClass.createc                 C   s@   i }| j D ]}t| |t}|tu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__      
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   rN   r   rN   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   rQ      s   zPClass.__setattr__c                 C   s   t d|)Nz-Can't delete attribute, key={0}, use remove())r;   r:   )rB   r   r   r   r   __delattr__      zPClass.__delattr__c                 C   s0   i }| j D ]}t| |t}|tu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<   rU   r6   rN   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   rN   r   r   r      s   & z$PClass.__reduce__.<locals>.<genexpr>)dictr   r   r$   )rB   datar   rN   r   
__reduce__   s   zPClass.__reduce__c                 C   s   t | |  S )z5
        Returns an evolver for this object.
        )_PClassEvolverrU   rN   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NFr   )r%   r&   r'   __doc__r"   r?   classmethodrH   r
   r   rK   rL   rP   rQ   rS   rU   rX   r\   r^   r`   r(   r   r   r#   r   r1   %   s$    $ 
	
	r1   )	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 )r]   )_pclass_evolver_original_pclass_evolver_data_pclass_evolver_data_is_dirtyr2   c                 C   s   || _ || _d| _t | _d S ra   )re   rf   rg   r?   r2   )rB   ZoriginalZinitial_dictr   r   r   __init__   s   z_PClassEvolver.__init__c                 C   s
   | j | S r   )rf   rB   itemr   r   r   __getitem__   rM   z_PClassEvolver.__getitem__c                 C   s2   | j |t|ur|| j |< | j| d| _| S NT)rf   getrA   r2   addrg   rR   r   r   r   r?      s
   
z_PClassEvolver.setc                 C   s   |  || d S r   )r?   rR   r   r   r   __setitem__   s   z_PClassEvolver.__setitem__c                 C   s0   || j v r| j |= | j| d| _| S t|rl   )rf   r2   discardrg   r;   ri   r   r   r   r`      s   
z_PClassEvolver.removec                 C   s   |  | d S r   )r`   ri   r   r   r   __delitem__   rT   z_PClassEvolver.__delitem__c                 C   s&   | j r| jjdd| ji| jS | jS )Nr2   r   )rg   re   r$   r2   rf   rN   r   r   r   r_      s
   z_PClassEvolver.persistentc                    s0   || j vr| || d S tt| || d S r   )r   r?   r!   r]   rQ   rR   r#   r   r   rQ      s   
z_PClassEvolver.__setattr__c                 C   s   | | S r   r   ri   r   r   r   __getattr__  s   z_PClassEvolver.__getattr__)r%   r&   r'   r   rh   rk   r?   ro   r`   rq   r_   rQ   rr   r(   r   r   r#   r   r]      s    	r]   N)Zpyrsistent._checked_typesr   r   r   r   Zpyrsistent._field_commonr   r   r   r	   r
   r   Zpyrsistent._transformationsr   r   typer   objectrA   r0   r1   r]   r   r   r   r   <module>   s     	 1