a
    Df0<                     @   s  d dl Z d dlZd dlmZmZmZmZ d dlmZm	Z	 ddl
mZmZmZmZ ddlmZ ddlmZ zd dlZW n ey   dZY n0 d dlmZmZ d	Zd d
lmZ ee_eG dd deZeG dd deZG dd dZeG dd deZeG dd deZ dd Z!dS )    N)Magicsline_cell_magic
line_magicmagics_class)HTMLdisplay   )OptionsStoreStoreOptionsoptions_policy)InfoPrinter)
Compositor)CompositorSpecOptsSpecF)pagec                   @   s>   e Zd Zedd Zedd Zedd Zed
dd	ZdS )OutputMagicc                 C   s0   t jj}t jjd r,|s,ttj|dd d S )NinfoT)ansi)r
   output_settingsZ_disable_info_outputoptionsr   r   r   )clsobjdisabled r   e/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/holoviews/ipython/magics.pyr   #   s    zOutputMagic.infoc                 C   s   d\}}t jj D ]\\}}| d|}t|t| dkrdt|dkrLdnd|  |d7 }|}q|d| 7 }qt|dkrdnd|  d	S )
z:
        Pretty print the current element options
        ) r   =P   r   %outputz          N)r
   r   r   itemslenprint)r   currentcountkvkeywordr   r   r   pprint)   s    zOutputMagic.pprintc           	         s   |j }|dd}d  dd t|dD }|D ] }| tjjv r2|   qTq2 fddtjj g D }dd |D }|dd tjj	 D  S )	Nr   r   c                 S   s   g | ]}|  D ]}|qqS r   split).0Zelstr   r   r   
<listcomp>B       z0OutputMagic.option_completer.<locals>.<listcomp>r   c                    s$   g | ]}|t jj g vr|qS r   )r
   r   hiddenget)r-   valcompletion_keyr   r   r/   I   s   c                 S   s   g | ]}t |tst|qS r   )
isinstancetuplereprr-   elr   r   r   r/   K   r0   c                 S   s   g | ]}|d  qS r   r   r9   r   r   r   r/   L   r0   )
text_until_cursorreplacereversedr,   stripr
   r   allowedr2   keys)	r   r'   r(   Zraw_linelinetokenstokenvaluesZvreprsr   r4   r   option_completer:   s    zOutputMagic.option_completerNc                    sX   |dkr    td d S  fdd}dd }|r:d}nd}tjj|||||d	 d S )
Nr   z/
For help with the %output magic, call %output?c                    s    j j| td d S )NZstore_history)shellrun_cellSTORE_HISTORY)cellZrendererselfr   r   cell_runnerV   s    z'OutputMagic.output.<locals>.cell_runnerc                 S   s   t td|   d S )Nz<b>Warning:</b> )r   r   )msgr   r   r   warnfnY   s    z"OutputMagic.output.<locals>.warnfnz/For help with the %output magic, call %output?
z1For help with the %%output magic, call %%output?
)rN   help_promptrP   )r*   r$   r
   r   output)rM   rB   rK   rN   rP   rQ   r   rL   r   rR   N   s    zOutputMagic.output)N)	__name__
__module____qualname__classmethodr   r*   rF   r   rR   r   r   r   r   r       s   


r   c                       s8   e Zd ZdZ fddZedd Zedd Z  Z	S )CompositorMagiczq
    Magic allowing easy definition of compositor operations.
    Consult %compositor? for more information.
    c                    s4   t  j|i | dg}d|tjg | jj_d S )Nz9The %compositor line magic is used to define compositors.
)super__init__joinr   __doc__
compositor__func__)rM   argskwargslines	__class__r   r   rZ   m   s    zCompositorMagic.__init__c                 C   sj   |  r^tj|  | jjdD ]<}dd tjD }|jj}|t	
 |d |j < t| qntd d S )Nnsc                 S   s   i | ]}|t  qS r   )r	   )r-   groupr   r   r   
<dictcomp>w   r0   z.CompositorMagic.compositor.<locals>.<dictcomp>.z7For help with the %compositor magic, call %compositor?
)r?   r   parserH   user_nsr	   Z_option_groupsoutput_typerS   r
   r   rf   r   registerr$   )rM   rB   Z
definitionrf   	type_namer   r   r   r]   s   s    zCompositorMagic.compositorc                    s   |j  dd tjD }ddg}t fdd|D }t fdd|D }|sR|S |sZ|S |rld vrldgS  dd	  rd
 vrd
gS d
 v rdgS d S )Nc                 S   s   g | ]}|j d  qS )()rS   r-   opr   r   r   r/      r0   z4CompositorMagic.option_completer.<locals>.<listcomp>datar   c                 3   s   | ]}| v V  qd S Nr   ro   rB   r   r   	<genexpr>   r0   z3CompositorMagic.option_completer.<locals>.<genexpr>c                 3   s   | ]}| v V  qd S rr   r   )r-   moders   r   r   rt      r0   )r    [])r<   r   
operationsanyr,   r?   )r   r'   r(   Zoperation_openersmodesZop_declaredZmode_declaredr   rs   r   rF      s    z CompositorMagic.option_completer)
rS   rT   rU   r\   rZ   r   r]   rV   rF   __classcell__r   r   rb   r   rW   f   s   
rW   c                   @   sP   e Zd ZdZi Zedd Zedd Zedd Zedd	 Z	ed
d Z
dS )OptsCompleterz=
    Implements the TAB-completion for the %%opts magic.
    c                 C   sv   z\t   D ]J}t  d| }|d j}|d j}d|}||rL|ng f| j|< qW n tyn   Y n0 | jS )z'Get the dictionary of valid completionsrh   Zplotstyle)r
   r   rA   r[   Zallowed_keywords_completionsKeyError)r   elementr   ZplotkwsZstylekwsZdottedr   r   r   setup_completer   s    


zOptsCompleter.setup_completerc           
   	      s
  dg   }dd t |dd D }t|D ]\}}g }|dkrR|||  |dkrz|d|d ||d  g |d	kr|d|d ||d	  g t |D ]>}	|	|v r|	  d fd
d|D } |f    S q||v r0||   qq0 |fS )zt
        Supply the appropriate key in Store.options and supply
        suggestions for further completion.
        Nc                 S   s   g | ]}|qS r   r   )r-   r.   r   r   r   r/      r0   z3OptsCompleter.dotted_completion.<locals>.<listcomp>rh   r!   r   r    r   c                    s,   g | ]$}|  d  r|d d  qS )rh   r    )
startswithr,   r-   r'   r5   depthr   r   r/      s   )r>   r=   r,   	enumerateappendr[   r&   )
r   rB   sorted_keyscompositor_defssuggestionsrC   irD   Z
key_checkskeyr   r   r   dotted_completion   s(    
  
zOptsCompleter.dotted_completionc                 C   s   | || | d S )Nr   )r&   )r   rB   openercloserr   r   r   _inside_delims   s    zOptsCompleter._inside_delimsc                 C   s,   |j }|  }dd tjD }| |||S )z.Tab completion hook for the %%opts cell magic.c                 S   s   i | ]}|j r|j |jjqS r   )rf   rk   rS   r9   r   r   r   rg      s   z2OptsCompleter.option_completer.<locals>.<dictcomp>)r<   r   r   Zdefinitionsline_completer)r   r'   r(   rB   completionsr   r   r   r   rF      s    zOptsCompleter.option_completerc           	         s   t | }dd |D }| |||\ }g d}|rT|drT fdd|D S  sl|t|  | S | |ddrdd |  d	 D S | |d
drddgS dd |  d D }| |ddr|S |t|  | S )Nc                 S   s   g | ]}d |vr|qS rh   r   )r-   r   r   r   r   r/      r0   z0OptsCompleter.line_completer.<locals>.<listcomp>)zstyle(zplot[znorm{rh   c                    s   g | ]}  d | qS r   r   r9   r4   r   r   r/      r0   rw   rx   c                 S   s   g | ]}|d  qS r;   r   r-   kwr   r   r   r/      r0   r   {}z	+axiswisez
+framewisec                 S   s   g | ]}|d  qS r;   r   r   r   r   r   r/      r0   r    rn   rv   )sortedrA   r   endswithlistr   )	r   rB   r   r   r   Z	type_keysr   Zverbose_openersZstyle_completionsr   r4   r   r      s     zOptsCompleter.line_completerN)rS   rT   rU   r\   r   rV   r   r   r   rF   r   r   r   r   r   r}      s   



r}   c                   @   sN   e Zd ZdZdZdZdZedd Zedd Z	edd	 Z
edddZdS )	OptsMagiczx
    Magic for easy customising of normalization, plot and style options.
    Consult %%opts? for more information.
    NFc                 C   sB   | j r | jr| j S tj| j  | jdur>t|| j d| _dS )a/  
        To be called by the display hook which supplies the element to
        be displayed. Any customisation of the object can then occur
        before final display. If there is any error, a HTML message
        may be returned. If None is returned, display will proceed as
        normal.
        N)error_messagestrictsysstderrwrite	opts_specr   Zset_options)r   r   r   r   r   process_element   s    	
zOptsMagic.process_elementc                 C   s,   t |\}}t |}|r"|| _|| _d S rr   )r   Zexpand_compositor_keysvalidation_error_messager   r   )r   spec_errmsgr   r   r   register_custom_spec
  s
    
zOptsMagic.register_custom_specc                 C   sn   |du r||fS |gg  }}|  D ]4}| drN|| dd  q$|| q$d|d|fS )z
        Check the code for additional use of %%opts. Enables
        multi-line use of %%opts in a single call to the magic.
        N%%opts   r!   rX   )
splitlinesr?   r   r   r[   )r   rB   rK   specscoder   r   r   _partition_lines  s    zOptsMagic._partition_linesr   c           
      C   sN  |  ||\}}ztj|| jjd}W n  tyF   ttd Y dS 0 t }t	
 D ]}|tt	|jO }qVdd | D }|| }|rd}tt|jd|d |r| | | jj|td	 nxt|}	|	rdt_tj|	 | jrttd
 dS tddd  t|t	  W d   n1 s:0    Y  dt_dS )a  
        The opts line/cell magic with tab-completion.

        %%opts [ [path] [normalization] [plotting options] [style options]]+

        path:             A dotted type.group.label specification
                          (e.g. Image.Grayscale.Photo)

        normalization:    List of normalization options delimited by braces.
                          One of | -axiswise | -framewise | +axiswise | +framewise |
                          E.g. { +axiswise +framewise }

        plotting options: List of plotting option keywords delimited by
                          square brackets. E.g. [show_title=False]

        style options:    List of style option keywords delimited by
                          parentheses. E.g. (lw=10 marker='+')

        Note that commas between keywords are optional (not
        recommended) and that keywords must end in '=' without a
        separating space.

        More information may be found in the class docstring of
        util.parser.OptsSpec.
        rd   zE<b>Invalid syntax</b>: Consult <tt>%%opts?</tt> for more information.Nc                 S   s   h | ]}| d d qS )rh   r   r+   r   r   r   r   	<setcomp>I  r0   z!OptsMagic.opts.<locals>.<setcomp>zZ<b>WARNING:</b> Unknown elements {unknown} not registered with any of the loaded backends.z, )unknownrG   z*Options specification will not be applied.TF)Zskip_invalidZwarn_on_skip)r   r   ri   rH   rj   SyntaxErrorr   r   setr
   Zloaded_backendsr   childrenrA   formatr[   r   rI   rJ   r   r   r   r   r   r   r   r   r   Zapply_customizations)
rM   rB   rK   r   Zavailable_elementsbackendZspec_elementsZunknown_elementsrO   r   r   r   r   opts"  s6    

0zOptsMagic.opts)r   N)rS   rT   rU   r\   r   r   r   rV   r   r   r   r   r   r   r   r   r   r      s   


r   c                   @   s:   e Zd ZdZdZedd Zedd Ze	d
dd	Z
dS )
TimerMagicz
    A line magic for measuring the execution time of multiple cells.

    After you start/reset the timer with '%timer start' you may view
    elapsed time with any subsequent calls to %timer.
    Nc                  C   s4   t   tj } | d }|d }d||d | d f S )N<   zTimer elapsed: %02d:%02d:%02d)timer   
start_time)secondsminuteshoursr   r   r   elapsed_timen  s    zTimerMagic.elapsed_timec                 C   s   dgS )Nstartr   )r   r'   r(   r   r   r   rF   u  s    zTimerMagic.option_completerr   c                 C   sn   |  dvrtd dS |  dkrJt t_td}td|  dS | jdu r^td nt|   dS )a  
        Timer magic to print initial date/time information and
        subsequent elapsed time intervals.

        To start the timer, run:

        %timer start

        This will print the start date and time.

        Subsequent calls to %timer will print the elapsed time
        relative to the time when %timer start was called. Subsequent
        calls to %timer start may also be used to reset the timer.
        )r   r   z@Invalid argument to %timer. For more information consult %timer?Nr   z%Y/%m/%d %H:%M:%SzTimer start: zJPlease start timer with %timer start. For more information consult %timer?)r?   r$   r   r   r   strftimer   )rM   rB   	timestampr   r   r   timery  s    



zTimerMagic.timer)r   )rS   rT   rU   r\   r   staticmethodr   rV   rF   r   r   r   r   r   r   r   c  s   

r   c                 C   s   |  t |  t tj }|tj_td u r8t	d n
|  t
 td u rTt	d n
|  t | jdtjdd | jdtjdd | jdtjdd | jdtjdd t  | jdtjd	d | jdtjd
d d S )Nz6%opts magic unavailable (pyparsing cannot be imported)z<%compositor magic unavailable (pyparsing cannot be imported)Zcomplete_commandz%timer)Zstr_keyz%compositorr   z%%outputr   z%opts)Zregister_magicsr   r   r
   r   Z_generate_docstringrR   r\   	pyparsingr$   r   rW   Zset_hookrF   r}   r   )ip	docstringr   r   r   load_magics  s    




r   )"r   r   ZIPython.core.magicr   r   r   r   ZIPython.displayr   r   Zcore.optionsr	   r
   r   r   Zcore.pprintr   Z	operationr   r   ImportErrorZholoviews.util.parserr   r   rJ   ZIPython.corer   storer   rW   r}   r   r   r   r   r   r   r   <module>   s0   
E-Yu3