ó
æNXc           @  sÌ   d  Z  d d l m Z d d l Z d d l 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 d
 e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   sb   
An application for managing IPython history.

To be invoked as the `ipython history` subcommand.
iÿÿÿÿ(   t   print_functionN(   t   Application(   t   BaseIPythonApplication(   t   Boolt   Intt   Dict(   t
   ask_yes_nosè   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.
s  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`
t   HistoryTrimc           B  sŽ   e  Z e Z e e d  e d d ƒZ e d d  e d d ƒZ	 e
 e d i i e d 6d 6e j d ƒ f ƒ ƒ Z e
 e d d ƒ ƒ Z d	 „  Z RS(
   t   configt   helps/   Keep the old history file as history.sqlite.<N>iè  s/   Number of recent lines to keep in the database.t   backupR   t   keeps   HistoryTrim.keepc         C  s”  |  j  j } t j j | d ƒ } t j | ƒ } t | j d |  j	 d f ƒ ƒ } t
 | ƒ |  j	 k r† t d |  j	 ƒ t d ƒ d  St d |  j	 ƒ | j ƒ  | j ƒ  | rø | d d } t | j d | f ƒ ƒ } t | j d	 | f ƒ ƒ } n  | j ƒ  t j j | d
 ƒ } d }	 x? t j j | ƒ r^|	 d 7}	 t j j | d
 t |	 ƒ ƒ } q Wt j | ƒ }
 |
 j d ƒ |
 j d ƒ |
 j d ƒ |
 j ƒ  | rå|
 5 |
 j d | ƒ |
 j d | ƒ |
 j d | ƒ Wd  QXn  |
 j ƒ  |  j rsd }	 t j j | d |	 ƒ } x9 t j j | ƒ rR|	 d 7}	 t j j | d |	 ƒ } qWt j | | ƒ t d | ƒ n t j | ƒ t j | | ƒ d  S(   Ns   history.sqlites^   SELECT session, line, source, source_raw FROM history ORDER BY session DESC, line DESC LIMIT ?i   s=   There are already at most %d entries in the history database.s>   Not doing anything. Use --keep= argument to keep fewer entriess/   Trimming history to the most recent %d entries.i    sC   SELECT session, line, output FROM output_history WHERE session >= ?sM   SELECT session, start, end, num_cmds, remark FROM sessions WHERE session >= ?s   history.sqlite.newsÆ   CREATE TABLE IF NOT EXISTS sessions (session integer
                            primary key autoincrement, start timestamp,
                            end timestamp, num_cmds integer, remark text)s­   CREATE TABLE IF NOT EXISTS history
                        (session integer, line integer, source text, source_raw text,
                        PRIMARY KEY (session, line))s£   CREATE TABLE IF NOT EXISTS output_history
                        (session integer, line integer, output text,
                        PRIMARY KEY (session, line))s'   insert into sessions values (?,?,?,?,?)s$   insert into history values (?,?,?,?)s)   insert into output_history values (?,?,?)s   history.sqlite.old.%ds    Backed up longer history file to(   t   profile_dirt   locationt   ost   patht   joint   sqlite3t   connectt   listt   executeR   t   lent   printt   popt   reverset   closet   existst   strt   committ   executemanyR
   t   renamet   remove(   t   selfR   t	   hist_filet   cont   inputst   first_sessiont   outputst   sessionst   new_hist_filet   it   new_dbt   backup_hist_file(    (    sO   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/historyapp.pyt   start5   s`    




#

	
(   t   __name__t
   __module__t   trim_hist_helpt   descriptionR   t   Falset   TrueR
   R   R   R   t   dictt   get_metadatat   flagst   aliasesR+   (    (    (    sO   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/historyapp.pyR   "   s   				t   HistoryClearc        	   B  s¥   e  Z e Z e d  d e d d ƒZ e e d e d d ƒZ	 e
 e d i i e d 6d 6e	 j d ƒ f d i i e d 6d 6e	 j d ƒ f ƒ ƒ Z e
 ƒ  Z d	 „  Z RS(
   i    R   R	   s/   Number of recent lines to keep in the database.s"   Don't prompt user for confirmationt   forceR6   t   fR   c         C  s5   |  j  s! t d d d d d ƒr1 t j |  ƒ n  d  S(   Ns#   Really delete all ipython history? t   defaultt   not	   interrupt(   R7   R   R   R+   (   R    (    (    sO   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/historyapp.pyR+   ˆ   s    (   R,   R-   t   clear_hist_helpR/   R   R0   R   R   R1   R7   R   R2   R3   R4   R5   R+   (    (    (    sO   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/historyapp.pyR6   w   s   				t
   HistoryAppc           B  s^   e  Z d  Z d Z e e d e e j j ƒ  d f d e e j j ƒ  d f ƒ ƒ Z	 d „  Z
 RS(   u   ipython-historys$   Manage the IPython history database.t   trimi    t   clearc         C  sb   |  j  d  k rQ t d |  j j ƒ  ƒ t ƒ  |  j ƒ  |  j ƒ  |  j d ƒ n |  j  j ƒ  Sd  S(   Ns0   No subcommand specified. Must specify one of: %si   (	   t   subappt   NoneR   t   subcommandst   keyst   print_descriptiont   print_subcommandst   exitR+   (   R    (    (    sO   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/historyapp.pyR+   –   s    

(   R,   R-   t   nameR/   R   R2   R   t
   splitlinesR6   RB   R+   (    (    (    sO   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/historyapp.pyR=      s   	(   t   __doc__t
   __future__R    R   R   t   traitlets.config.applicationR   t   IPython.core.applicationR   t	   traitletsR   R   R   t   IPython.utils.ioR   R.   R<   R   R6   R=   (    (    (    sO   /data/av2000/mvv/env_mvv/lib/python2.7/site-packages/IPython/core/historyapp.pyt   <module>   s   	U