3
AAd]ô7  ã               @   s\   d Z ddlmZ ddlmZ ddlmZmZmZ ddl	m
Z
 ddlmZ G dd	„ d	eƒZd
S )z@ A preferences node that adds the notion of preferences scopes. é    )Újoin)Ú	ETSConfig)ÚListÚStrÚ	Undefinedé   )ÚIPreferences)ÚPreferencesc               @   sÞ   e Zd ZdZeZeeƒZeZ	d/dd„Z
dd„ Zdd	„ Zd0dd„Zd1dd„Zd2dd„Zd3dd„Zd4dd„Zd5dd„Zd6dd„Zd7dd„Zd8dd„Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zd9d-d.„ZdS ):ÚScopedPreferencesa/   A preferences node that adds the notion of preferences scopes.

    Scopes provide a way to access preferences in a precedence order, usually
    depending on where they came from, for example from the command-line,
    or set by the user in a preferences file, or the defaults (set by the
    developer).

    By default, this class provides two scopes - 'application' which is
    persistent and 'default' which is not.

    Path names passed to 'ScopedPreferences' nodes can be either::

    a) a preference path as used in a standard 'Preferences' node, e.g::

       'acme.widget.bgcolor'.

       In this case the operation either takes place in the primary scope
       (for operations such as 'set' etc), or on all scopes in precedence order
       (for operations such as 'get' etc).

    or

    b) a preference path that refers to a specific scope e.g::

       'default/acme.widget.bgcolor'

       In this case the operation takes place *only* in the specified scope.

    There is one drawback to this scheme. If you want to access a scope node
    itself via the 'clear', 'keys', 'node', 'node_exists' or 'node_names'
    methods then you have to append a trailing '/' to the path. Without that,
    the node would try to perform the operation in the primary scope.

    e.g. To get the names of the children of the 'application' scope, use::

      scoped.node_names('application/')

    If you did this::

      scoped.node_names('application')

    Then the node would get the primary scope and try to find its child node
    called 'application'.

    Of course you can just get the scope via::

      application_scope = scoped.get_scope('application')

    and then call whatever methods you like on it - which is definitely more
    intentional and is highly recommended::

      application_scope.node_names()

    NFc             C   sz   t |ƒdkrtdƒ‚| j|ƒr:| j|ƒ\}}| j|ƒg}n| j}| j|t|dd�}|tkrv|rr| j|||dd�}n|}|S )z8 Get the value of the preference at the specified path. r   z
empty pathF)ÚinheritT)ÚlenÚ
ValueErrorÚ_path_contains_scopeÚ_parse_pathÚ
_get_scopeÚscopesÚ_getr   )ÚselfÚpathÚdefaultr   Ú
scope_nameÚnodesÚvalue© r   úƒ/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.6/site-packages/apptools-4.4.0-py3.6.egg/apptools/preferences/scoped_preferences.pyÚgeth   s    
zScopedPreferences.getc             C   sN   t |ƒdkrtdƒ‚| j|ƒr8| j|ƒ\}}| j|ƒ}n| jƒ }|j|ƒ dS )z. Remove the preference at the specified path. r   z
empty pathN)r   r   r   r   r   Ú_get_primary_scopeÚremove)r   r   r   Únoder   r   r   r   ƒ   s    

zScopedPreferences.removec             C   sP   t |ƒdkrtdƒ‚| j|ƒr8| j|ƒ\}}| j|ƒ}n| jƒ }|j||ƒ dS )z8 Set the value of the preference at the specified path. r   z
empty pathN)r   r   r   r   r   r   Úset)r   r   r   r   r   r   r   r   r   —   s    
zScopedPreferences.setÚ c             C   s6   | j |ƒr$| j|ƒ\}}| j|ƒ}n| jƒ }|j|ƒS )z< Remove all preference from the node at the specified path. )r   r   r   r   Úclear)r   r   r   r   r   r   r   r!   ­   s
    
zScopedPreferences.clearc             C   s`   | j |ƒr&| j|ƒ\}}| j|ƒg}n| j}tƒ }x$|D ]}|jt|j|ƒjƒ ƒƒ q8W t|ƒS )z? Return the preference keys of the node at the specified path. )	r   r   r   r   r   ÚupdateÚlistr   Úkeys)r   r   r   r   r$   r   r   r   r   r$   ¼   s    

zScopedPreferences.keysc             C   sL   t |ƒdkr| }n6| j|ƒr6| j|ƒ\}}| j|ƒ}n| jƒ }|j|ƒ}|S )z( Return the node at the specified path. r   )r   r   r   r   r   r   )r   r   r   r   r   r   r   r   Ï   s    

zScopedPreferences.nodec             C   s6   | j |ƒr$| j|ƒ\}}| j|ƒ}n| jƒ }|j|ƒS )z7 Return True if the node at the specified path exists. )r   r   r   r   Únode_exists)r   r   r   r   r   r   r   r%   ä   s
    
zScopedPreferences.node_existsc             C   s\   | j |ƒr&| j|ƒ\}}| j|ƒg}n| j}tƒ }x |D ]}|j|j|ƒjƒ ƒ q8W t|ƒS )zN Return the names of the children of the node at the specified path.

        )	r   r   r   r   r   r"   r   Ú
node_namesr#   )r   r   r   r   Únamesr   r   r   r   r&   ó   s    

zScopedPreferences.node_namesc             C   sJ   | j |ƒr&| j|ƒ\}}| j|ƒg}n| j}x|D ]}|j||ƒ q2W dS )z5 Add a listener for changes to a node's preferences. N)r   r   r   r   Úadd_preferences_listener)r   Úlistenerr   r   r   r   r   r   r   r(     s    

z*ScopedPreferences.add_preferences_listenerc             C   sJ   | j |ƒr&| j|ƒ\}}| j|ƒg}n| j}x|D ]}|j||ƒ q2W dS )z8 Remove a listener for changes to a node's preferences. N)r   r   r   r   Úremove_preferences_listener)r   r)   r   r   r   r   r   r   r   r*      s    

z-ScopedPreferences.remove_preferences_listenerc             C   s2   |dkrt | jƒdkr| j}| jƒ }|j|ƒ dS )a   Load preferences from a file.

        This loads the preferences into the primary scope.

        fixme: I'm not sure it is worth providing an implentation here. I
        think it would be better to encourage people to explicitly reference
        a particular scope.

        Nr   )r   Úfilenamer   Úload)r   Úfile_or_filenamer   r   r   r   r,   4  s
    
zScopedPreferences.loadc             C   sR   |dkrt | jƒdkr| j}| jƒ j|ƒ x"| jD ]}|| jƒ k	r2|jƒ  q2W dS )zÑ Save the node's preferences to a file.

        This asks each scope in turn to save its preferences.

        If a file or filename is specified then it is only passed to the
        primary scope.

        Nr   )r   r+   r   Úsaver   )r   r-   Úscoper   r   r   r.   G  s    
zScopedPreferences.savec             C   s   t tjdƒS )z Trait initializer. zpreferences.ini)r   r   Zapplication_home)r   r   r   r   Ú)_application_preferences_filename_default_  s    z;ScopedPreferences._application_preferences_filename_defaultc             C   s   t d| jd�t dd�g}|S )z Trait initializer. Zapplication)Únamer+   r   )r1   )r	   Ú application_preferences_filename)r   r   r   r   r   Ú_scopes_defaultg  s
    z!ScopedPreferences._scopes_defaultc             C   s$   x| j D ]}||jkrP qW d}|S )zb Return the scope with the specified name.

        Return None if no such scope exists.

        N)r   r1   )r   r   r/   r   r   r   Ú	get_scopeu  s
    
zScopedPreferences.get_scopec             C   s.   x(|D ]}|j |t|ƒ}|tk	rP qW |}|S )z( Get a preference from a list of nodes. )r   r   )r   r   r   r   r   r   r   r   r   r   r   ‰  s    
zScopedPreferences._getc             C   s"   | j |ƒ}|dkrtd| ƒ‚|S )zk Return the scope with the specified name.

        Raise a 'ValueError' is no such scope exists.

        Nzno such scope %s)r4   r   )r   r   r/   r   r   r   r   –  s    
zScopedPreferences._get_scopec             C   s*   t | jƒdkr| j| jƒ}n
| jd }|S )zR Return the primary scope.

        By default, this is the first scope.

        r   )r   Úprimary_scope_namer   r   )r   r/   r   r   r   r   £  s    
z$ScopedPreferences._get_primary_scopec             C   s   d|kS )z5 Return True if the path contains a scope component. ú/r   )r   r   r   r   r   r   ²  s    z&ScopedPreferences._path_contains_scopec             C   s$   |j dƒ}|d dj|dd… ƒfS )z? 'Parse' the path into two parts, the scope name and the rest! r6   r   r   N)Úsplitr   )r   r   Ú
componentsr   r   r   r   ·  s    
zScopedPreferences._parse_pathc             C   sH   |dkrt ƒ  t |d| j | jƒ |d7 }x| jD ]}|j|ƒ q2W dS )z+ Dump the preferences hierarchy to stdout. r    zNode(%s)z  N)Úprintr1   Z_preferencesr   Údump)r   ÚindentÚchildr   r   r   r:   Â  s    zScopedPreferences.dump)NF)r    )r    )r    )r    )r    )r    )r    )N)N)r    )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r2   r   r   r   r5   r   r   r   r!   r$   r   r%   r&   r(   r*   r,   r.   r0   r3   r4   r   r   r   r   r   r:   r   r   r   r   r
      s2   6		









r
   N)r@   Úos.pathr   Ztraits.etsconfig.apir   Z
traits.apir   r   r   Zi_preferencesr   Zpreferencesr	   r
   r   r   r   r   Ú<module>   s   