a
    Fb                     @   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_nozTrim 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Z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  |
  |  |r|d d }t|d|f}t|d	|f}|  |d
 }d}	| r|	d7 }	|d
t|	  }qt|}
|
d |
d |
d |
  |rf|
4 |
d| |
d| |
d| W d    n1 s\0    Y  |
  | jrd}	|d|	  }| r|	d7 }	|d|	  }q|| 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.newzCREATE TABLE IF NOT EXISTS sessions (session integer
                            primary key autoincrement, start timestamp,
                            end timestamp, num_cmds integer, remark text)zCREATE TABLE IF NOT EXISTS history
                        (session integer, line integer, source text, source_raw text,
                        PRIMARY KEY (session, line))zCREATE 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sqlite3ZconnectlistZexecuter   lenprintpopreversecloseexistsstrZcommitZexecutemanyr   renameunlink)selfr   Z	hist_fileZconZinputsZfirst_sessionZoutputsZsessionsZnew_hist_fileiZnew_dbZbackup_hist_file r!   6lib/python3.9/site-packages/IPython/core/historyapp.pystart6   s`    





,

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   )r.   fc                 C   s"   | j stddddrt|  d S )Nz#Really delete all ipython history? Zno)defaultZ	interrupt)r.   r
   r   r#   r   r!   r!   r"   r#      s    
zHistoryClear.startN)r$   r%   r&   clear_hist_helpr(   r   r   r   r)   r.   r   r*   r   r+   r,   r#   r!   r!   r!   r"   r-   x   s&   

r-   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   sL   | j d u r>td| j   t  |   |   | d n
| j  S d S )Nz0No subcommand specified. Must specify one of: %sr   )Zsubappr   subcommandskeysZprint_descriptionZprint_subcommandsexitr#   r1   r!   r!   r"   r#      s    
zHistoryApp.startN)r$   r%   r&   namer(   r   r*   r   
splitlinesr-   r5   r#   r!   r!   r!   r"   r3      s   r3   )__doc__r   pathlibr   Ztraitlets.config.applicationr   Zapplicationr   Z	traitletsr   r   r   Zutils.ior
   r'   r2   r   r-   r3   r!   r!   r!   r"   <module>   s   
W