a
    ¨ˆcD  ã                   @   s~   d Z ddlZddlZddlmZmZmZmZmZm	Z	 ddlm
Z
mZmZmZ ddlmZ e e¡ZeeƒG dd„ deƒƒZdS )	úB The default implementation of a node in a preferences hierarchy. é    N)ÚAnyÚCallableÚDictÚ	HasTraitsÚInstanceÚList)ÚPropertyÚStrÚ	UndefinedÚprovidesé   )ÚIPreferencesc                       sP  e Zd ZdZeeƒZeeƒZ	eZ
eZeZeeeƒZeeeƒZeeƒZ‡ fdd„Zdd„ Zd?dd	„Zd
d„ Zdd„ Zd@dd„ZdAdd„ZdBdd„ZdCdd„ZdDdd„Zdd„ ZdEdd„ZdFdd„Z dGdd „Z!dHd!d"„Z"d#d$„ Z#d%d&„ Z$d'd(„ Z%d)d*„ Z&d+d,„ Z'dId-d.„Z(d/d0„ Z)d1d2„ Z*d3d4„ Z+d5d6„ Z,d7d8„ Z-d9d:„ Z.d;d<„ Z/dJd=d>„Z0‡  Z1S )KÚPreferencesr   c                    s6   t  ¡ | _tƒ jf i |¤Ž t| jƒdkr2|  ¡  dS )z Constructor. r   N)Ú	threadingÚLockÚ_lkÚsuperÚ__init__ÚlenÚfilenameÚload)ÚselfZtraits©Ú	__class__© úc/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/apptools/preferences/preferences.pyr   O   s    
zPreferences.__init__c                 C   s8   g }| }|j dur&| |j¡ |j }q| ¡  d |¡S )z Property getter. NÚ.)ÚparentÚappendÚnameÚreverseÚjoin)r   ÚnamesÚnoder   r   r   Ú	_get_pathc   s    
zPreferences._get_pathNFc                 C   s²   t |ƒdkrtdƒ‚| d¡}t |ƒdkr8|  |t¡}n6|  |d ¡}|durj| d |dd… ¡t¡}nt}|r¢|tu r¢t |ƒdkr¢|d= | jd |¡td�}qn|tu r®|}|S )z8 Get the value of the preference at the specified path. r   ú
empty pathr   r   Néþÿÿÿ)Údefault)r   Ú
ValueErrorÚsplitÚ_getr   Ú
_get_childÚgetr"   )r   Úpathr(   ZinheritÚ
componentsÚvaluer$   r   r   r   r-   u   s    
zPreferences.getc                 C   sh   t |ƒdkrtdƒ‚| d¡}t |ƒdkr6|  |¡ n.|  |d ¡}|durd| d |dd… ¡¡ dS )z. Remove the preference at the specified path. r   r&   r   r   N)r   r)   r*   Ú_remover,   Úremover"   ©r   r.   r/   r$   r   r   r   r2   Ÿ   s    
zPreferences.removec                 C   sd   t |ƒdkrtdƒ‚| d¡}t |ƒdkr8|  ||¡ n(|  |d ¡}| d |dd… ¡|¡ dS )z8 Set the value of the preference at the specified path. r   r&   r   r   N)r   r)   r*   Ú_setÚ_nodeÚsetr"   )r   r.   r0   r/   r$   r   r   r   r6   ²   s    
zPreferences.setÚ c                 C   sR   t |ƒdkr|  ¡  n8| d¡}|  |d ¡}|durN| d |dd… ¡¡ dS )z= Remove all preferences from the node at the specified path. r   r   Nr   )r   Ú_clearr*   r,   Úclearr"   r3   r   r   r   r9   Ç   s    

zPreferences.clearc                 C   sX   t |ƒdkr|  ¡ }n>| d¡}|  |d ¡}|durP| d |dd… ¡¡}ng }|S )z? Return the preference keys of the node at the specified path. r   r   Nr   )r   Ú_keysr*   r,   Úkeysr"   )r   r.   r;   r/   r$   r   r   r   r;   Ö   s    

zPreferences.keysc                 C   sF   t |ƒdkr| }n0| d¡}|  |d ¡}| d |dd… ¡¡}|S )z( Return the node at the specified path. r   r   r   N)r   r*   r5   r$   r"   )r   r.   r$   r/   r   r   r   r$   ê   s    
zPreferences.nodec                 C   sT   t |ƒdkrd}n>| d¡}|  |d ¡}|durL| d |dd… ¡¡}nd}|S )z7 Return True if the node at the specified path exists. r   Tr   Nr   F)r   r*   r,   Únode_existsr"   )r   r.   Úexistsr/   r$   r   r   r   r<   ú   s    
zPreferences.node_existsc                 C   sX   t |ƒdkr|  ¡ }n>| d¡}|  |d ¡}|durP| d |dd… ¡¡}ng }|S )zLReturn the names of the children of the node at the specified path.
        r   r   Nr   )r   Ú_node_namesr*   r,   Ú
node_namesr"   )r   r.   r#   r/   r$   r   r   r   r?     s    

zPreferences.node_namesc                 C   s   |   ¡  dS )z{Force any changes in the node to the backing store.

        This includes any changes to the node's descendants.

        N)Úsave©r   r   r   r   Úflush%  s    zPreferences.flushc                 C   sN   t |ƒdkr|  |¡ n2| d¡}|  |d ¡}| |d |dd… ¡¡ dS )z5 Add a listener for changes to a node's preferences. r   r   r   N)r   Ú_add_preferences_listenerr*   r5   Úadd_preferences_listenerr"   ©r   Úlistenerr.   r/   r$   r   r   r   rD   4  s
    
z$Preferences.add_preferences_listenerc                 C   sN   t |ƒdkr|  |¡ n2| d¡}|  |d ¡}| |d |dd… ¡¡ dS )z8 Remove a listener for changes to a node's preferences. r   r   r   N)r   Ú_remove_preferences_listenerr*   r5   Úremove_preferences_listenerr"   rE   r   r   r   rH   B  s    
ÿz'Preferences.remove_preferences_listenerc           	      C   sv   |du r| j }t d|¡ ddlm} ||dd�}| ¡ D ]6\}}| d¡}| }|D ]}| |¡}qT|  ||¡ q:dS )z¾Load preferences from a file.

        This is a *merge* operation i.e. the contents of the file are added to
        the node.

        This implementation uses 'ConfigObj' files.

        Nzloading preferences from <%s>r   ©Ú	ConfigObjúutf-8©Úencodingr   )	r   ÚloggerÚdebugÚ	configobjrJ   Úitemsr*   r5   Ú_add_dictionary_to_node)	r   Úfile_or_filenamerJ   Ú
config_objr    r0   r/   r$   Ú	componentr   r   r   r   T  s    

zPreferences.loadc                 C   sV   |du r| j }t|ƒdkrRddlm} t d|¡ ||dd�}|  | |¡ | ¡  dS )zeSave the node's preferences to a file.

        This implementation uses 'ConfigObj' files.

        Nr   rI   zsaving preferences to <%s>rK   rL   )r   r   rP   rJ   rN   rO   Ú_add_node_to_dictionaryÚwrite)r   rS   rJ   rT   r   r   r   r@   w  s    zPreferences.savec                 C   s6   | j � |j |¡ W d  ƒ n1 s(0    Y  dS )z; Add the contents of a dictionary to a node's preferences. N)r   Ú_preferencesÚupdate)r   r$   Ú
dictionaryr   r   r   rR   ˜  s    z#Preferences._add_dictionary_to_nodec                 C   s`   t | ¡ ƒdkr<i ||j< | ¡ D ]}| |¡||j |< q"| ¡ D ]}|  | |¡|¡ qDdS )z+ Add a node's preferences to a dictionary. r   N)r   r:   r.   r+   r>   rV   r,   )r   r$   rZ   Úkeyr    r   r   r   rV   ž  s    
z#Preferences._add_node_to_dictionaryc                 C   s6   | j � | j |¡ W d  ƒ n1 s(0    Y  dS )z7 Add a listener for changes to thisnode's preferences. N)r   Ú_preferences_listenersr   ©r   rF   r   r   r   rC   ¬  s    z%Preferences._add_preferences_listenerc                 C   s4   | j � | j ¡  W d  ƒ n1 s&0    Y  dS )z( Remove all preferences from this node. N)r   rX   r9   rA   r   r   r   r8   ²  s    zPreferences._clearc                 C   s@   | j �& t|| d� }| j|< W d  ƒ n1 s20    Y  |S )z6 Create a child of this node with the specified name. )r    r   N)r   r   Ú	_children©r   r    Úchildr   r   r   Ú_create_child¸  s    4zPreferences._create_childc                 C   s8   | j � | j ||¡}W d  ƒ n1 s*0    Y  |S )z- Get the value of a preference in this node. N)r   rX   r-   )r   r[   r(   r0   r   r   r   r+   À  s    ,zPreferences._getc                 C   s6   | j � | j |¡}W d  ƒ n1 s(0    Y  |S )znReturn the child of this node with the specified name.

        Return None if no such child exists.

        N)r   r^   r-   r_   r   r   r   r,   È  s    *zPreferences._get_childc                 C   s8   | j � t| j ¡ ƒ}W d  ƒ n1 s*0    Y  |S )z* Return the preference keys of this node. N)r   ÚlistrX   r;   )r   r;   r   r   r   r:   Ô  s    ,zPreferences._keysc                 C   s    |   |¡}|du r|  |¡}|S )zuReturn the child of this node with the specified name.

        Create the child node if it does not exist.

        N)r,   ra   )r   r    r$   r   r   r   r5   Ü  s    

zPreferences._nodec                 C   s8   | j � t| j ¡ ƒ}W d  ƒ n1 s*0    Y  |S )z0 Return the names of the children of this node. N)r   rb   r^   r;   )r   r?   r   r   r   r>   é  s    ,zPreferences._node_namesc                 C   s<   | j �" || jv r| j|= W d  ƒ n1 s.0    Y  dS )z+ Remove a preference value from this node. N)r   rX   )r   r    r   r   r   r1   ñ  s    
zPreferences._removec                 C   s@   | j �& || jv r| j |¡ W d  ƒ n1 s20    Y  dS )z: Remove a listener for changes to the node's preferences. N)r   r\   r2   r]   r   r   r   rG   ø  s    
z(Preferences._remove_preferences_listenerc                 C   s|   t |ƒ}| j�B | j |¡}|| j|< ||kr4g }n| jdd… }W d  ƒ n1 sV0    Y  |D ]}|| |||ƒ qddS )z- Set the value of a preference in this node. N)Ústrr   rX   r-   r\   )r   r[   r0   ÚoldZ	listenersrF   r   r   r   r4   ÿ  s    
,zPreferences._setc                 C   sH   |dkrt ƒ  t |d| j | jƒ |d7 }| j ¡ D ]}| |¡ q4dS )z+ Dump the preferences hierarchy to stdout. r7   zNode(%s)z  N)Úprintr    rX   r^   ÚvaluesÚdump)r   Úindentr`   r   r   r   rg     s    zPreferences.dump)NF)r7   )r7   )r7   )r7   )r7   )r7   )r7   )N)N)N)r7   )2Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   r
   r.   r   r   r   r    r   r   r   r   r^   rX   r   r   r\   r   r%   r-   r2   r6   r9   r;   r$   r<   r?   rB   rD   rH   r   r@   rR   rV   rC   r8   ra   r+   r,   r:   r5   r>   r1   rG   r4   rg   Ú__classcell__r   r   r   r   r      sL   



*







#
!
r   )rl   Úloggingr   Z
traits.apir   r   r   r   r   r   r	   r
   r   r   Zi_preferencesr   Ú	getLoggerri   rN   r   r   r   r   r   Ú<module>
   s    
