a
    gDf:                     @   s   d Z ddlZddlZddlZddlZddlZddlmZ ddlm	Z	 dZ
dZdZdZd	ZG d
d dZdZed7 ZdadddZG dd dZdd Zede dS )an  
Optional IPython extension for working with Parameters.

This extension offers extended but completely optional functionality
for IPython users.  From within IPython, it may be loaded using:

%load_ext param.ipython

This will register the %params line magic to allow easy inspection of
all the parameters defined on a parameterized class or object:

%params <parameterized class or object>

All parameters of the class or object will be listed in the IPython
pager together with all their corresponding attributes and
docstrings. Note that the class or object to be inspected must already
exist in the active namespace.
    N)register_display_accessor)async_executorFz[1;31m%s[0mz[1;34m%s[0mz[1;32m%s[0mz[1;36m%s[0mc                   @   sP   e Zd ZdZdddZdddZdd	d
Zdd ZdddZdd Z	dd Z
dS )
ParamPagerz}
    Callable class that displays information about the supplied
    Parameterized object or class in the IPython pager.
    Fc                 C   s   g d| _ || _dS )z
        If metaclass is set to True, the checks for Parameterized
        classes objects are disabled. This option is for use in
        ParameterizedMetaclass for automatic docstring generation.
        )namechangedvaluetypeboundsmodeN)order	metaclass)selfr    r   Z/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/param/ipython.py__init__/   s    
zParamPager.__init__Tc                    s   t |jd}t|tr6g }dd | D }| n&t|jjdd }|j }|j	 |sv fdd| D }|
d |||fS )z
        Get the parameter dictionary, the list of modifed parameters
        and the dictionary of parameter values. If include_super is
        True, parameters are also collected from the super classes.
        existingc                 S   s   i | ]\}}||j qS r   )default).0kpr   r   r   
<dictcomp>D       z-ParamPager.get_param_info.<locals>.<dictcomp>T)Zonlychangedc                    s    i | ]\}}| j v r||qS r   )__dict__)r   r   vZ
self_classr   r   r   L   s   
r   )dictparamZobjects
isinstancer   itemslistvalueskeys	__class__pop)r   objinclude_superparamsr   val_dictr   r   r   get_param_info:   s    


zParamPager.get_param_infod   c                    s  |\}}}g }g }|  |D ]$}	|r.|	|vr.q||	||	 f qtdd |D d  t|D ]\}
\}	}d|	 }t|jdu rdn|j}tr|dst	|
 dkrtjd	|	 |dr|dd }q|
 }t	|dkr fd
d|dd D }| |d  g| }n(t	|dkrB| |d  g}ng }|
d rf|dd |D  q`|dd |D  q`d|S )z
        Build a string to that presents all of the parameter
        docstrings in a clean format (alternating red and blue for
        readability).
        c                 s   s   | ]\}}t |V  qd S N)len)r   r   _r   r   r   	<genexpr>b   r   z.ParamPager.param_docstrings.<locals>.<genexpr>   z%s: Nz< No docstring available >
   zQMulti-line docstring for %r is incorrectly formatted  (should start with newline)c                    s   g | ]}d    | qS ) r   )r   lineZright_shiftr   r   
<listcomp>r   r   z/ParamPager.param_docstrings.<locals>.<listcomp>r   c                 S   s   g | ]}t | qS r   )redr   elr   r   r   r4   z   r   c                 S   s   g | ]}t | qS r   )bluer6   r   r   r   r4   |   r   )sort_by_precedenceappendmax	enumeratetextwrapdedentdocWARN_MISFORMATTED_DOCSTRINGS
startswithr+   
splitlinesr   mainwarningljustextendjoin)r   infomax_col_lenonly_changedr&   r'   r   contentsZdisplayed_paramsr   ir   headingZ
unindentedlinestailZ	all_linesr   r3   r   param_docstringsS   s@    


zParamPager.param_docstringsc                    sX   dd    D }dd }t||d}tj||d}dd |D } fdd|D }|S )z
        Sort the provided dictionary of parameters by their precedence value,
        preserving the original ordering for parameters with the
        same precedence.
        c                 S   s   g | ]\}}||fqS r   r   )r   r   Zpobjr   r   r   r4      r   z1ParamPager.sort_by_precedence.<locals>.<listcomp>c                 S   s   | d j d ur| d j S dS )Nr0   g:0yE>)
precedence)xr   r   r   <lambda>   r   z/ParamPager.sort_by_precedence.<locals>.<lambda>)keyc                 S   s   g | ]\}}t |qS r   )r   )r   r,   grpr   r   r   r4      r   c                    s6   g | ].}|D ]$}|d  dks(|d   v r|d  qqS )r   r   r   )r   groupr7   
parametersr   r   r4      s   )r   sorted	itertoolsgroupby)r   rX   r&   Zkey_fnZsorted_paramsgroupsZordered_groupsZordered_paramsr   rW   r   r9      s    zParamPager.sort_by_precedence(   c                 C   s  g i  }}|\}}}	dd |D }
|  |}|D ]}|| }|rL||	vrLq0|jrVdnd}|jrddnd}t|dr||jr|dnd	}| d
| | }t|| }t||d kr|d|d  d }||jj||d}t|dr|j	du rdn|j	\}}d\}}t|drP|
 \}}|du r4|dur4|}d}|du rP|durP|}d}||fdkr||f||< d| d| d|d< |D ]&}t|
| t|| g}||
|< q|||f q0| ||
|	||S )z
        Collect the information about parameters needed to build a
        properly formatted table and then tabulate it.
        c                 S   s   i | ]
}|d qS )r   r   )r   r   r   r   r   r      r   z+ParamPager._build_table.<locals>.<dictcomp>CVZROZRW
allow_Nonez AN r1      Nz...)r   r   r
   r   r	   )NN)FFget_soft_boundsT(z, ))r9   constantreadonlyhasattrr`   reprr+   r"   __name__r	   rc   r;   r:   	_tabulate)r   rH   r   rI   rJ   	info_listbounds_dictr&   r'   r   
col_widthsZorderingr   r   rf   rg   r`   r
   r   Zp_dictlbounduboundmark_lboundmark_uboundZsoft_lboundZsoft_uboundcol	max_widthr   r   r   _build_table   sJ    



zParamPager._build_tablec                    s  g g  }}dd |D   fdd|D }g }	t |D ]B\}
}|| d }| }|
dkrd||n||}|	| q6|td|	 d  |D ]\}}g }t |D ]\}
}|| d }||v r|| nd}|
dkr||n||}|d	kr||d
r|| \}}|d\}}|d\}}|d\}}|rT|d t	|  n|}|rnt	| d | n|}| d| }|| qd|}||v rt
| }|| qd|| S )a  
        Returns the supplied information as a table suitable for
        printing or paging.

        info_list:  List of the parameters name, type and mode.
        col_widths: Dictionary of column widths in characters
        changed:    List of parameters modified from their defaults.
        order:      The order of the table columns
        bound_dict: Dictionary of appropriately formatted bounds
        c                 S   s   h | ]\}}|D ]}|qqS r   r   )r   r,   rowr   r   r   r   	<setcomp>   r   z'ParamPager._tabulate.<locals>.<setcomp>c                    s   g | ]}| v r|qS r   r   )r   rs   Z
column_setr   r   r4      r   z(ParamPager._tabulate.<locals>.<listcomp>r.   r   ra   r/   r	   F,rd   re   )r<   
capitalizerE   centerr:   r8   rG   getrsplitcyanr5   )r   rl   rn   r   r   rm   rK   rO   columnsZ	title_rowrL   rs   width	formattedrv   rH   Zrow_listvalrq   rr   ZlvalZuvalZlspaceZlstrZustruspacero   rp   Zrow_textr   rx   r   rk      s<    


zParamPager._tabulatec                 C   sn  d}| j st|tj}t|to*t|tj}|s@|s@td dS |r|jj}t	
d| d |j}|rjdnd|j }d|d| }|du rd	|j }d
t| }| d| d}	| j|dd}
|
d st|	 }| dS | j|
| jddd}| j|
ddd}d}t|	 }|dt|  7 }|dtd  7 }|d7 }d}	|	 dd
t|	  }t| }| d| d| d| S )z
        Given a Parameterized object or class, display information
        about the parameters in the IPython pager.
        Nz.Object is not a Parameterized class or object.^z[0-9]+$ra   z %rzParameters of z	 instancezParameters of %r=r/   T)r%   r   z
Object has no parameters.r]   F)rI   rJ   r)   z?Parameters changed from their default values are marked in red.z
%sz%Soft bound values are marked in cyan.zB
C/V= Constant/Variable, RO/RW = ReadOnly/ReadWrite, AN=Allow NonezParameter docstrings:z

)r   r   r   ZParameterizedr   
issubclassprintr"   rj   rematchr   r+   r(   greenru   r   rP   r5   r~   )r   Z	param_objtitleZparameterized_objectZparameterized_class
class_namedefault_nameZobj_nameZheading_lineZheading_textZ
param_infoZtop_headingtableZ
docstringsZdflt_msgZheading_stringZdocstring_headingr   r   r   __call__   sF    



zParamPager.__call__N)F)T)r)   F)r]   F)rj   
__module____qualname____doc__r   r(   rP   r9   ru   rk   r   r   r   r   r   r   )   s   


.
84r   zDWelcome to the param IPython extension! (https://param.holoviz.org/)z
Available magics: %paramsTc                    s\   ddl m}m}m  ddlm |G  fddd|}|rFtt tsXda| 	| d S )Nr   )Magicsmagics_class
line_magicpagec                       s2   e Zd ZdZ fddZdfdd	Z  ZS )	z+load_ipython_extension.<locals>.ParamMagicsz~
        Implements the %params line magic used to inspect the parameters
        of a parameterized class or object.
        c                    s   t  j|i | t | _d S r*   )superr   r   param_pager)r   argskwargsr"   r   r   r   ?  s    z4load_ipython_extension.<locals>.ParamMagics.__init__ra   Nc                    sP   |dkrt d dS | j|}|jdu r:t d|  dS  | |j dS )aj  
            The %params line magic accepts a single argument which is a
            handle on the parameterized object to be inspected. If the
            object can be found in the active namespace, information about
            the object's parameters is displayed in the IPython pager.

            Usage: %params <parameterized class or object>
            ra   z$Please specify an object to inspect.NFz%Object %r not found in the namespace.)r   shellZ_object_findfoundr   r   r$   )r   Zparameter_s
namespacesr$   r   r   r   r&   D  s    

z2load_ipython_extension.<locals>.ParamMagics.params)ra   N)rj   r   r   r   r   r&   __classcell__r   r   r   r   r   ParamMagics9  s   r   T)
ZIPython.core.magicr   r   r   ZIPython.corer   r   message_loadedZregister_magics)ipverboser   r   r   r   r   r   load_ipython_extension3  s    !r   c                   @   s$   e Zd ZdZdZdd Zdd ZdS )IPythonDisplayz;
    Reactive display handler that updates the output.
    Tc                 C   s
   || _ d S r*   )	_reactive)r   Zreactiver   r   r   r   j  s    zIPythonDisplay.__init__c                    s   ddl m} ddlm}m} ddlm} d t| j|rb| jj	 || jj
ddi fdd}n&| j || ddi fd	d}z(  }||u rd }t|t jd
W n ty   tY n0 d S )Nr   )depends)	Undefinedresolve_ref)rxZwatchTc                     s   d ur    d S r*   updater   r   cbhandler   r   update_handleu  s    z.IPythonDisplay.__call__.<locals>.update_handlec                     s   d ur    d S r*   r   r   r   r   r   r   {  s    )Z
display_id)Zparam.dependsr   Zparam.parameterizedr   r   Zparam.reactiver   r   r   Z	_callback_paramsZdisplayuuiduuid4hex	TypeErrorNotImplementedError)r   r   r   r   r   r   r$   r   r   r   r   m  s$    zIPythonDisplay.__call__N)rj   r   r   r   enabledr   r   r   r   r   r   r   c  s   r   c              	   C   sx   d }zPt  }|jrRddlm} | }|j}| r@||  n||   W d S W n t	t
fyj   Y n0 t|  d S )Nr   )IOLoop)Zget_ipythonkernelZtornado.ioloopr   currentZasyncio_loopZ
is_runningZadd_callbackZrun_until_complete	NameErrorAttributeErrorr   )funcZ
event_loopr   r   Zioloopr   r   r   ipython_async_executor  s    
r   Z_ipython_display_)T)r   r   rZ   r=   r   r   Zparam.displayr   Zparam._utilsr   r@   r5   r8   r   r~   r   r   r   r   r   r   r   r   r   r   <module>   s,     
0$