3
AAd]E  ã               @   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jeƒZeeƒG dd„ deƒƒZdS )	zB 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ÚPreferenceszB The default implementation of a node in a preferences hierarchy. c                s6   t jƒ | _tt| ƒjf |Ž t| jƒdkr2| jƒ  dS )z Constructor. r   N)	Ú	threadingÚLockÚ_lkÚsuperr   Ú__init__ÚlenÚfilenameÚload)ÚselfÚtraits)Ú	__class__© ú|/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.6/site-packages/apptools-4.4.0-py3.6.egg/apptools/preferences/preferences.pyr   H   s
    
zPreferences.__init__c             C   s<   g }| }x |j dk	r(|j|jƒ |j }q
W |jƒ  dj|ƒS )z Property getter. NÚ.)ÚparentÚappendÚnameÚreverseÚjoin)r   ÚnamesÚnoder   r   r   Ú	_get_path^   s    
zPreferences._get_pathNFc             C   s¶   t |ƒdkrtdƒ‚|jdƒ}t |ƒdkr8| j|tƒ}n6| j|d ƒ}|dk	rj|jdj|dd… ƒtƒ}nt}x6|r¤|tkr¤t |ƒdkr¤|d= | jdj|ƒtd�}qpW |tkr²|}|S )	z8 Get the value of the preference at the specified path. r   z
empty pathr   r   Né   )Údefaultéþÿÿÿ)r   Ú
ValueErrorÚsplitÚ_getr
   Ú
_get_childÚgetr!   )r   Úpathr&   ZinheritÚ
componentsÚvaluer#   r   r   r   r,   p   s    
zPreferences.getc             C   sh   t |ƒdkrtdƒ‚|jdƒ}t |ƒdkr6| j|ƒ n.| j|d ƒ}|dk	rd|jdj|dd… ƒƒ dS )z. Remove the preference at the specified path. r   z
empty pathr   r   N)r   r(   r)   Ú_remover+   Úremover!   )r   r-   r.   r#   r   r   r   r1   š   s    
zPreferences.removec             C   sd   t |ƒdkrtdƒ‚|jdƒ}t |ƒdkr8| j||ƒ n(| j|d ƒ}|jdj|dd… ƒ|ƒ dS )z8 Set the value of the preference at the specified path. r   z
empty pathr   r   N)r   r(   r)   Ú_setÚ_nodeÚsetr!   )r   r-   r/   r.   r#   r   r   r   r4   ¯   s    
zPreferences.setÚ c             C   sR   t |ƒdkr| jƒ  n8|jdƒ}| j|d ƒ}|dk	rN|jdj|dd… ƒƒ dS )z= Remove all preferences from the node at the specified path. r   r   Nr   )r   Ú_clearr)   r+   Úclearr!   )r   r-   r.   r#   r   r   r   r7   Æ   s    

zPreferences.clearc             C   sX   t |ƒdkr| jƒ }n>|jdƒ}| j|d ƒ}|dk	rP|jdj|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-   r9   r.   r#   r   r   r   r9   ×   s    

zPreferences.keysc             C   sF   t |ƒdkr| }n0|jdƒ}| j|d ƒ}|jdj|dd… ƒƒ}|S )z( Return the node at the specified path. r   r   r   N)r   r)   r3   r#   r!   )r   r-   r#   r.   r   r   r   r#   ë   s    
zPreferences.nodec             C   sT   t |ƒdkrd}n>|jdƒ}| j|d ƒ}|dk	rL|jdj|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| jƒ }n>|jdƒ}| j|d ƒ}|dk	rP|jdj|dd… ƒƒ}ng }|S )zN Return 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   | j ƒ  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| j|ƒ}n2|jdƒ}| j|d ƒ}|j|dj|dd… ƒƒ dS )z5 Add a listener for changes to a node's preferences. r   r   r   N)r   Ú_add_preferences_listenerr)   r3   Úadd_preferences_listenerr!   )r   Úlistenerr-   r"   r.   r#   r   r   r   rA   8  s    
z$Preferences.add_preferences_listenerc             C   sN   t |ƒdkr| j|ƒ}n2|jdƒ}| j|d ƒ}|j|dj|dd… ƒƒ dS )z8 Remove a listener for changes to a node's preferences. r   r   r   N)r   Ú_remove_preferences_listenerr)   r3   Úremove_preferences_listenerr!   )r   rB   r-   r"   r.   r#   r   r   r   rD   H  s    
z'Preferences.remove_preferences_listenerc       	      C   s‚   |dkr| j }tjd|ƒ ddlm} ||dd�}xJt|jƒ ƒD ]:\}}|jdƒ}| }x|D ]}|j|ƒ}q\W | j	||ƒ q@W 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   )Ú	ConfigObjzutf-8)Úencodingr   )
r   ÚloggerÚdebugÚ	configobjrE   ÚlistÚitemsr)   r3   Ú_add_dictionary_to_node)	r   Úfile_or_filenamerE   Ú
config_objr   r/   r.   r#   Ú	componentr   r   r   r   Z  s    


zPreferences.loadc             C   sV   |dkr| j }t|ƒdkrRddlm} tjd|ƒ ||dd�}| j| |ƒ |jƒ  dS )zf Save the node's preferences to a file.

        This implementation uses 'ConfigObj' files.

        Nr   )rE   zsaving preferences to <%s>zutf-8)rF   )r   r   rI   rE   rG   rH   Ú_add_node_to_dictionaryÚwrite)r   rM   rE   rN   r   r   r   r>     s    zPreferences.savec             C   s$   | j jƒ  |jj|ƒ | j jƒ  dS )z; Add the contents of a dictionary to a node's preferences. N)r   ÚacquireÚ_preferencesÚupdateÚrelease)r   r#   Ú
dictionaryr   r   r   rL   ¢  s    

z#Preferences._add_dictionary_to_nodec             C   sh   t |jƒ ƒdkr@i ||j< x$|jƒ D ]}|j|ƒ||j |< q$W x"|jƒ D ]}| j|j|ƒ|ƒ qJW dS )z+ Add a node's preferences to a dictionary. r   N)r   r8   r-   r*   r<   rP   r+   )r   r#   rV   Úkeyr   r   r   r   rP   «  s    
z#Preferences._add_node_to_dictionaryc             C   s$   | j jƒ  | jj|ƒ | j jƒ  dS )z7 Add a listener for changes to thisnode's preferences. N)r   rR   Ú_preferences_listenersr   rU   )r   rB   r   r   r   r@   »  s    

z%Preferences._add_preferences_listenerc             C   s"   | j jƒ  | jjƒ  | j jƒ  dS )z( Remove all preferences from this node. N)r   rR   rS   r7   rU   )r   r   r   r   r6   Ä  s    


zPreferences._clearc             C   s.   | j jƒ  t|| d� }| j|< | j jƒ  |S )z6 Create a child of this node with the specified name. )r   r   )r   rR   r   Ú	_childrenrU   )r   r   Úchildr   r   r   Ú_create_childÍ  s    

zPreferences._create_childc             C   s&   | j jƒ  | jj||ƒ}| j jƒ  |S )z- Get the value of a preference in this node. )r   rR   rS   r,   rU   )r   rW   r&   r/   r   r   r   r*   Ö  s    

zPreferences._getc             C   s$   | j jƒ  | jj|ƒ}| j jƒ  |S )zo Return the child of this node with the specified name.

        Return None if no such child exists.

        )r   rR   rY   r,   rU   )r   r   rZ   r   r   r   r+   ß  s    

zPreferences._get_childc             C   s&   | j jƒ  t| jjƒ ƒ}| j jƒ  |S )z* Return the preference keys of this node. )r   rR   rJ   rS   r9   rU   )r   r9   r   r   r   r8   ì  s    

zPreferences._keysc             C   s    | j |ƒ}|dkr| j|ƒ}|S )zv Return the child of this node with the specified name.

        Create the child node if it does not exist.

        N)r+   r[   )r   r   r#   r   r   r   r3   õ  s    

zPreferences._nodec             C   s&   | j jƒ  t| jjƒ ƒ}| j jƒ  |S )z0 Return the names of the children of this node. )r   rR   rJ   rY   r9   rU   )r   r=   r   r   r   r<     s    

zPreferences._node_namesc             C   s*   | j jƒ  || jkr| j|= | j jƒ  dS )z+ Remove a preference value from this node. N)r   rR   rS   rU   )r   r   r   r   r   r0     s
    


zPreferences._removec             C   s.   | j jƒ  || jkr | jj|ƒ | j jƒ  dS )z: Remove a listener for changes to the node's preferences. N)r   rR   rX   r1   rU   )r   rB   r   r   r   rC     s
    


z(Preferences._remove_preferences_listenerc             C   sn   t |ƒ}| jjƒ  | jj|ƒ}|| j|< ||kr6g }n| jdd… }| jjƒ  x|D ]}|| |||ƒ qTW dS )z- Set the value of a preference in this node. N)Ústrr   rR   rS   r,   rX   rU   )r   rW   r/   ÚoldZ	listenersrB   r   r   r   r2     s    



zPreferences._setc             C   sP   |dkrt ƒ  t |d| j | jƒ |d7 }x t| jjƒ ƒD ]}|j|ƒ q:W dS )z+ Dump the preferences hierarchy to stdout. r5   zNode(%s)z  N)Úprintr   rS   rJ   rY   ÚvaluesÚdump)r   ÚindentrZ   r   r   r   r`   <  s    zPreferences.dump)NF)r5   )r5   )r5   )r5   )r5   )r5   )r5   )N)N)N)r5   )2Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r	   r-   r   r   r   r   r   r   r   r   rY   rS   r   r   rX   r   r$   r,   r1   r4   r7   r9   r#   r:   r=   r?   rA   rD   r   r>   rL   rP   r@   r6   r[   r*   r+   r8   r3   r<   r0   rC   r2   r`   Ú__classcell__r   r   )r   r   r      sL   



*







%
#				
			

r   )re   Úloggingr   Z
traits.apir   r   r   r   r   r   r   r	   r
   r   Zi_preferencesr   Ú	getLoggerrb   rG   r   r   r   r   r   Ú<module>   s    
