a
    ¨ˆcÌ  ã                   @   sN   d Z ddlZddlmZ ddlmZ ddlmZ e e	¡Z
G dd„ deƒZdS )	aÂ   Provider of a framework that dynamically determines the contents of a
    context at the time of interaction with the contents rather than at the
    time a class is written.

    This capability is particularly useful when the object acting as a context
    is part of a plug-in application -- such as Envisage.  In general, this
    capability allows the context to be:

    - Extendable by contributions from somewhere other than the original
      code writer
    - Dynamic in that the elements it is composed of can change each time
      someone interacts with the contents of the context.

    It should be noted that this capability is explicitly different from
    contexts that look at another container to determine their contents, such
    as a file system context!

    Users of this framework contribute items to a dynamic context by adding
    traits to the dynamic context instance.  (This addition can happen
    statically through the use of a Traits Category.)  The trait value is the
    context item's value and the trait definition's metadata determines how the
    item is treated within the context.  The support metadata is:

    context_name: A non-empty string
        Represents the name of the item within this context.  This must be
        present for the trait to show up as a context item though the value
        may change over time as the item gets bound to different names.
    context_order: A float value
        Indicates the position for the item within this context.  All
        dynamically contributed context items are sorted by ascending order
        of this value using the standard list sort function.
    is_context: A boolean value
        True if the item is itself a context.
é    Né   ©ÚBinding)ÚContext)ÚOperationNotSupportedErrorc                   @   sX   e Zd Z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dS )ÚDynamicContextae  A framework that dynamically determines the contents of a context at
    the time of interaction with the contents rather than at the time a
    context class is written.

    It should be noted that this capability is explicitly different from
    contexts that look at another container to determine their contents,
    such as a file system context!
    c                 C   s   |   |¡}|dk}|S )z Is a name bound in this context?©NN©Ú_get_contributed_context_item)ÚselfÚnameÚitemÚresult© r   úb/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/apptools/naming/dynamic_context.pyÚ	_is_boundK   s    
zDynamicContext._is_boundc                 C   s.   |   |¡}|dkr&|\}}|jdu }nd}|S )z-Returns True if a name is bound to a context.r   TF)r
   Z
is_context©r   r   r   ÚobjÚtraitr   r   r   r   Ú_is_contextS   s    
zDynamicContext._is_contextc                    s   ‡ fdd„ˆ   ¡ D ƒ}|S )z#Lists the bindings in this context.c                    s    g | ]\}}}t ||ˆ d �‘qS ))r   r   Úcontextr   ©Ú.0ÚnÚoÚt©r   r   r   Ú
<listcomp>`   s   ÿz1DynamicContext._list_bindings.<locals>.<listcomp>©Ú_get_contributed_context_items©r   r   r   r   r   Ú_list_bindings^   s    
þzDynamicContext._list_bindingsc                 C   s   dd„ |   ¡ D ƒ}|S )z&Lists the names bound in this context.c                 S   s   g | ]\}}}|‘qS r   r   r   r   r   r   r   i   ó    z.DynamicContext._list_names.<locals>.<listcomp>r   r    r   r   r   Ú_list_namesg   s    zDynamicContext._list_namesc                 C   s(   |   |¡}|dkr |\}}|}nd}|S )z Looks up a name in this context.r   Nr	   r   r   r   r   Ú_lookupm   s    
zDynamicContext._lookupc                 C   s0   |   |¡}|dkr"|\}}||_n
td|ƒ‚dS )z"Renames an object in this context.r   zName "%s" not in contextN)r
   Úcontext_nameÚ
ValueError)r   Zold_nameÚnew_namer   r   r   r   r   r   Ú_renamex   s
    
zDynamicContext._renamec                 C   s&   |   |¡}|dkr"tdd|  ƒ‚dS )z!Unbinds a name from this context.r   zUnable to unbind zbuilt-in with name [%s]N)r
   r   )r   r   r   r   r   r   Ú_unbind‚   s
    

ÿzDynamicContext._unbindc                 C   s,   d}|   ¡ D ]\}}}||kr||f}q|S )zÕIf the specified name matches a contributed context item then
        returns a tuple of the item's current value and trait definition
        (in that order.)  Otherwise, returns a tuple of (None, None).
        r   r   )r   r   r   r   r   r   r   r   r   r
   ‘   s
    
z,DynamicContext._get_contributed_context_itemc                    sL   ddd„ i}ˆ j f i |¤Ž}dd„ | ¡ D ƒ}| ¡  ‡ fdd„|D ƒ}|S )zÄReturns an ordered list of items to be treated as part of our
        context.

        Each item in the list is a tuple of its name, object, and trait
        definition (in that order.)
        r%   c                 S   s   | d uot | ƒdkS )Nr   )Úlen)Úvr   r   r   Ú<lambda>§   r"   z?DynamicContext._get_contributed_context_items.<locals>.<lambda>c                 S   s   g | ]\}}|j ||f‘qS r   )Zcontext_order)r   r   r   r   r   r   r   «   r"   zADynamicContext._get_contributed_context_items.<locals>.<listcomp>c                    s$   g | ]\}}}|j tˆ |ƒ|f‘qS r   )r%   Úgetattr)r   Úorderr   r   r   r   r   r   ¯   s   )ÚtraitsÚitemsÚsort)r   Úfilterr/   r   r   r   r   r   ž   s    	
ÿz-DynamicContext._get_contributed_context_itemsN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r!   r#   r$   r(   r)   r
   r   r   r   r   r   r   ;   s   	
r   )r6   ÚloggingZbindingr   r   r   Ú	exceptionr   Ú	getLoggerr3   Úloggerr   r   r   r   r   Ú<module>   s   $
