a
    ¨ˆc  ã                   @   s<   d Z ddlmZ ddlmZ ddlmZ G dd„ deƒZdS )	ú, The base class for all directory contexts. é    )ÚDicté   )ÚContext)ÚNameNotFoundErrorc                       sd   e Zd ZdZeZdd„ Zdd„ Zdd„ Zdd	„ Z	d
d„ Z
‡ fdd„Z‡ fdd„Z‡ fdd„Z‡  ZS )Ú
DirContextr   c                 C   s˜   t |ƒdkr|  |¡}n||  |¡}t |ƒdkrT|d }|  |¡sHt|ƒ‚|  |¡}n@|  |d ¡snt|d ƒ‚|  |d ¡}| d |dd… ¡¡}|S )z8 Returns the attributes associated with a named object. r   r   ú/N)ÚlenÚ_get_attributesÚ_parse_nameÚ	_is_boundr   Ú_get_next_contextÚget_attributesÚjoin©ÚselfÚnameÚ
attributesÚ
componentsZatomZnext_context© r   ú^/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/apptools/naming/dir_context.pyr   "   s    

ÿzDirContext.get_attributesc                 C   sž   t |ƒdkr|  ||¡}n€|  |¡}t |ƒdkrX|d }|  |¡sJt|ƒ‚|  ||¡ nB|  |d ¡srt|d ƒ‚|  |d ¡}| d |dd… ¡|¡ dS )z5 Sets the attributes associated with a named object. r   r   r   N)r	   Ú_set_attributesr   r   r   r   Úset_attributesr   r   r   r   r   r   D   s    

ÿzDirContext.set_attributesc                 C   sB   ||   ¡ | jƒ}|   ¡ D ]$}|j}t|tƒr| | |¡¡ q|S )aI  Find bindings with attributes matching criteria in visitor.

        Visitor is a function that is passed the bindings for each level of the
        heirarchy and the attribute dictionary for those bindings.  The visitor
        examines the bindings and dictionary and returns the bindings it is
        interested in.

        )Zlist_bindingsÚ_attributesÚobjÚ
isinstancer   ÚextendÚfind_bindings)r   ÚvisitorZbindingsZbindingr   r   r   r   r   e   s    

zDirContext.find_bindingsc                 C   s   | j  |i ¡}| ¡ S )z6 Returns the attributes of an object in this context. )r   Ú
setdefaultÚcopy©r   r   r   r   r   r   r
   }   s    zDirContext._get_attributesc                 C   s   || j |< dS )z3 Sets the attributes of an object in this context. N)r   r!   r   r   r   r   „   s    zDirContext._set_attributesc                    s&   t t| ƒ |¡ || jv r"| j|= dS )z# Unbinds a name from this context. N)Úsuperr   Ú_unbindr   ©r   r   ©Ú	__class__r   r   r#   �   s    
zDirContext._unbindc                    s8   t t| ƒ ||¡ || jv r4| j| | j|< | j|= dS )z$ Renames an object in this context. N)r"   r   Ú_renamer   )r   Zold_nameÚnew_namer%   r   r   r'   •   s    
zDirContext._renamec                    s&   t t| ƒ |¡ || jv r"| j|= dS )z) Destroys a sub-context of this context. N)r"   r   Ú_destroy_subcontextr   r$   r%   r   r   r)   ž   s    
zDirContext._destroy_subcontext)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r
   r   r#   r'   r)   Ú__classcell__r   r   r%   r   r      s   "!		r   N)r-   Z
traits.apir   Úcontextr   Ú	exceptionr   r   r   r   r   r   Ú<module>
   s   