
    e0<                        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 	 d dlZd dlmZmZ n# e$ r dZY nw xY wd	Zd d
lmZ ee_        e G d de                      Ze G d de                      Z G d d          Ze G d de                      Ze G d de                      Z 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                   h    e Zd Zed             Zed             Zed             Zedd            ZdS )OutputMagicc                     t           j        j        }t           j        j        d         r,|s,t	          j        t          j        |d                     d S d S d S )NinfoT)ansi)r   output_settings_disable_info_outputoptionsr   r   r   )clsobjdisableds      8lib/python3.11/site-packages/holoviews/ipython/magics.pyr   zOutputMagic.info#   sa    (= (0 	8 	8Ik&s66677777	8 	8 	8 	8    c                 >   d\  }}t           j        j                                        D ]Y\  }}| d|}t	          |          t	          |          z   dk    r"t          |dk    rdnd|z              |dz  }|}Q|d|z   z  }Zt          |dk    rdnd|z              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keywords         r   pprintzOutputMagic.pprint)   s    
 (06688 		D 		DCAallQllG7||c'll*R//E1HHyy(wFGGG
!3<'q99h7BCCCCCr   c                   	 |j         }|                    dd          }d 	d t          |                    d                    D             }|D ]=}|                                t
          j        j        v r|                                	 n>	fdt
          j        j                            	g           D             }d |D             }|d t
          j        j        	                                D             z   S )Nr$   r!   c                 @    g | ]}|                                 D ]}|S  split).0elsts      r   
<listcomp>z0OutputMagic.option_completer.<locals>.<listcomp>B   s-    NNN#))++NNQ!NNNNr   r"   c                 b    g | ]+}|t           j        j                            g           v)|,S r2   )r   r   hiddenget)r5   valcompletion_keys     r   r8   z0OutputMagic.option_completer.<locals>.<listcomp>I   sG     V V V# 5 < @ @QS T TTT TTTr   c                 V    g | ]&}t          |t                    t          |          'S r2   )
isinstancetuplereprr5   els     r   r8   z0OutputMagic.option_completer.<locals>.<listcomp>K   s-    IIIr:b%3H3HI$r((IIIr   c                     g | ]}|d z   S r"   r2   rB   s     r   r8   z0OutputMagic.option_completer.<locals>.<listcomp>L   s    OOOBCOOOr   )
text_until_cursorreplacereversedr4   stripr   r   allowedr;   keys)
r   r,   r-   raw_linelinetokenstokenvaluesvreprsr=   s
            @r   option_completerzOutputMagic.option_completer:   s   &	"-- NNx

388NNN 	 	E{{}} 5 ===!& >V V V V!6!>!B!B>SU!V!V V V VIIVIIIOO%*?*G*L*L*N*NOOOOOr   Nc                      |dk    r%                                   t          d           d S  fd}d }|rd}nd}t          j                            |||||           d S )Nr!   z/
For help with the %output magic, call %output?c                 J    j                             | t                     d S )Nstore_history)shellrun_cellSTORE_HISTORY)cellrendererselfs     r   cell_runnerz'OutputMagic.output.<locals>.cell_runnerV   s$    JMBBBBBr   c                 D    t          t          d|                       d S )Nz<b>Warning:</b> )r   r   )msgs    r   warnfnz"OutputMagic.output.<locals>.warnfnY   s'    D1C112233333r   z/For help with the %output magic, call %output?
z1For help with the %%output magic, call %%output?
)r]   help_promptr`   )r/   r)   r   r   output)r\   rM   rZ   r]   r`   ra   s   `     r   rb   zOutputMagic.outputN   s     2::KKMMMDEEEF	C 	C 	C 	C 	C	4 	4 	4  	OLKKNK$$T4[1<V 	% 	M 	M 	M 	M 	Mr   N)	__name__
__module____qualname__classmethodr   r/   rR   r   rb   r2   r   r   r   r       s         8 8 [8
 D D [D  P P [P& M M M _M M Mr   r   c                   N     e Zd ZdZ fdZed             Zed             Z xZ	S )CompositorMagiczq
    Magic allowing easy definition of compositor operations.
    Consult %compositor? for more information.
    c                      t                      j        |i | dg}d                    |t          j        gz             | j        j        _        d S )Nz9The %compositor line magic is used to define compositors.
)super__init__joinr   __doc__
compositor__func__)r\   argskwargslines	__class__s       r   rm   zCompositorMagic.__init__m   sQ    $)&)))LM+/99Un>T=U5U+V+V (((r   c                 h   |                                 rt          j        |                                 | j        j                  D ]Y}d t
          j        D             }|j        j        }|t          j
                    |dz   |j        z   <   t          j        |           Zd S t          d           d S )Nnsc                 ,    i | ]}|t                      S r2   )r
   )r5   groups     r   
<dictcomp>z.CompositorMagic.compositor.<locals>.<dictcomp>w   s    MMMUwyyMMMr   .z7For help with the %compositor magic, call %compositor?
)rI   r   parserW   user_nsr
   _option_groupsoutput_typerd   r   r   rz   r   registerr)   )r\   rM   
definitionrz   	type_names        r   rp   zCompositorMagic.compositors   s    ::<< 	N,24::<<DJDVWWW 0 0
MMg6LMMM&2;	FK	C*2B BC#J////	0 0 LMMMMMr   c                 N   |j         d t          j        D             }ddg}t          fd|D                       }t          fd|D                       }|s|S |s|S |rdvrdgS                     d          d                                         rdvrdgS dv rd	gS d S )
Nc                 "    g | ]}|j         d z   S )()rd   )r5   ops     r   r8   z4CompositorMagic.option_completer.<locals>.<listcomp>   s    MMMR[_MMMr   datar   c              3       K   | ]}|v V  	d S rc   r2   )r5   r   rM   s     r   	<genexpr>z3CompositorMagic.option_completer.<locals>.<genexpr>   s'      AA"*AAAAAAr   c              3       K   | ]}|v V  	d S rc   r2   )r5   moderM   s     r   r   z3CompositorMagic.option_completer.<locals>.<genexpr>   s'      ;;TDDL;;;;;;r   )r%   [])rF   r   
operationsanyr4   rI   )r   r,   r-   operation_openersmodesop_declaredmode_declaredrM   s          @r   rR   z CompositorMagic.option_completer   s    "MMz7LMMM#AAAA/@AAAAA;;;;U;;;;; 	%L 	%$$ 	3d??5LZZ__Q%%'' 	S__5LD[[5L [r   )
rd   re   rf   ro   rm   r   rp   rg   rR   __classcell__)ru   s   @r   ri   ri   f   s         
W W W W W N N ZN   [    r   ri   c                       e Zd ZdZi Zed             Zed             Zed             Zed             Z	ed             Z
dS )OptsCompleterz=
    Implements the TAB-completion for the %%opts magic.
    c                 Z   	 t          j                                                    D ]m}t          j                    d                    |                   }|d         j        }|d         j        }d                    |          }||r|ng f| j        |<   nn# t          $ r Y nw xY w| j        S )z'Get the dictionary of valid completionsr|   plotstyle)r   r   rK   rn   allowed_keywords_completionsKeyError)r   elementr   plotkwsstylekwsdotteds         r   setup_completerzOptsCompleter.setup_completer   s    	 =??//11 S S-//#((7*;*;<!&/:"7+<'**,35QXXr+R ((S  	 	 	D	s   BB 
B#"B#c           	        
 dg c
}d t          |                    dd                                                    D             }t          |          D ]\  }}g }|dk    r|                    ||                    |dk    r9|                    d                    |d         ||dz
           g                     |dk    r9|                    d                    |d         ||dz
           g                     t          |          D ]4}	|	|v r.|	

                    d          
fd	|D             }
|fc c S 5||v r
||         
 n
|fS )
zt
        Supply the appropriate key in Store.options and supply
        suggestions for further completion.
        Nc                     g | ]}|S r2   r2   )r5   r7   s     r   r8   z3OptsCompleter.dotted_completion.<locals>.<listcomp>   s    FFF!FFFr   r|   r&   r   r%   r	   c                 |    g | ]8}|                     d z             |                    d           dz            9S )r|   r%   )
startswithr4   )r5   r,   r=   depths     r   r8   z3OptsCompleter.dotted_completion.<locals>.<listcomp>   sX     #H #H #HQ&'ll>#3E&F&F#H1773<<a#8 #H #H #Hr   )rH   rG   r4   	enumerateappendrn   r+   )r   rM   sorted_keyscompositor_defssuggestionsrN   irO   
key_checkskeyr=   r   s             @@r   dotted_completionzOptsCompleter.dotted_completion   s    '+B#FFXdll3&<&<&B&B&D&DEEFFF!&)) 	 	HAuJAvv!!&),,,Avv!!#((JrNF1Q3K+H"I"IJJJAvv!!#((JrNF1Q3K+H"I"IJJJ
++ 7 7+%%%(N*0055E#H #H #H #H #H+ #H #H #HK);666666 & ''!0!7 ( {**r   c                 ^    |                     |          |                     |          z
  dz  S )Nr	   )r+   )r   rM   openerclosers       r   _inside_delimszOptsCompleter._inside_delims   s*    

6""TZZ%7%771<<r   c                     |j         }|                                 }d t          j        D             }|                     |||          S )z.Tab completion hook for the %%opts cell magic.c                 @    i | ]}|j         	|j         |j        j        S r2   )rz   r   rd   rB   s     r   r{   z2OptsCompleter.option_completer.<locals>.<dictcomp>   s>     I I I!rxI28BN$; I I Ir   )rF   r   r   definitionsline_completer)r   r,   r-   rM   completionsr   s         r   rR   zOptsCompleter.option_completer   s[     "))++I I%/%;I I I!!$_EEEr   c                 z  	 t          |                                          }d |D             }|                     |||          \  	}g d}|r#|                    d          r	fd|D             S 	s'|t	          |                                          z   |z   S |                     |dd          rd |	         d         D             S |                     |d	d
          rddgS d |	         d         D             }|                     |dd          r|S |t	          |                                          z   |z   S )Nc                     g | ]}d |v|	S r|   r2   )r5   r   s     r   r8   z0OptsCompleter.line_completer.<locals>.<listcomp>   s    DDDSCsNNSNNNr   )zstyle(zplot[znorm{r|   c                     g | ]	} d | 
S r   r2   )r5   rC   r=   s     r   r8   z0OptsCompleter.line_completer.<locals>.<listcomp>   s'    CCC~,,,,CCCr   r   r   c                     g | ]}|d z   S rE   r2   r5   kws     r   r8   z0OptsCompleter.line_completer.<locals>.<listcomp>   s    DDDrBsFDDDr   r   {}z	+axiswisez
+framewisec                     g | ]}|d z   S rE   r2   r   s     r   r8   z0OptsCompleter.line_completer.<locals>.<listcomp>   s    MMMRVMMMr   r%   r   r   )sortedrK   r   endswithlistr   )
r   rM   r   r   r   	type_keysr   verbose_openersstyle_completionsr=   s
            @r   r   zOptsCompleter.line_completer   sz   [--//00DDKDDD	&)&;&;D+&_&_#666 	N4==-- 	NCCCC{CCCC 	NtO$8$8$:$:;;;oMMd3,, 	EDD[%@%CDDDDdC-- 	/..MMk..I!.LMMMdC-- 	%$$4 4 4 6 6777/IIr   N)rd   re   rf   ro   r   rg   r   r   r   rR   r   r2   r   r   r   r      s          L    [  + + [+: = = [= F F [F J J [J J Jr   r   c                   x    e Zd ZdZdZdZdZed             Zed             Z	ed             Z
ed	d            ZdS )
	OptsMagiczx
    Magic for easy customising of normalization, plot and style options.
    Consult %%opts? for more information.
    NFc                     | j         r2| j        r| j         S t          j                            | j                    | j        !t          j        || 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   set_options)r   r   s     r   process_elementzOptsMagic.process_element   sd      	4z 4((
  !2333=$$S#-888 CMtr   c                 |    t          j        |          \  }}t          j        |          }|r|| _        || _        d S rc   )r   expand_compositor_keysvalidation_error_messager   r   )r   spec_errmsgs       r   register_custom_speczOptsMagic.register_custom_spec
  sA    5d;;a6t<< 	' &Cr   c                 r   |||fS |gg }}|                                 D ]n}|                                                    d          r0|                    |                                dd                    Y|                    |           od                    |          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&   rk   )
splitlinesrI   r   r   rn   )r   rM   rZ   specscodes        r   _partition_lineszOptsMagic._partition_lines  s     <t,fbtOO%% 	" 	"Dzz||&&x00 "TZZ\\!""-....D!!!!xx		$//r   r!   c           	      8   |                      ||          \  }}	 t          j        || j        j                  }n-# t
          $ r  t          t          d                     Y dS w xY wt                      }t          j
                    D ]+}|t          t          j        |          j                  z  },d |                                D             }||z
  }|rEd}t          t          |                    d                    |                                         |r7|                     |           | j                            |t$                     nt'          j        |          }	|	rPdt*          _        t.          j                            |	           | j        rt          t          d	                     dS t7          d
d          5  t'          j        |t          j                               ddd           n# 1 swxY w 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.
        rw   zE<b>Invalid syntax</b>: Consult <tt>%%opts?</tt> for more information.Nc                 D    h | ]}|                     d           d         S )r|   r   r3   )r5   r,   s     r   	<setcomp>z!OptsMagic.opts.<locals>.<setcomp>I  s&    >>>Qa>>>r   zZ<b>WARNING:</b> Unknown elements {unknown} not registered with any of the loaded backends.z, )unknownrU   z*Options specification will not be applied.TF)skip_invalidwarn_on_skip)r   r   r}   rW   r~   SyntaxErrorr   r   setr   loaded_backendsr   childrenrK   formatrn   r   rX   rY   r   r   r   r   r   r   r   r   r   apply_customizations)
r\   rM   rZ   r   available_elementsbackendspec_elementsunknown_elementsr_   r   s
             r   optszOptsMagic.opts"  s^   6 **466
d	>$4:+=>>>DD 	 	 	D`aabbbFF	
 !UU,.. 	G 	GG#emG&<&<&E"F"FF>>$))++>>>(+== 	K6CDDII6F,G,GHHIIJJJ 	I%%d+++JMBBBB!:4@@F *.	'
  (((; D!MNNOOOFTFFF I I1$HHHI I I I I I I I I I I I I I I"&	s!    < &A&%A&'HH
H)r!   N)rd   re   rf   ro   r   r   r   rg   r   r   r   r   r   r2   r   r   r   r      s          MIF  [$   [ 0 0 [0 <' <' <' _<' <' <'r   r   c                   Z    e Zd ZdZdZed             Zed             Ze	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                  r    t          j                     t          j        z
  } | dz  }|dz  }d||dz  | dz  fz  S )N<   zTimer elapsed: %02d:%02d:%02d)timer   
start_time)secondsminuteshourss      r   elapsed_timezTimerMagic.elapsed_timen  sB    )++!66R-2.%2wQS|1TTTr   c                     dgS )Nstartr2   )r   r,   r-   s      r   rR   zTimerMagic.option_completeru  s
    yr   r!   c                    |                                 dvrt          d           dS |                                 dk    rEt          j                    t          _        t          j        d          }t          d|            dS | j        t          d           dS 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?)rI   r)   r   r   r   strftimer   )r\   rM   	timestamps      r   timerzTimerMagic.timery  s      ::<<},,TUUUFZZ\\W$$$(IKKJ!&9::I-)--...F_$^_____$##%%&&&&&r   )r!   )rd   re   rf   ro   r   staticmethodr   rg   rR   r   r  r2   r   r   r   r   c  s|          JU U \U   [ ' ' ' Z' ' 'r   r   c                 Z   |                      t                     |                      t                     t          j                                        }|t          j        _        t          t          d           n|                      t                     t          t          d           n|                      t                     |                     dt          j        d           |                     dt          j        d           |                     dt          j        d           |                     dt          j        d           t                                           |                     dt          j        d	           |                     dt          j        d
           d S )Nz6%opts magic unavailable (pyparsing cannot be imported)z<%compositor magic unavailable (pyparsing cannot be imported)complete_commandz%timer)str_keyz%compositorr$   z%%outputr   z%opts)register_magicsr   r   r   r   _generate_docstringrb   ro   	pyparsingr)   r   ri   set_hookrR   r   r   )ip	docstrings     r   load_magicsr    sb   z"""{###%99;;I!*K5!YZZZZ


Y
'
'
'% ^____


_
-
-
- KK"J$?8KTTTKK"O$DP]K^^^KK"K$@IKVVVKK"K$@JKWWW!!###KK"M$BhKWWWKK"M$BgKVVVVVr   )"r   r   IPython.core.magicr   r   r   r   IPython.displayr   r   core.optionsr
   r   r   r   core.pprintr   	operationr   r  holoviews.util.parserr   r   ImportErrorrY   IPython.corer   storer   ri   r   r   r   r  r2   r   r   <module>r     s[   



  P P P P P P P P P P P P ) ) ) ) ) ) ) ) G G G G G G G G G G G G % % % % % % " " " " " "? ?>>>>>>>>    III         BM BM BM BM BM& BM BM BMJ ) ) ) ) )f ) ) )ZUJ UJ UJ UJ UJ UJ UJ UJr q' q' q' q' q' q' q' q'j 0' 0' 0' 0' 0' 0' 0' 0'fW W W W Ws   A AA