ó
ù2¢\c           @   s²   d  Z  d d l Z d d l Z y d d l Z Wn! e k
 rQ d d l m Z n Xd d l m Z m	 Z	 d d l
 m Z d d l m Z d d l m Z d e f d	 „  ƒ  YZ d S(
   s   A base class session manager.iÿÿÿÿN(   t   dbapi2(   t   gent   web(   t   LoggingConfigurable(   t   unicode_type(   t   Instancet   SessionManagerc           B   s  e  Z e d  ƒ Z e d ƒ Z d Z d Z d d d d d h Z e	 d „  ƒ Z
 e	 d „  ƒ Z d	 „  Z d
 „  Z d „  Z d „  Z e j d d d d d d „ ƒ Z e j d „  ƒ Z d d d d d „ Z d „  Z d „  Z d „  Z d „  Z e j d „  ƒ Z RS(   s<   notebook.services.kernels.kernelmanager.MappingKernelManagers2   notebook.services.contents.manager.ContentsManagert
   session_idt   patht   namet   typet	   kernel_idc         C   s;   |  j  d k r4 |  j j ƒ  |  _  |  j  j d ƒ n  |  j  S(   s5   Start a cursor and create a database called 'session'sO   CREATE TABLE session 
                (session_id, path, name, type, kernel_id)N(   t   _cursort   Nonet
   connectiont   cursort   execute(   t   self(    (    sH   lib/python2.7/site-packages/notebook/services/sessions/sessionmanager.pyR       s
    	
c         C   s:   |  j  d k r3 t j d ƒ |  _  t j |  j  _ n  |  j  S(   s   Start a database connections   :memory:N(   t   _connectionR   t   sqlite3t   connectt   Rowt   row_factory(   R   (    (    sH   lib/python2.7/site-packages/notebook/services/sessions/sessionmanager.pyR   )   s    c         C   s,   |  j  d k	 r( |  j  j ƒ  d |  _  n  d S(   s   Close the sqlite connectionN(   R   R   t   close(   R   (    (    sH   lib/python2.7/site-packages/notebook/services/sessions/sessionmanager.pyR   1   s    c         C   s   |  j  ƒ  d S(   s+   Close connection once SessionManager closesN(   R   (   R   (    (    sH   lib/python2.7/site-packages/notebook/services/sessions/sessionmanager.pyt   __del__7   s    c         C   s=   |  j  j d | f ƒ |  j  j ƒ  } | d k r5 t St Sd S(   s2   Check to see if the session of a given name existss"   SELECT * FROM session WHERE path=?N(   R   R   t   fetchoneR   t   Falset   True(   R   R   t   reply(    (    sH   lib/python2.7/site-packages/notebook/services/sessions/sessionmanager.pyt   session_exists;   s
    c         C   s   t  t j ƒ  ƒ S(   s   Create a uuid for a new session(   R   t   uuidt   uuid4(   R   (    (    sH   lib/python2.7/site-packages/notebook/services/sessions/sessionmanager.pyt   new_session_idD   s    c         c   sŠ   |  j  ƒ  } | d k	 r* | |  j k r* n |  j | | | | | ƒ V} t j |  j | d | d | d | d | ƒƒ V} t j | ƒ ‚ d S(   s'   Creates a session and returns its modelR   R	   R
   R   N(   R    R   t   kernel_managert   start_kernel_for_sessionR   t   maybe_futuret   save_sessiont   Return(   R   R   R	   R
   t   kernel_nameR   R   t   result(    (    sH   lib/python2.7/site-packages/notebook/services/sessions/sessionmanager.pyt   create_sessionH   s    +c         c   sM   |  j  j d | ƒ } t j |  j j d | d | ƒ ƒ V} t j | ƒ ‚ d S(   s'   Start a new kernel for a given session.R   R&   N(   t   contents_managert   get_kernel_pathR   R#   R!   t   start_kernelR%   (   R   R   R   R	   R
   R&   t   kernel_pathR   (    (    sH   lib/python2.7/site-packages/notebook/services/sessions/sessionmanager.pyR"   V   s    c         C   s2   |  j  j d | | | | | f ƒ |  j d | ƒ S(   sü  Saves the items for the session with the given session_id
        
        Given a session_id (and any other of the arguments), this method
        creates a row in the sqlite session database that holds the information
        for a session.
        
        Parameters
        ----------
        session_id : str
            uuid for the session; this method must be given a session_id
        path : str
            the path for the given session
        name: str
            the name of the session
        type: string
            the type of the session
        kernel_id : str
            a uuid for the kernel associated with this session
        
        Returns
        -------
        model : dict
            a dictionary of the session model
        s&   INSERT INTO session VALUES (?,?,?,?,?)R   (   R   R   t   get_session(   R   R   R   R	   R
   R   (    (    sH   lib/python2.7/site-packages/notebook/services/sessions/sessionmanager.pyR$   a   s    c   	      K   s8  | s t  d ƒ ‚ n  g  } xF | j ƒ  D]8 } | |  j k rO t  d | ƒ ‚ n  | j d | ƒ q( Wd d j | ƒ } |  j j | t | j ƒ  ƒ ƒ y |  j j	 ƒ  } Wn t
 k
 rÂ d
 } n X| d
 k r+g  } x1 | j ƒ  D]# \ } } | j d | | f ƒ qâ Wt j d d d	 j | ƒ ƒ ‚ n  |  j | ƒ S(   sO  Returns the model for a particular session.
        
        Takes a keyword argument and searches for the value in the session
        database, then returns the rest of the session's info.

        Parameters
        ----------
        **kwargs : keyword argument
            must be given one of the keywords and values from the session database
            (i.e. session_id, path, name, type, kernel_id)

        Returns
        -------
        model : dict
            returns a dictionary that includes all the information from the 
            session described by the kwarg.
        s   must specify a column to querys   No such column: %rs   %s=?s   SELECT * FROM session WHERE %ss    AND s   %s=%ri”  u   Session not found: %ss   , N(   t	   TypeErrort   keyst   _columnst   appendt   joinR   R   t   listt   valuesR   t   KeyErrorR   t   itemsR   t	   HTTPErrort   row_to_model(	   R   t   kwargst
   conditionst   columnt   queryt   rowt   qt   keyt   value(    (    sH   lib/python2.7/site-packages/notebook/services/sessions/sessionmanager.pyR-      s&    
"c         K   s§   |  j  d | ƒ | s d Sg  } xG | j ƒ  D]9 } | |  j k rU t d | ƒ ‚ n  | j d | ƒ q- Wd d j | ƒ } |  j j | t | j	 ƒ  ƒ | g ƒ d S(   s  Updates the values in the session database.
        
        Changes the values of the session with the given session_id
        with the values from the keyword arguments. 
        
        Parameters
        ----------
        session_id : str
            a uuid that identifies a session in the sqlite3 database
        **kwargs : str
            the key must correspond to a column title in session database,
            and the value replaces the current value in the session 
            with session_id.
        R   Ns   No such column: %rs   %s=?s(   UPDATE session SET %s WHERE session_id=?s   , (
   R-   R/   R0   R.   R1   R2   R   R   R3   R4   (   R   R   R9   t   setsR;   R<   (    (    sH   lib/python2.7/site-packages/notebook/services/sessions/sessionmanager.pyt   update_session¬   s    c         C   s¶   | d |  j  k r6 |  j j d | d f ƒ t ‚ n  i | d d 6| d d 6| d d 6| d d 6|  j  j | d ƒ d 6} | d d	 k r² i | d d 6| d d 6| d	 <n  | S(
   s@   Takes sqlite database session row and turns it into a dictionaryR   s&   DELETE FROM session WHERE session_id=?R   t   idR   R	   R
   t   kernelt   notebook(   R!   R   R   R5   t   kernel_model(   R   R=   t   model(    (    sH   lib/python2.7/site-packages/notebook/services/sessions/sessionmanager.pyR8   É   s    	#c         C   sa   |  j  j d ƒ } g  } xB | j ƒ  D]4 } y | j |  j | ƒ ƒ Wq% t k
 rX q% Xq% W| S(   s_   Returns a list of dictionaries containing all the information from
        the session databases   SELECT * FROM session(   R   R   t   fetchallR1   R8   R5   (   R   t   cR'   R=   (    (    sH   lib/python2.7/site-packages/notebook/services/sessions/sessionmanager.pyt   list_sessionsß   s    c         c   sN   |  j  d | ƒ } t j |  j j | d d ƒ ƒ V|  j j d | f ƒ d S(   s=   Deletes the row in the session database with given session_idR   RD   RC   s&   DELETE FROM session WHERE session_id=?N(   R-   R   R#   R!   t   shutdown_kernelR   R   (   R   R   t   session(    (    sH   lib/python2.7/site-packages/notebook/services/sessions/sessionmanager.pyt   delete_sessioní   s    "N(   t   __name__t
   __module__R   R!   R)   R   R   R   R0   t   propertyR   R   R   R   R   R    R   t	   coroutineR(   R"   R$   R-   RB   R8   RJ   RM   (    (    (    sH   lib/python2.7/site-packages/notebook/services/sessions/sessionmanager.pyR      s(   							-			(   t   __doc__t   osR   R   t   ImportErrort	   pysqlite2R    t   tornadoR   R   t   traitlets.config.configurableR   t   ipython_genutils.py3compatR   t	   traitletsR   R   (    (    (    sH   lib/python2.7/site-packages/notebook/services/sessions/sessionmanager.pyt   <module>   s   