
    ^h                         S r SSKrSSKJr  SSKJr  SSKJr  SSKJ	r	J
r
Jr  SS	KJr  S
rSr " S S\5      r " S S\5      r " S S\5      rg)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                       \ rS rSr\r\" SSS9R                  SS9r\	" SSS9R                  SS9r
\" \" S S	S00\R                  4S
95      r\" \" SS95      rS rSrg)HistoryTrim!   Fz/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                    [        U R                  R                  5      nUS-  n[        R                  " U5      n[        UR                  SU R                  S-   45      5      n[        U5      U R                  ::  a$  [        SU R                  -  5        [        S5        g [        SU R                  -  5        UR                  5         UR                  5         U(       a@  US   S   n[        UR                  SU45      5      n[        UR                  S	U45      5      nUR                  5         US
-  nSn	UR                  5       (       a-  U	S-  n	US
[        U	5      -   -  nUR                  5       (       a  M-  [        R                  " U5      n
U
R                  S5        U
R                  S5        U
R                  S5        U
R                  5         U(       aA  U
   U
R!                  SW5        U
R!                  SU5        U
R!                  SW5        S S S 5        U
R                  5         U R"                  (       aa  Sn	USU	-  -  nUR                  5       (       a$  U	S-  n	USU	-  -  nUR                  5       (       a  M$  UR%                  U5        [        SU5        OUR'                  5         UR%                  U5        g ! , (       d  f       N= f)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               Q/home/james-whalen/.local/lib/python3.13/site-packages/IPython/core/historyapp.pystart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 	 ""#LhW""#I6R""#NPWX	 
 	;;A*.E.IJ"))++Q#.2IA2M#N  #))++ -.46FGY'% s   7J>>
K N)__name__
__module____qualname____firstlineno__trim_hist_helpdescriptionr   tagr   r   r   r	   dictr   flagsaliasesr4   __static_attributes__r6       r3   r   r   !   s     K%>

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

#T#
 	  Ht#45KK
 E ! G@(rB   r   c                       \ rS rSr\r\" SSS9r\" SSS9R                  SS9r
\" \" S S	S00\
R                  4S
S	S00\
R                  4S95      r\" 5       rS rSrg)HistoryClearx   r   r   r   Fz"Don't prompt user for confirmationTr   forcer   )rF   fc                 r    U R                   (       d  [        SSSS9(       a  [        R                  U 5        g g )Nz#Really delete all ipython history? no)default	interrupt)rF   r   r   r4   r(   s    r3   r4   HistoryClear.start   s+    ::$I.d#.rB   r6   N)r7   r8   r9   r:   clear_hist_helpr<   r   r   r   r=   rF   r	   r>   r   r?   r@   r4   rA   r6   rB   r3   rD   rD   x   s    !Kq>@D 1

#T#
 
  Gd#34JJw./JJ
 E fG$rB   rD   c                       \ rS rSrSrSr\" \" \\R
                  R                  5       S   4\
\
R
                  R                  5       S   4S95      rS rSrg)	
HistoryApp   zipython-historyz$Manage the IPython history database.r   )trimclearc                    U R                   cb  [        SU R                  R                  5       -  5        [        5         U R	                  5         U R                  5         U R                  S5        g U R                   R                  5       $ )Nz0No subcommand specified. Must specify one of: %sr   )subappr   subcommandskeysprint_descriptionprint_subcommandsexitr4   rL   s    r3   r4   HistoryApp.start   sk    ;;D595E5E5J5J5LN OG""$""$IIaL;;$$&&rB   r6   N)r7   r8   r9   r:   namer<   r	   r>   r   
splitlinesrD   rV   r4   rA   r6   rB   r3   rP   rP      sZ    D8Kt[44??A!DE|77BBDQGH K
	'rB   rP   )__doc__r   pathlibr   traitlets.config.applicationr   applicationr   	traitletsr   r   r	   utils.ior   r;   rN   r   rD   rP   r6   rB   r3   <module>rd      sW      4 / % % !U(( U(n$; $.' 'rB   