a
    ¨ˆc2W  ã                   @   s¨   d Z ddlmZmZmZmZ ddlmZmZ ddlm	Z	 ddl
mZmZ ddl
mZmZ ddlmZ dd	lmZ dd
lmZ dZdZdZeg eg iZG dd„ deƒZdS )ú) The base class for all naming contexts. é    )ÚAnyÚDictÚEventÚ	HasTraits)ÚPropertyÚStré   )ÚBinding)ÚInvalidNameErrorÚNameAlreadyBoundError)ÚNameNotFoundErrorÚNotContextError)ÚNamingEvent)Únaming_manager)Úmake_unique_namezapptools.naming.factory.initialzapptools.naming.factory.objectzapptools.naming.factory.statec                   @   sV  e Zd ZdZeZeZeZeeƒZ	e
eƒZeeƒZeeƒZeeƒZeeƒZeeƒZeeeƒZdd„ ZdAdd„ZdBd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 dCdd„Z!dDd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*d.d/„ Z+d0d1„ Z,d2d3„ Z-d4d5„ Z.d6d7„ Z/d8d9„ Z0d:d;„ Z1d<d=„ Z2d>d?„ Z3d@S )EÚContextr   c                 C   s   dS )až  
        Return the name of the context within its own namespace.

        That is the full-path, through the namespace this context participates
        in, to get to this context.  For example, if the root context of the
        namespace was called 'Foo', and there was a subcontext of that called
        'Bar', and we were within that and called 'Baz', then this should
        return 'Foo/Bar/Baz'.

        Ú © ©Úselfr   r   úZ/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/apptools/naming/context.pyÚ_get_namespace_nameV   s    zContext._get_namespace_nameFc                 C   sÄ   t |ƒdkrtdƒ‚|  |¡}t |ƒdkrh|d }|  |¡rDt|ƒ‚|  ||¡ tt||| d�d�| _nX|  |d ¡s–|rŠ|  	|d ¡ nt
|d ƒ‚|  |d ¡}| d |dd… ¡||¡ dS )z’Binds a name to an object.

        If 'make_contexts' is True then any missing intermediate contexts are
        created automatically.

        r   ú
empty namer	   ©ÚnameÚobjÚcontext©Únew_bindingú/N)Úlenr   Ú_parse_nameÚ	_is_boundr   Ú_bindr   r
   Úobject_addedÚ_create_subcontextr   Ú_get_next_contextÚbindÚjoin)r   r   r   Úmake_contextsÚ
componentsÚatomÚnext_contextr   r   r   r(   r   s"    

ÿ
zContext.bindc                 C   s®   t |ƒdkrtdƒ‚|  |¡}t |ƒdkrR|  |d |¡ tt||| d�d�| _nX|  |d ¡s€|rt|  |d ¡ nt	|d ƒ‚|  
|d ¡}| d |dd… ¡||¡ dS )aÝ  Binds an object to a name that may already be bound.

        If 'make_contexts' is True then any missing intermediate contexts are
        created automatically.

        The object may be a different object but may also be the same object
        that is already bound to the specified name. The name may or may not be
        already used. Think of this as a safer version of 'bind' since this
        one will never raise an exception regarding a name being used.

        r   r   r	   r   r   r    N)r!   r   r"   Ú_rebindr   r
   Úobject_changedr#   r&   r   r'   Úrebindr)   )r   r   r   r*   r+   r-   r   r   r   r0   �   s    
ÿ
zContext.rebindc                 C   s´   t |ƒdkrtdƒ‚|  |¡}t |ƒdkrp|d }|  |¡sDt|ƒ‚|  |¡}|  |¡ tt||| d�d�| _	n@|  |d ¡sŠt|d ƒ‚|  
|d ¡}| d |dd… ¡¡ dS )z Unbinds a name. r   r   r	   r   ©Úold_bindingr    N)r!   r   r"   r#   r   Ú_lookupÚ_unbindr   r
   Úobject_removedr'   Úunbindr)   ©r   r   r+   r,   r   r-   r   r   r   r6   Ç   s     



ÿ
zContext.unbindc                 C   sÎ   t |ƒdkst |ƒdkr tdƒ‚|  |¡}|  |¡}t |ƒdkrªt |ƒdkrª|  |¡s^t|ƒ‚|  |¡rpt|ƒ‚|  ||¡ |  |¡}tt	||| d�t	||| d�d�| _
n |  |¡}|  ||¡ |  |¡ dS )z  Binds a new name to an object. r   r   r	   r   )r2   r   N)r!   r   r"   r#   r   r   Ú_renamer3   r   r
   Úobject_renamedÚlookupr(   r6   )r   Úold_nameÚnew_nameZold_componentsZnew_componentsr   r   r   r   Úrenameì   s$    




þ

zContext.renamec                 C   s�   t |ƒdkr| S |  |¡}t |ƒdkrL|d }|  |¡s@t|ƒ‚|  |¡}n@|  |d ¡sft|d ƒ‚|  |d ¡}| d |dd… ¡¡}|S )z+ Resolves a name relative to this context. r   r	   r    N)r!   r"   r#   r   r3   r'   r:   r)   r7   r   r   r   r:     s    

zContext.lookupc                 C   s”   t |ƒdkrtdƒ‚|  |¡}t |ƒdkrP|d }|  |¡sDt|ƒ‚|  |¡}n@|  |d ¡sjt|d ƒ‚|  |d ¡}| d |dd… ¡¡}|S )z; Looks up the binding for a name relative to this context. r   r   r	   r    N)	r!   r   r"   r#   r   Ú_lookup_bindingr'   Úlookup_bindingr)   )r   r   r+   r,   Úbindingr-   r   r   r   r?   >  s    

zContext.lookup_bindingc                 C   s�   t |ƒdkr| S |  |¡}t |ƒdkrL|d }|  |¡s@t|ƒ‚|  |¡}n@|  |d ¡sft|d ƒ‚|  |d ¡}| d |dd… ¡¡}|S )z`Resolves a name relative to this context.

        The name MUST resolve to a context.

        r   r	   r    N)r!   r"   r#   r   r'   r:   r)   r7   r   r   r   Úlookup_context]  s    

zContext.lookup_contextc                 C   sª   t |ƒdkrtdƒ‚|  |¡}t |ƒdkrf|d }|  |¡rDt|ƒ‚|  |¡}tt||| d�d�| _n@|  |d ¡s€t	|d ƒ‚|  
|d ¡}| d |dd… ¡¡}|S )z Creates a sub-context. r   r   r	   r   r   r    N)r!   r   r"   r#   r   r&   r   r
   r%   r   r'   Úcreate_subcontextr)   )r   r   r+   r,   Úsubr-   r   r   r   rB   ‚  s     


ÿ
zContext.create_subcontextc                 C   sÆ   t |ƒdkrtdƒ‚|  |¡}t |ƒdkr‚|d }|  |¡sDt|ƒ‚|  |¡}|  |¡s`t|ƒ‚|  |¡ t	t
||| d�d�| _n@|  |d ¡sœt|d ƒ‚|  |d ¡}| d |dd… ¡¡ dS )z Destroys a sub-context. r   r   r	   r   r1   r    N)r!   r   r"   r#   r   r3   Ú_is_contextr   Ú_destroy_subcontextr   r
   r5   r'   Údestroy_subcontextr)   r7   r   r   r   rF   ¦  s$    




ÿ
zContext.destroy_subcontextc                 C   s   t ||  d¡dd�S )z|Returns a name that is unique within the context.

        The name returned will start with the specified prefix.

        r   z%s (%d))ÚexistingÚformat)r   Ú
list_names)r   Úprefixr   r   r   Úget_unique_nameÌ  s    ÿzContext.get_unique_namer   c                 C   sd   t |ƒdkr|  ¡ }nJ|  |¡}|  |d ¡s:t|d ƒ‚|  |d ¡}| d |dd… ¡¡}|S )z% Lists the names bound in a context. r   r    r	   N)r!   Ú_list_namesr"   r#   r   r'   rI   r)   )r   r   Únamesr+   r-   r   r   r   rI   ×  s    

zContext.list_namesc                 C   sd   t |ƒdkr|  ¡ }nJ|  |¡}|  |d ¡s:t|d ƒ‚|  |d ¡}| d |dd… ¡¡}|S )z" Lists the bindings in a context. r   r    r	   N)r!   Ú_list_bindingsr"   r#   r   r'   Úlist_bindingsr)   )r   r   Úbindingsr+   r-   r   r   r   rO   ë  s    

zContext.list_bindingsc                 C   s’   t |ƒdkrd}n||  |¡}t |ƒdkrN|d }|  |¡sBt|ƒ‚|  |¡}n@|  |d ¡sht|d ƒ‚|  |d ¡}| d |dd… ¡¡}|S )z1 Returns True if the name is bound to a context. r   Tr	   r    N)r!   r"   r#   r   rD   r'   Ú
is_contextr)   )r   r   rQ   r+   r,   r-   r   r   r   rQ      s    

zContext.is_contextc                 C   s(   |du rg S g }g }|   |||i ¡ |S )z: Returns a list of namespace names that are bound to obj. N)Ú_search)r   r   rM   Úpathr   r   r   Úsearch!  s    zContext.searchc                 C   s
   |  d¡S )zfParse a name into a list of components.

        e.g. 'foo/bar/baz' -> ['foo', 'bar', 'baz']

        r    )Úsplit©r   r   r   r   r   r"   6  s    zContext._parse_namec                 C   s
   || j v S )z" Is a name bound in this context? ©Ú	_bindingsrV   r   r   r   r#   ?  s    zContext._is_boundc                 C   s   | j | }t ||| ¡S )z" Looks up a name in this context. )rX   r   Zget_object_instance©r   r   r   r   r   r   r3   D  s    
zContext._lookupc                 C   s   t ||  |¡| d�S )z2 Looks up the binding for a name in this context. r   )r
   r3   rV   r   r   r   r>   K  s    zContext._lookup_bindingc                 C   s   t  ||| ¡}|| j|< dS )z, Binds a name to an object in this context. N)r   Zget_state_to_bindrX   )r   r   r   Ústater   r   r   r$   P  s    zContext._bindc                 C   s   |   ||¡ dS )z. Rebinds a name to an object in this context. N)r$   rY   r   r   r   r.   V  s    zContext._rebindc                 C   s   | j |= dS )z# Unbinds a name from this context. NrW   rV   r   r   r   r4   [  s    zContext._unbindc                 C   s   | j | | j |< | j |= dS )z$ Renames an object in this context. NrW   )r   r;   r<   r   r   r   r8   `  s    zContext._renamec                 C   s   | j | jd�}|| j|< |S )z( Creates a sub-context of this context. )Úenvironment)Ú	__class__r[   rX   )r   r   rC   r   r   r   r&   i  s    
zContext._create_subcontextc                 C   s   | j |= dS )z) Destroys a sub-context of this context. NrW   rV   r   r   r   rE   q  s    zContext._destroy_subcontextc                 C   s0   g }|   ¡ D ]}| t||  |¡| d�¡ q|S )z% Lists the bindings in this context. r   )rL   Úappendr
   r3   )r   rP   r   r   r   r   rN   v  s    ÿzContext._list_bindingsc                 C   s   t | j ¡ ƒS )z( Lists the names bound in this context. )ÚlistrX   Úkeysr   r   r   r   rL   �  s    zContext._list_namesc                 C   s   |   |¡duS )z/ Returns True if a name is bound to a context. N)r'   rV   r   r   r   rD   †  s    zContext._is_contextc                 C   s&   |   |¡}t|tƒr|}nt|ƒ‚|S )z Returns the next context. )r3   Ú
isinstancer   r   )r   r   r   r-   r   r   r   r'   ‹  s
    

zContext._get_next_contextc                 C   s†   |   ¡ D ]x}|j|u r:| |j¡ | d |¡¡ | ¡  t|jtƒr|j|vr| |j¡ d||j< |j ||||¡ | ¡  qdS )zˆAppend to names any name bound to obj.
        Join path and name with '/' to for a complete name from the
        top context.
        r    TN)	rO   r   r]   r   r)   Úpopr`   r   rR   )r   r   rM   rS   Zsearchedr@   r   r   r   rR   ˜  s    

ÿþ
zContext._searchN)F)F)r   )r   )4Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚINITIAL_CONTEXT_FACTORYÚOBJECT_FACTORIESÚSTATE_FACTORIESr   ÚENVIRONMENTr[   r   r   Znamespace_namer   r   r%   r/   r5   r9   Zcontext_changedr   rX   r   r(   r0   r6   r=   r:   r?   rA   rB   rF   rK   rI   rO   rQ   rT   r"   r#   r3   r>   r$   r.   r4   r8   r&   rE   rN   rL   rD   r'   rR   r   r   r   r   r   (   sT   

+
*%0"%$&

!		r   N)re   Z
traits.apir   r   r   r   r   r   r@   r
   Ú	exceptionr   r   r   r   Znaming_eventr   r   Zunique_namer   rf   rg   rh   ri   r   r   r   r   r   Ú<module>
   s   ý