3
6ßL]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)ÚselfZtraits)Ú	__class__© úF/tmp/pip-install-5sebvq2j/apptools/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   r0   š   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   r3   ¯   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   r6   Æ   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,   r8   r-   r"   r   r   r   r8   ×   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(   r2   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   r9   û   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(   r2   Úadd_preferences_listenerr    )r   Úlistenerr,   r!   r-   r"   r   r   r   r@   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(   r2   Úremove_preferences_listenerr    )r   rA   r,   r!   r-   r"   r   r   r   rC   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Ú	configobjrD   ÚlistÚitemsr(   r2   Ú_add_dictionary_to_node)	r   Úfile_or_filenamerD   Ú
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   )rD   zsaving preferences to <%s>zutf-8)rE   )r   r   rH   rD   rF   rG   Ú_add_node_to_dictionaryÚwrite)r   rL   rD   rM   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   rK   ¢  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   r7   r,   r)   r;   rO   r*   )r   r"   rU   Úkeyr   r   r   r   rO   «  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   rQ   Ú_preferences_listenersr   rT   )r   rA   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   rQ   rR   r6   rT   )r   r   r   r   r5   Ä  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   rQ   r   Ú	_childrenrT   )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   rQ   rR   r+   rT   )r   rV   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   rQ   rX   r+   rT   )r   r   rY   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   rQ   rI   rR   r8   rT   )r   r8   r   r   r   r7   ì  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*   rZ   )r   r   r"   r   r   r   r2   õ  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   rQ   rI   rX   r8   rT   )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   rQ   rR   rT   )r   r   r   r   r   r/     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   rQ   rW   r0   rT   )r   rA   r   r   r   rB     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   rQ   rR   r+   rW   rT   )r   rV   r.   ÚoldZ	listenersrA   r   r   r   r1     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. r4   zNode(%s)z  N)Úprintr   rR   rI   rX   ÚvaluesÚdump)r   ÚindentrY   r   r   r   r_   <  s    zPreferences.dump)NF)r4   )r4   )r4   )r4   )r4   )r4   )r4   )N)N)N)r4   )2Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r	   r,   r   r   r   r   r   r   r   r   rX   rR   r   r   rW   r   r#   r+   r0   r3   r6   r8   r"   r9   r<   r>   r@   rC   r   r=   rK   rO   r?   r5   rZ   r)   r*   r7   r2   r;   r/   rB   r1   r_   Ú__classcell__r   r   )r   r   r      sL   



*







%
#				
			

r   )rd   Úloggingr   Z
traits.apir   r   r   r   r   r   r   r	   r
   r   Zi_preferencesr   Ú	getLoggerra   rF   r   r   r   r   r   Ú<module>   s    
