Format: application/x-python-code
⬇ Download File (9.2 KB)
   1  o
   2  
   3  ��hU1�@s�dZddlZddlZddlmZddlZddlmZddl	m
   4  Z
   5  mZmZddl
   6  mZmZmZddlmZddlmZe�ZeGd	d
   7  �d
   8  e
   9  ��ZdS)z6Implementation of magic functions related to History.
  10  �N)�open)�StdinNotImplementedError)�Magics�magics_class�
  11  line_magic)�argument�magic_arguments�parse_argstring)�skip_doctest)�ioc@s�eZdZe�edddddd�eddddd	d�ed
  12  ddddd�ed
  13  ddddd�edddd�edddddd�eddededd �ed!d"dd#d$�ed%dd&�eed.d(d)�������������Z	ed*d+��Z
  14  ed.d,d-��ZdS)/�
  15  HistoryMagicsz-n�
  16  print_nums�
  17  store_trueFz{
  18          print line numbers for each input.
  19          This feature is only available if numbered prompts are in use.
  20          )�dest�action�default�helpz-o�
  21  get_outputz"also print outputs for each input.z-p�	pypromptsz�
  22          print classic '>>>' python prompts before each input.
  23          This is useful for making documentation, and in conjunction
  24          with -o, for producing doctest-ready output.
  25          z-t�raw�store_falseTa�
  26          print the 'translated' history, as IPython understands it.
  27          IPython filters your input and converts it all into valid Python
  28          source before executing it (things like magics or aliases are turned
  29          into function calls, for example). With this option, you'll see the
  30          native history instead of the user-entered version: '%%cd /' will be
  31          seen as 'get_ipython().run_line_magic("cd", "/")' instead of '%%cd /'.
  32          z-f�filenamea�
  33          FILENAME: instead of printing the output to the screen, redirect
  34          it to the given file.  The file is always overwritten, though *when
  35          it can*, IPython asks for confirmation first. In particular, running
  36          the command 'history -f FILENAME' from the IPython Notebook
  37          interface will replace FILENAME even if it already exists *without*
  38          confirmation.
  39          )rrz-g�pattern�*Nac
  40          treat the arg as a glob pattern to search for in (full) history.
  41          This includes the saved history (almost all commands ever written).
  42          The pattern may contain '?' to match one unknown character and '*'
  43          to match any number of unknown characters. Use '%%hist -g' to show
  44          full saved history (may be very long).
  45          )r�nargsrrz-l�limit�?z�
  46          get the last n lines from all sessions. Specify n as a single
  47          arg, or the default is the last 10 lines.
  48          )r�typerrrz-u�uniquezN
  49          when searching history using `-g`, show only unique history.
  50          )rrr�range)r�csbt|j|�}|jj��fdd�}|j}|stj}d}n6tj�	|�}tj�
  51  |�rHz	t�d|�}Wnt
  52  y;d}Ynw|sDtd�dStd�t|d	d
  53  d�}d}|j}|j}	|j}
  54  |j}d}|jturfdn|j}
  55  d}|jdur�|js�|jr�dd
  56  �|j�d}nd}�j|||	|
  57  |jd�}d}n0|jtur�|
  58  dur�dn|
  59  }�j|||	d�}n|jr�dd
  60  �|j�d}d}��d
  61  �|j�||	�}d}|D]`\}}}|	r�|\}}|r�t�||�s�q�|�d���}d|v}|r�dnd
  62  }|r�td|||�� |�|f|dd�|
  63  �rtdd|d�|�rd�|�!��d}t||d�|	�r%|�r%t||d�q�|�r/|�"�dSdS)a�Print input history (_i<n> variables), with most recent last.
  64  
  65          By default, input history is printed without line numbers so it can be
  66          directly pasted into an editor. Use -n to show them.
  67  
  68          By default, all input history from the current session is displayed.
  69          Ranges of history can be indicated using the syntax:
  70  
  71          ``4``
  72              Line 4, current session
  73          ``4-6``
  74              Lines 4-6, current session
  75          ``243/1-5``
  76              Lines 1-5, session 243
  77          ``~2/7``
  78              Line 7, session 2 before current
  79          ``~8/1-~6/5``
  80              From the first line of 8 sessions ago, to the fifth line of 6
  81              sessions ago.
  82  
  83          Multiple ranges can be entered, separated by spaces
  84  
  85          The same syntax is used by %macro, %save, %edit, %rerun
  86  
  87          Examples
  88          --------
  89          ::
  90  
  91            In [6]: %history -n 4-6
  92            4:a = 12
  93            5:print(a**2)
  94            6:%history -n 4-6
  95  
  96          cs"|d�jfvrt|�Sd||fS)z0Helper function to format line numbers properly.rz%s/%s)Zsession_number�str)�session�line��history_manager��Qc:\Users\shubh\Videos\agent\nova\Lib\site-packages\IPython/core/magics/history.py�_format_lineno�sz-HistoryMagics.history.<locals>._format_linenoFzFile %r exists. Overwrite?Tz	Aborting.NzOverwriting file.�wzutf-8)�encodingr� )r�output�nr�
  97  )rr,��
  98  z%s:%sr )�file�endz>>> )r2r1z
  99  ... z
 100  ...)r1)#r	�history�shellr%r�sys�stdout�os�path�
 101  expanduser�existsrZ
 102  ask_yes_nor�print�io_openr
 103  rrrr�_unspecifiedrr�join�searchr�get_tail�get_range_by_str�fnmatch�
 104  expandtabs�rstrip�rjust�
 105  splitlines�close)�self�parameter_s�argsr(Zoutfname�outfileZclose_at_endZansr
 106  rrrrrZ
 107  range_pattern�histr-�widthr"�lineno�inliner,�	multilineZline_sepr&r$r'r3(s�`�
 108  �
 109  �����zHistoryMagics.historycCs�|s|j�t|jjd��dS|jj�|�}d�dd�|D��}|r-|j�|���dSztt||jj��}Wn4t	yl|jj�
 110  d|d�}tdd�|D��D]}d	|vs]d
 111  |vr^qS|j�|���YdSYnw|j�|���dStd|�dS)aIRepeat a command, or get command to input line for editing.
 112  
 113          %recall and %rep are equivalent.
 114  
 115          - %recall (no arguments):
 116  
 117          Place a string version of last computation result (stored in the
 118          special '_' variable) to the next input prompt. Allows you to create
 119          elaborate command lines without using copy-paste::
 120  
 121               In[1]: l = ["hei", "vaan"]
 122               In[2]: "".join(l)
 123              Out[2]: heivaan
 124               In[3]: %recall
 125               In[4]: heivaan_ <== cursor blinking
 126  
 127          %recall 45
 128  
 129          Place history line 45 on the next input prompt. Use %hist to find
 130          out the number.
 131  
 132          %recall 1-4
 133  
 134          Combine the specified lines into one cell, and place it on the next
 135          input prompt. See %history for the slice syntax.
 136  
 137          %recall foo+bar
 138  
 139          If foo+bar can be evaluated in the user namespace, the result is
 140          placed at the next input prompt. Otherwise, the history is searched
 141          for lines which contain that substring, and the most recent one is
 142          placed at the next input prompt.
 143          �_Nr0css�|]}|dVqdS)�Nr&��.0�xr&r&r'�	<genexpr>s�z'HistoryMagics.recall.<locals>.<genexpr>rcS�g|]}|d�qS�rRr&rSr&r&r'�
 144  <listcomp>�z(HistoryMagics.recall.<locals>.<listcomp>�recall�repz%Couldn't evaluate or find in history:)
 145  r4Zset_next_inputr!Zuser_nsr%rAr>rD�eval�	Exceptionr?�reversedr;)rH�arg�	histlines�cmd�hr&r&r'r[�s,#��zHistoryMagics.recallc	CsR|j|ddd�\}}d|vrAzt|d�}Wnty$td�YdSw|dkr/td�dS|dkr9td	�dS|jj�|�}n;d
 146  |vrkd|d
 147  d}t|jj�|��}t	|�D]
 148  }d|d
 149  vrg|g}nqZg}n|ru|jj�
 150  |�}n|jj�d�}dd�|D�}|s�td�dSd�|�}td�t|�td�|jjd�|�dd�dS)aoRe-run previous input
 151  
 152          By default, you can specify ranges of input history to be repeated
 153          (as with %history). With no arguments, it will repeat the last line.
 154  
 155          Options:
 156  
 157            -l <n> : Repeat the last n lines of input, not including the
 158            current command.
 159  
 160            -g foo : Repeat the most recent line which contains foo
 161          zl:g:�string)�mode�lz"Number of lines must be an integerNrz'Requested 0 last lines - nothing to runz+Number of lines to rerun cannot be negative�gr�rerunrR�cSrWrXr&rSr&r&r'rYJrZz'HistoryMagics.rerun.<locals>.<listcomp>z'No lines in history match specificationr0z=== Executing: ===z=== Output: ===F)Z
 162  store_history)
 163  Z
 164  parse_options�int�
 165  ValueErrorr;r4r%r@�listr?r_rAr>Zrun_cell)	rHrI�optsrJr-rL�prfrar&r&r'rhsJ���
 166  zHistoryMagics.rerun)r )�__name__�
 167  __module__�__qualname__rrrjr=r
 168  rr3r[rhr&r&r&r'r%sV����
 169  �
 170  �	
 171  ��
 172  "
 173  :r)�__doc__r7r5rrr<rBZIPython.core.errorrZIPython.core.magicrrrZIPython.core.magic_argumentsrrr	ZIPython.testing.skipdoctestr
 174  Z
 175  IPython.utils�objectr=rr&r&r&r'�<module>s