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_invariants__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__)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|| d S t||| d S r   )r   	invariantappendsetattr)clsfieldr   valueresultinvariant_errorsis_ok
error_coder   r   r   _check_and_set_attr   s
   r6   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)r9   z{0}.{1}zField invariant failedz0'{0}' are not among the specified fields for {1}, T)r"   r7   r#   popr   itemsr   factoryr6   initialr	   callable	mandatoryr-   formatr(   r   r!   AttributeErrorjoinr   r   r   )r/   kwargsr2   factory_fieldsr9   missing_fieldsr3   r   r0   r1   r>   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   r8   Nr   )setr   getattr_MISSING_VALUEr'   )selfargsrD   rE   r   r1   r   r   r   rG   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rD   r   r   
<dictcomp>   s    z!PClass.create.<locals>.<dictcomp>)r8   r9   Nr   )
isinstancer   )r/   rD   r8   r9   r   rM   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   rH   rI   r
   
serializer)rJ   rA   r2   r   r1   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   )rJ   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)rO   r'   r   rH   rI   NotImplemented)rJ   otherr   r   r   r   __eq__   s   
zPClass.__eq__c                 C   s
   | |k S r   r   )rJ   rT   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   )rH   rI   r   rJ   r   r   r      s    z"PClass.__hash__.<locals>.<genexpr>)hashr!   r   rX   r   rX   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})rH   rB   rA   r"   r7   __setattr__rJ   r   r1   r&   r   r   r[      s   zPClass.__setattr__c                 C   s   t d|)Nz-Can't delete attribute, key={0}, use remove())rB   rA   )rJ   r   r   r   r   __delattr__      zPClass.__delattr__c                 C   s0   i }| j D ]}t| |t}|tur|||< q|S r   )r   rH   rI   )rJ   r2   r   r1   r   r   r   _to_dict   s   
zPClass._to_dictc                 C   s(   d | jjddd |   D S )Nz{0}({1})r:   c                 s   s$    | ]\}}d  |t|V  qdS )z{0}={1}N)rA   repr)r   rL   vr   r   r   r      s   " z"PClass.__repr__.<locals>.<genexpr>)rA   r'   r(   rC   r_   r<   rX   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   )hasattrrH   r   rX   r   r   r      s   & z$PClass.__reduce__.<locals>.<genexpr>)dictr   r   r'   )rJ   datar   rX   r   
__reduce__   s   zPClass.__reduce__c                 C   s   t | |  S )z5
        Returns an evolver for this object.
        )_PClassEvolverr_   rX   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.
        )rh   
persistent)rJ   r   rh   r   r   r   remove   s   zPClass.removeNFr   )r(   r)   r*   __doc__r#   rG   classmethodrP   r
   r   rU   rV   rZ   r[   r]   r_   rb   rf   rh   rj   r+   r   r   r&   r   r7   %   s$    $ 
	
	r7   )	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 )rg   )_pclass_evolver_original_pclass_evolver_data_pclass_evolver_data_is_dirtyr8   c                 C   s   || _ || _d| _t | _d S rk   )ro   rp   rq   rG   r8   )rJ   originalinitial_dictr   r   r   __init__   s   z_PClassEvolver.__init__c                 C   s
   | j | S r   )rp   rJ   itemr   r   r   __getitem__   rW   z_PClassEvolver.__getitem__c                 C   s2   | j |t|ur|| j |< | j| d| _| S NT)rp   getrI   r8   addrq   r\   r   r   r   rG      s
   
z_PClassEvolver.setc                 C   s   |  || d S r   )rG   r\   r   r   r   __setitem__   s   z_PClassEvolver.__setitem__c                 C   s0   || j v r| j |= | j| d| _| S t|rx   )rp   r8   discardrq   rB   ru   r   r   r   rj      s   
z_PClassEvolver.removec                 C   s   |  | d S r   )rj   ru   r   r   r   __delitem__   r^   z_PClassEvolver.__delitem__c                 C   s&   | j r| jjdd| ji| jS | jS )Nr8   r   )rq   ro   r'   r8   rp   rX   r   r   r   ri      s
   z_PClassEvolver.persistentc                    s0   || j vr| || d S tt| || d S r   )r   rG   r"   rg   r[   r\   r&   r   r   r[      s   
z_PClassEvolver.__setattr__c                 C   s   | | S r   r   ru   r   r   r   __getattr__  s   z_PClassEvolver.__getattr__)r(   r)   r*   r   rt   rw   rG   r{   rj   r}   ri   r[   r~   r+   r   r   r&   r   rg      s    	rg   N)pyrsistent._checked_typesr   r   r   r   pyrsistent._field_commonr   r   r   r	   r
   r   pyrsistent._transformationsr   r   typer   objectrI   r6   r7   rg   r   r   r   r   <module>   s     	 1