o
    û›hï  ã                   @   sŒ   d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	m
Z
mZ dd	lmZ d
ZdZG dd„ deƒZG dd„ deƒZG dd„ deƒZdS )zb
An application for managing IPython history.

To be invoked as the `ipython history` subcommand.
é    N)ÚPath)ÚApplicationé   )ÚBaseIPythonApplication)ÚBoolÚIntÚDicté   )Ú
ask_yes_nozèTrim the IPython history database to the last 1000 entries.

This actually copies the last 1000 entries to a new database, and then replaces
the old file with the new. Use the `--keep=` argument to specify a number
other than 1000.
a  Clear the IPython history database, deleting all entries.

Because this is a destructive operation, IPython will prompt the user if they
really want to do this. Passing a `-f` flag will force clearing without a
prompt.

This is an handy alias to `ipython history trim --keep=0`
c                   @   sj   e Zd ZeZeddd�jdd�Zeddd�jdd�Z	e
ed ddiiejfd	�ƒZe
ed
d�ƒZdd„ ZdS )ÚHistoryTrimFz/Keep the old history file as history.sqlite.<N>©ÚhelpT©Úconfigiè  ú/Number of recent lines to keep in the database.Úbackup)r   zHistoryTrim.keep)Úkeepc                 C   sÖ  t | jjƒ}|d }t |¡}t| d| jd f¡ƒ}t|ƒ| jkr/t	d| j ƒ t	dƒ d S t	d| j ƒ | 
¡  | ¡  |rX|d d }t| d|f¡ƒ}t| d	|f¡ƒ}| ¡  |d
 }d}	| ¡ rv|	d7 }	|d
t|	ƒ  }| ¡ sft |¡}
|
 d¡ |
 d¡ |
 d¡ |
 ¡  |r´|
� |
 d|¡ |
 d|¡ |
 d|¡ W d   ƒ n1 s¯w   Y  |
 ¡  | jràd}	|d|	  }| ¡ rÕ|	d7 }	|d|	  }| ¡ sÇ| |¡ t	d|ƒ n| ¡  | |¡ d S )Nzhistory.sqlitez^SELECT session, line, source, source_raw FROM history ORDER BY session DESC, line DESC LIMIT ?r   z=There are already at most %d entries in the history database.z>Not doing anything. Use --keep= argument to keep fewer entriesz/Trimming history to the most recent %d entries.r   zCSELECT session, line, output FROM output_history WHERE session >= ?zMSELECT session, start, end, num_cmds, remark FROM sessions WHERE session >= ?zhistory.sqlite.newzÆCREATE TABLE IF NOT EXISTS sessions (session integer
                            primary key autoincrement, start timestamp,
                            end timestamp, num_cmds integer, remark text)z­CREATE TABLE IF NOT EXISTS history
                        (session integer, line integer, source text, source_raw text,
                        PRIMARY KEY (session, line))z£CREATE TABLE IF NOT EXISTS output_history
                        (session integer, line integer, output text,
                        PRIMARY KEY (session, line))z'insert into sessions values (?,?,?,?,?)z$insert into history values (?,?,?,?)z)insert into output_history values (?,?,?)zhistory.sqlite.old.%dz Backed up longer history file to)r   Úprofile_dirÚlocationÚsqlite3ÚconnectÚlistÚexecuter   ÚlenÚprintÚpopÚreverseÚcloseÚexistsÚstrÚcommitZexecutemanyr   ÚrenameÚunlink)Úselfr   Z	hist_fileÚconZinputsZfirst_sessionZoutputsÚsessionsZnew_hist_fileÚiZnew_dbZbackup_hist_file© r'   úMc:\Users\shubh\Videos\agent\nova\Lib\site-packages\IPython/core/historyapp.pyÚstart2   sf   

ÿÿÿý



üþ
zHistoryTrim.startN)Ú__name__Ú
__module__Ú__qualname__Útrim_hist_helpÚdescriptionr   Útagr   r   r   r   Údictr   ÚflagsÚaliasesr)   r'   r'   r'   r(   r   !   s    ÿÿÿr   c                   @   sj   e Zd ZeZeddd�Zeddd�jdd�Z	e
ed ddiie	jfd	ddiie	jfd
�ƒZe
ƒ Zdd„ ZdS )ÚHistoryClearr   r   r   Fz"Don't prompt user for confirmationTr   Úforcer   )r4   Úfc                 C   s&   | j s
tdddd�rt | ¡ d S d S )Nz#Really delete all ipython history? Úno)ÚdefaultZ	interrupt)r4   r
   r   r)   ©r#   r'   r'   r(   r)   ƒ   s
   ÿýzHistoryClear.startN)r*   r+   r,   Úclear_hist_helpr.   r   r   r   r/   r4   r   r0   r   r1   r2   r)   r'   r'   r'   r(   r3   u   s    þÿr3   c                   @   sH   e Zd ZdZdZeeeej ¡ d fe	e	j ¡ d fd�ƒZ
dd„ ZdS )Ú
HistoryAppzipython-historyz$Manage the IPython history database.r   )ZtrimÚclearc                 C   sP   | j d u r#tdd tt| jƒ¡ d ƒ |  ¡  |  ¡  |  d¡ d S | j  	¡ S )Nz.No subcommand specified. Must specify one of: z, z.
r   )
Zsubappr   ÚjoinÚmapÚreprÚsubcommandsZprint_descriptionZprint_subcommandsÚexitr)   r8   r'   r'   r(   r)   “   s   
ÿþÿ
zHistoryApp.startN)r*   r+   r,   Únamer.   r   r0   r   Ú
splitlinesr3   r?   r)   r'   r'   r'   r(   r:   Š   s    þr:   )Ú__doc__r   Úpathlibr   Ztraitlets.config.applicationr   Zapplicationr   Z	traitletsr   r   r   Zutils.ior
   r-   r9   r   r3   r:   r'   r'   r'   r(   Ú<module>   s   
T