
    f                         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e      Z G d de      Z G d de      Zy)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                       e Zd ZeZ edd      j                  d      Z edd      j                  d      Z	 e
 ed ddiiej                  f	            Z e
 ed
            Zd Zy)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                 D   t        | j                  j                        }|dz  }t        j                  |      }t        |j                  d| j                  dz   f            }t        |      | j                  k  r$t        d| j                  z         t        d       y t        d| j                  z         |j                          |j                          |r@|d   d   }t        |j                  d|f            }t        |j                  d	|f            }|j                          |d
z  }d}	|j                         r'|	dz  }	|d
t        |	      z   z  }|j                         r't        j                  |      }
|
j                  d       |
j                  d       |
j                  d       |
j                          |rA|
5  |
j!                  d       |
j!                  d|       |
j!                  d       d d d        |
j                          | j"                  rVd}	|d|	z  z  }|j                         r|	dz  }	|d|	z  z  }|j                         r|j%                  |       t        d|       n|j'                          |j%                  |       y # 1 sw Y   xY w)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_files               R/var/www/cvtools/html/venv/lib/python3.12/site-packages/IPython/core/historyapp.pystartzHistoryTrim.start6   s   4++445"22	ooi( ckk #SUYU^U^_`U`Tbd ev;$))#QTXT]T]]^RS?$))KL

"1IaLM3;; (KMZL\^ _GCKK )FHUGWY ZH		 $&::""$FA'+?#a&+HIM ""$ / M 	N 	 8 	9 	 8 	9 	  Y""#LhW""#I6R""#NPWX	Y
 	;;A*.E.IJ"))+Q#.2IA2M#N  #))+ -.46FGY'%Y Ys   7JJN)__name__
__module____qualname__trim_hist_helpdescriptionr   tagr   r   r   r	   dictr   flagsaliasesr3        r2   r   r   !   s     K%>

#T#
  t>

#T#
 	  Ht#45KK
 E ! G@(r>   r   c                       e Zd ZeZ edd      Z edd      j                  d      Z	 e
 ed ddiie	j                  fd	ddiie	j                  f
            Z e
       Zd Zy)HistoryClearr   r   r   Fz"Don't prompt user for confirmationTr   forcer   )rA   fc                 d    | j                   st        ddd      rt        j                  |        y y )Nz#Really delete all ipython history? no)default	interrupt)rA   r   r   r3   r'   s    r2   r3   zHistoryClear.start   s+    ::$I.d#.r>   N)r4   r5   r6   clear_hist_helpr8   r   r   r   r9   rA   r	   r:   r   r;   r<   r3   r=   r>   r2   r@   r@   x   s    !Kq>@D 1

#T#
 
  Gd#34JJw./JJ
 E fG$r>   r@   c                       e Zd ZdZdZ e eeej                  j                         d   fe	e	j                  j                         d   f            Z
d Zy)
HistoryAppzipython-historyz$Manage the IPython history database.r   )trimclearc                    | j                   bt        d| j                  j                         z         t                | j	                          | j                          | j                  d       y | j                   j                         S )Nz0No subcommand specified. Must specify one of: %sr   )subappr   subcommandskeysprint_descriptionprint_subcommandsexitr3   rG   s    r2   r3   zHistoryApp.start   sk    ;;D595E5E5J5J5LN OG""$""$IIaL;;$$&&r>   N)r4   r5   r6   namer8   r	   r:   r   
splitlinesr@   rO   r3   r=   r>   r2   rJ   rJ      sZ    D8Kt[44??A!DE|77BBDQGH K
	'r>   rJ   )__doc__r   pathlibr   traitlets.config.applicationr   applicationr   	traitletsr   r   r	   utils.ior   r7   rH   r   r@   rJ   r=   r>   r2   <module>r\      sW      4 / % % !U(( U(n$; $.' 'r>   