o
    ˷e                     @   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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 sw   Y  |
  | jrd}	|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.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sqlite3connectlistexecuter   lenprintpopreversecloseexistsstrcommitexecutemanyr   renameunlink)selfr   	hist_fileconinputsfirst_sessionoutputssessionsnew_hist_fileinew_dbbackup_hist_file r/   N/var/www/ideatree/venv/lib/python3.10/site-packages/IPython/core/historyapp.pystart6   sf   






zHistoryTrim.startN)__name__
__module____qualname__trim_hist_helpdescriptionr   tagr   r   r   r   dictr   flagsaliasesr1   r/   r/   r/   r0   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 d S )Nz#Really delete all ipython history? no)default	interrupt)r<   r
   r   r1   r$   r/   r/   r0   r1      s
   
zHistoryClear.startN)r2   r3   r4   clear_hist_helpr6   r   r   r   r7   r<   r   r8   r   r9   r:   r1   r/   r/   r/   r0   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   )trimclearc                 C   sJ   | j d u r td| j   t  |   |   | d d S | j  S )Nz0No subcommand specified. Must specify one of: %sr   )subappr   subcommandskeysprint_descriptionprint_subcommandsexitr1   rA   r/   r/   r0   r1      s   

zHistoryApp.startN)r2   r3   r4   namer6   r   r8   r   
splitlinesr;   rG   r1   r/   r/   r/   r0   rC      s    rC   )__doc__r   pathlibr   traitlets.config.applicationr   applicationr   	traitletsr   r   r   utils.ior
   r5   rB   r   r;   rC   r/   r/   r/   r0   <module>   s   
W