a
    ¨ˆcÆ  ã                   @   sb   d Z ddlmZ ddlmZmZmZmZmZ ddl	m
Z
 ddlmZ G dd„ deƒZdd
d„Zd	S )ú@ A binding between a trait on an object and a preference value. é    )Úliteral_eval)ÚAnyÚ	HasTraitsÚInstanceÚStrÚ	Undefinedé   )ÚIPreferences©Úget_default_preferencesc                       sf   e Zd ZdZeZeeƒZe	Z
e	Z‡ fdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Zddd„Z‡  ZS )ÚPreferenceBindingr   c                    s.   t t| ƒjf i |¤Ž | jdd� |  ¡  dS )z Constructor. F)ÚnotifyN)Úsuperr   Ú__init__Ú
_set_traitÚ_initialize)ÚselfÚtraits©Ú	__class__© új/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/apptools/preferences/preference_binding.pyr   -   s    zPreferenceBinding.__init__c                 C   s   t ƒ S )z Trait initializer. r   )r   r   r   r   Ú_preferences_default>   s    z&PreferenceBinding._preferences_defaultc                 C   s   | j  | j|j¡ dS )z Dynamic trait change handler. N)ÚpreferencesÚsetÚpreference_pathÚnew)r   Úeventr   r   r   Ú_on_trait_changedI   s    z#PreferenceBinding._on_trait_changedc                 C   s$   | j  d¡}||d kr |  ¡  dS )z5 Listener called when a preference value is changed. Ú.éÿÿÿÿN)r   Úsplitr   )r   ÚnodeÚkeyÚoldr   Ú
componentsr   r   r   Ú_preferences_listenerP   s    z'PreferenceBinding._preferences_listenerc                 C   sJ   | j  |¡j}t|ƒtu rn zt|ƒ}W n ty:   Y n0 | | ||¡S )zœGet the actual value to set.

        This method makes sure that any required work is done to convert the
        preference value from a string.

        )ÚobjZtraitÚhandlerÚtyper   r   Ú	ExceptionÚvalidate)r   Ú
trait_nameÚvaluer)   r   r   r   Ú
_get_value[   s    zPreferenceBinding._get_valuec                 C   sD   | j  | j| j¡ | j d¡}d |dd… ¡}| j | j	|¡ dS )z$ Wire-up trait change handlers etc. r    Nr!   )
r(   Zobserver   r-   r   r"   Újoinr   Zadd_preferences_listenerr'   )r   r&   r#   r   r   r   r   v   s    ÿzPreferenceBinding._initializeTc                 C   sJ   | j  | jt¡}|turF|  | j|¡}| j|i}| jjf d|i|¤Ž dS )z8 Set the object's trait to the value of the preference. Ztrait_change_notifyN)r   Úgetr   r   r/   r-   r(   Z	trait_set)r   r   r.   Ztrait_valuer   r   r   r   r   „   s
    
zPreferenceBinding._set_trait)T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r(   r   r
   r   r   r   r-   r   r   r   r'   r/   r   r   Ú__classcell__r   r   r   r   r      s   r   Nc                 C   s*   | ||dœ}|dur||d< t f i |¤ŽS )z" Create a new preference binding. )r(   r-   r   Nr   )r   )r(   r-   r   r   r   r   r   r   Úbind_preference�   s    ýr7   )N)r5   Úastr   Z
traits.apir   r   r   r   r   Zi_preferencesr
   Zpackage_globalsr   r   r7   r   r   r   r   Ú<module>
   s   z