a
    ¦ˆc‚A  ã                   @   s†   d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ G dd„ deƒZejG d	d
„ d
eƒƒZG dd„ deƒZdS )é    N)Úchain)Úref)ÚIObservable)Ú_validate_everything)Ú
TraitErrorc                   @   s(   e Zd ZdZdddœdd„Zdd„ ZdS )ÚTraitSetEventa‚   An object reporting in-place changes to a traits sets.

    Parameters
    ----------
    removed : set, optional
        Old values that were removed from the set.
    added : set, optional
        New values added to the set.

    Attributes
    ----------
    removed : set
        Old values that were removed from the set.
    added : set
        New values added to the set.
    N©ÚremovedÚaddedc                C   s,   |d u rt ƒ }|| _|d u r"t ƒ }|| _d S ©N)Úsetr	   r
   )Úselfr	   r
   © r   úZ/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traits/trait_set_object.pyÚ__init__'   s    zTraitSetEvent.__init__c                 C   s   | j j› d| j›d| j›d�S )Nz	(removed=z, added=ú))Ú	__class__Ú__name__r	   r
   ©r   r   r   r   Ú__repr__1   s    
ÿþÿzTraitSetEvent.__repr__)r   Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r      s   
r   c                       sú   e Zd ZdZ‡ fdd„Zd-dddœ‡ fdd„Zd	d
„ Z‡ fdd„Z‡ fdd„Z‡ fdd„Z	‡ fdd„Z
‡ fdd„Z‡ fdd„Z‡ fdd„Z‡ fdd„Z‡ fdd„Z‡ fdd„Z‡ fdd „Z‡ fd!d"„Z‡ fd#d$„Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Z‡  ZS ).ÚTraitSetax   A subclass of set that validates and notifies listeners of changes.

    Parameters
    ----------
    value : iterable, optional
        Iterable providing the items for the set.
    item_validator : callable, optional
        Called to validate and/or transform items added to the set. The
        callable should accept a single item and return the transformed
        item, raising TraitError for invalid items. If not given, no
        item validation is performed.
    notifiers : list of callable, optional
        A list of callables with the signature::

            notifier(trait_set, removed, added)

        Where 'added' is a set containing new values that have been added.
        And 'removed' is a set containing old values that have been removed.

        If this argument is not given, the list of notifiers is initially
        empty.

    Attributes
    ----------
    item_validator : callable
        Called to validate and/or transform items added to the set. The
        callable should accept a single item and return the transformed
        item, raising TraitError for invalid items.
    notifiers : list of callable
        A list of callables with the signature::

            notifier(trait_set, removed, added)

        where 'added' is a set containing new values that have been added
        and 'removed' is a set containing old values that have been removed.
    c                    s   t ƒ  | ¡}t|_g |_|S r   )ÚsuperÚ__new__r   Úitem_validatorÚ	notifiers)ÚclsÚargsÚkwargsr   ©r   r   r   r   `   s    zTraitSet.__new__r   N©r   r   c                   s:   |d ur|ˆ _ tƒ  ‡ fdd„|D ƒ¡ |d ur6|ˆ _d S )Nc                 3   s   | ]}ˆ   |¡V  qd S r   ©r   ©Ú.0Úitemr   r   r   Ú	<genexpr>i   ó    z$TraitSet.__init__.<locals>.<genexpr>)r   r   r   r   )r   Úvaluer   r   r!   r   r   r   f   s
    zTraitSet.__init__c                 C   s   | j D ]}|| ||ƒ qdS )a9   Call all notifiers.

        This simply calls all notifiers provided by the class, if any.
        The notifiers are expected to have the signature::

            notifier(trait_set, removed, added)

        Any return values are ignored. Any exceptions raised are not
        handled. Notifiers are therefore expected not to raise any
        exceptions under normal use.

        Parameters
        ----------
        removed : set
            The items that have been removed.
        added : set
            The new items that have been added to the set.
        N©r   )r   r	   r
   Únotifierr   r   r   Únotifym   s    
zTraitSet.notifyc                    s<   |   ¡ }tƒ  |¡}| | ¡}t|ƒdkr8|  |tƒ ¡ |S )zÓ  Return self &= value.

        Parameters
        ----------
        value : set or frozenset
            A value.

        Returns
        -------
        self : TraitSet
            The updated set.
        r   )Úcopyr   Ú__iand__Ú
differenceÚlenr,   r   ©r   r)   Úold_setÚretvalr	   r!   r   r   r.   …   s    
zTraitSet.__iand__c                    s\   ˆ   ¡ }t|ttfƒr(‡ fdd„|D ƒ}tƒ  |¡}ˆ  |¡}t|ƒdkrXˆ  tƒ |¡ |S )zÒ Return self |= value.

        Parameters
        ----------
        value : set or frozenset
            A value.

        Returns
        -------
        self : TraitSet
            The updated set.
        c                    s   h | ]}ˆ   |¡’qS r   r#   r$   r   r   r   Ú	<setcomp>°   s   ÿz#TraitSet.__ior__.<locals>.<setcomp>r   )	r-   Ú
isinstancer   Ú	frozensetr   Ú__ior__r/   r0   r,   )r   r)   r2   r3   r
   r!   r   r   r7   œ   s    
ÿ
zTraitSet.__ior__c                    s<   |   ¡ }tƒ  |¡}| | ¡}t|ƒdkr8|  |tƒ ¡ |S )zÐ Return self-=value.

        Parameters
        ----------
        value : set or frozenset
            A value.

        Returns
        -------
        self : TraitSet
            The updated set.
        r   )r-   r   Ú__isub__r/   r0   r,   r   r1   r!   r   r   r8   ¼   s    
zTraitSet.__isub__c                    s~   t ƒ }t ƒ }t|t tfƒrZt |ƒ}ˆ  |¡}| |¡}‡ fdd„|D ƒ}| ˆ ¡}||B }tƒ  |¡}|sn|rzˆ  ||¡ |S )zÒ Return self ^= value.

        Parameters
        ----------
        value : set or frozenset
            A value.

        Returns
        -------
        self : TraitSet
            The updated set.
        c                    s   h | ]}ˆ   |¡’qS r   r#   r$   r   r   r   r4   ì   r(   z$TraitSet.__ixor__.<locals>.<setcomp>)r   r5   r6   Úintersectionr/   r   Ú__ixor__r,   )r   r)   r	   r
   ÚvaluesZ	raw_addedZvalidated_addedr3   r!   r   r   r:   Ó   s    


ÿ
zTraitSet.__ixor__c                    s6   |   |¡}|| v }tƒ  |¡ |s2|  tƒ |h¡ dS )zÅ Add an element to a set.

        This has no effect if the element is already present.

        Parameters
        ----------
        value : any
            The value to add to the set.
        N)r   r   Úaddr,   r   ©r   r)   Zvalue_in_selfr!   r   r   r<   ø   s
    
zTraitSet.addc                    s(   t | ƒ}tƒ  ¡  |r$|  |t ƒ ¡ dS )z$ Remove all elements from this set. N)r   r   Úclearr,   ©r   r	   r!   r   r   r>   	  s    
zTraitSet.clearc                    s,   || v }t ƒ  |¡ |r(|  |htƒ ¡ dS )zÊ Remove an element from the set if it is a member.

        If the element is not a member, do nothing.

        Parameters
        ----------
        value : any
            An item in the set
        N)r   Údiscardr,   r   r=   r!   r   r   r@     s    zTraitSet.discardc                    s<   |   ¡ }tƒ j|Ž  | | ¡}t|ƒdkr8|  |tƒ ¡ dS )z�  Remove all elements of another set from this set.

        Parameters
        ----------
        args : iterables
            The other iterables.
        r   N)r-   r   Údifference_updater/   r0   r,   r   ©r   r   r2   r	   r!   r   r   rA   "  s
    	
zTraitSet.difference_updatec                    s<   |   ¡ }tƒ j|Ž  | | ¡}t|ƒdkr8|  |tƒ ¡ dS )z«  Update the set with the intersection of itself and another set.

        Parameters
        ----------
        args : iterables
            The other iterables.
        r   N)r-   r   Úintersection_updater/   r0   r,   r   rB   r!   r   r   rC   2  s
    	
zTraitSet.intersection_updatec                    s   t ƒ  ¡ }|  |htƒ ¡ |S )a   Remove and return an arbitrary set element.

        Raises KeyError if the set is empty.

        Returns
        -------
        item : any
            An element from the set.

        Raises
        ------
        KeyError
            If the set is empty.
        )r   Úpopr,   r   r?   r!   r   r   rD   B  s    
zTraitSet.popc                    s    t ƒ  |¡ |  |htƒ ¡ dS )a2   Remove an element that is a member of the set.

        If the element is not a member, raise a KeyError.

        Parameters
        ----------
        value : any
            An element in the set

        Raises
        ------
        KeyError
            If the value is not found in the set.
        N)r   Úremover,   r   )r   r)   r!   r   r   rE   V  s    zTraitSet.removec                    s`   t |ƒ}ˆ  |¡}| |¡}‡ fdd„|D ƒ}| ˆ ¡}tƒ  ||B ¡ |sP|r\ˆ  ||¡ dS )z� Update the set with the symmetric difference of itself and another.

        Parameters
        ----------
        value : iterable
        c                    s   h | ]}ˆ   |¡’qS r   r#   r$   r   r   r   r4   t  r(   z7TraitSet.symmetric_difference_update.<locals>.<setcomp>N)r   r9   r/   r   Úsymmetric_difference_updater,   )r   r)   r;   r	   Z
raw_resultZvalidated_resultr
   r!   r   r   rF   i  s    


z$TraitSet.symmetric_difference_updatec                    sL   ‡ fdd„t  |¡D ƒ}| ˆ ¡}tƒ  |¡ t|ƒdkrHˆ  tƒ |¡ dS )zž Update the set with the union of itself and others.

        Parameters
        ----------
        args : iterables
            The other iterables.
        c                    s   h | ]}ˆ   |¡’qS r   r#   r$   r   r   r   r4   „  s   ÿz"TraitSet.update.<locals>.<setcomp>r   N)r   Úfrom_iterabler/   r   Úupdater0   r,   r   )r   r   Zvalidated_valuesr
   r!   r   r   rH   {  s    	
ÿ
zTraitSet.updatec                    s*   t ‡ fdd„| D ƒt | jˆ ¡g d�}|S )úb Perform a deepcopy operation.

        Notifiers are transient and should not be copied.
        c                    s   g | ]}t  |ˆ ¡‘qS r   ©r-   Údeepcopy©r%   Úx©Úmemor   r   Ú
<listcomp>•  r(   z)TraitSet.__deepcopy__.<locals>.<listcomp>r"   )r   r-   rK   Z	validator©r   rO   Úresultr   rN   r   Ú__deepcopy__Ž  s    ýzTraitSet.__deepcopy__c                 C   s   | j  ¡ }|d= |S )úw Get the state of the object for serialization.

        Notifiers are transient and should not be serialized.
        r   )Ú__dict__r-   ©r   rR   r   r   r   Ú__getstate__œ  s    
zTraitSet.__getstate__c                 C   s   g |d< | j  |¡ dS )úƒ Restore the state of the object after serialization.

        Notifiers are transient and are restored to the empty list.
        r   N)rU   rH   ©r   Ústater   r   r   Ú__setstate__¦  s    zTraitSet.__setstate__c                 C   s   | j S )a   Return a list of callables where each callable is a notifier.
        The list is expected to be mutated for contributing or removing
        notifiers from the object.

        Parameters
        ----------
        force_create: boolean
            Not used here.
        r*   )r   Zforce_creater   r   r   Ú
_notifiers°  s    
zTraitSet._notifiers)r   )r   r   r   r   r   r   r,   r.   r7   r8   r:   r<   r>   r@   rA   rC   rD   rE   rF   rH   rS   rW   r[   r\   Ú__classcell__r   r   r!   r   r   9   s*   % %

r   c                       sV   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Z‡ fd
d„Zdd„ Z	ddd„Z
‡  ZS )ÚTraitSetObjectaŠ   A specialization of TraitSet with a default validator and notifier
    for compatibility with Traits versions before 6.0.

    Parameters
    ----------
    trait : CTrait
        The trait that the set has been assigned to.
    object : HasTraits
        The object this set belongs to. Can also be None in cases where the
        set has been disconnected from its HasTraits parent.
    name : str
        The name of the trait on the object.
    value : iterable
        The initial value of the set.

    Attributes
    ----------
    trait : CTrait
        The trait that the set has been assigned to.
    object : callable
        A callable that when called with no arguments returns the HasTraits
        object that this set belongs to, or None if there is no such object.
    name : str
        The name of the trait on the object.
    value : iterable
        The initial value of the set.
    c                    sX   || _ |d u rdd„ nt|ƒ| _|| _d | _|jr<|d | _tƒ j|| j| j	gd� d S )Nc                   S   s   d S r   r   r   r   r   r   Ú<lambda>Ý  r(   z)TraitSetObject.__init__.<locals>.<lambda>Z_itemsr"   )
Útraitr   ÚobjectÚnameÚ
name_itemsZ	has_itemsr   r   Ú
_validatorr+   )r   r`   ra   rb   r)   r!   r   r   r   Ú  s    
ÿzTraitSetObject.__init__c              
   C   sŽ   t | ddƒ}t | ddƒ}|du s(|du r,|S |ƒ }|jj}|du rF|S z||| j|ƒW S  tyˆ } z| d¡ |‚W Y d}~n
d}~0 0 dS )al   Validates the value by calling the inner trait's validate method.

        Parameters
        ----------
        value : any
            The value to be validated.

        Returns
        -------
        value : any
            The validated value.

        Raises
        ------
        TraitError
            On validation failure for the inner trait.
        ra   Nr`   zEach element of the)ÚgetattrZ
item_traitÚvalidaterb   r   Z
set_prefix)r   r)   Z
object_refr`   ra   rf   Zexcpr   r   r   rd   æ  s    
zTraitSetObject._validatorc                 C   s`   | j du rdS |  ¡ }|du r"dS t|| jƒ| ur6dS t||d�}| j ¡ }| | j ||¡ dS )aE   Converts and consolidates the parameters to a TraitSetEvent and
        then fires the event.

        Parameters
        ----------
        trait_set : set
            The complete set
        removed : set
            Set of values that were removed.
        added : set
            Set of values that were added.
        Nr   )rc   ra   re   rb   r   r`   Úitems_eventZtrait_items_event)r   Z	trait_setr	   r
   ra   Úeventrg   r   r   r   r+     s    

zTraitSetObject.notifierc                    s$   t | jd| j‡ fdd„| D ƒƒ}|S )rI   Nc                    s   h | ]}t  |ˆ ¡’qS r   rJ   rL   rN   r   r   r4   7  r(   z.TraitSetObject.__deepcopy__.<locals>.<setcomp>)r^   r`   rb   rQ   r   rN   r   rS   -  s    üzTraitSetObject.__deepcopy__c                    s   t ƒ  ¡ }|d= |d= |S )rT   ra   r`   )r   rW   rV   r!   r   r   rW   <  s    
zTraitSetObject.__getstate__c                 C   s<   |  dd¡ | jg|d< dd„ |d< d|d< | j |¡ dS )	rX   rb   Ú r   c                   S   s   d S r   r   r   r   r   r   r_   O  r(   z-TraitSetObject.__setstate__.<locals>.<lambda>ra   Nr`   )Ú
setdefaultr+   rU   rH   rY   r   r   r   r[   G  s
    zTraitSetObject.__setstate__Nc                 C   s   t jt| ƒtt| ƒf|  ¡ fS )zB Overridden to make sure we call our custom __getstate__.
        )ÚcopyregÚ_reconstructorÚtyper   ÚlistrW   )r   Úprotocolr   r   r   Ú__reduce_ex__S  s    ýzTraitSetObject.__reduce_ex__)N)r   r   r   r   r   rd   r+   rS   rW   r[   rp   r]   r   r   r!   r   r^   ½  s   ' r^   )r-   rk   Ú	itertoolsr   Úweakrefr   Ztraits.observation.i_observabler   Ztraits.trait_baser   Ztraits.trait_errorsr   ra   r   Úregisterr   r   r^   r   r   r   r   Ú<module>   s   $   