o
    iv+                     @   s   d Z ddlZzddlZW n ey   ddlmZ Y nw ddlmZmZ ddl	m
Z
 ddlmZ ddlmZ ddlmZ dd	lmZ G d
d de
ZdS )zA base class session manager.    N)dbapi2)genweb)LoggingConfigurable)unicode_type)Instance)maybe_future)InstanceFromClassesc                   @   s   e Zd ZedZeddgdZdZdZh dZ	e
dd Ze
d	d
 Zdd Zdd Zejdd Zdd Zejd&ddZejdd Zejd'ddZejdd Zejdd Zdd Zejd(d d!Zejd"d# Zejd$d% ZdS ))SessionManagerz<notebook.services.kernels.kernelmanager.MappingKernelManagerz2notebook.services.contents.manager.ContentsManagerz8jupyter_server.services.contents.manager.ContentsManager)klassesN>   namepathtype	kernel_id
session_idc                 C   s(   | j du r| j | _ | j d | j S )z5Start a cursor and create a database called 'session'NzNCREATE TABLE session
                (session_id, path, name, type, kernel_id))_cursor
connectioncursorexecuteself r   d/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/notebook/services/sessions/sessionmanager.pyr   +   s   
zSessionManager.cursorc                 C   s&   | j du rtd| _ tj| j _| j S )zStart a database connectionNz:memory:)_connectionsqlite3connectRowrow_factoryr   r   r   r   r   4   s   

zSessionManager.connectionc                 C   s"   | j dur| j   d| _ dS dS )zClose the sqlite connectionN)r   closer   r   r   r   r   <   s   


zSessionManager.closec                 C   s   |    dS )z+Close connection once SessionManager closesN)r   r   r   r   r   __del__B      zSessionManager.__del__c                 c   sR    d}| j d|f | j  }|dur$t| j|ddV }|dur$d}t|)z2Check to see if the session of a given name existsFz"SELECT * FROM session WHERE path=?NT)tolerate_culled)r   r   fetchoner   row_to_modelr   Return)r   r   existsrowmodelr   r   r   session_existsF   s   

zSessionManager.session_existsc                 C   s   t t S )zCreate a uuid for a new session)r   uuiduuid4r   r   r   r   new_session_idW   r    zSessionManager.new_session_idc                 c   sV    |   }|dur|| jv rn
| |||||V }t| j|||||dV }t|)z'Creates a session and returns its modelN)r   r   r   r   )r+   kernel_managerstart_kernel_for_sessionr   save_sessionr   r$   )r   r   r   r   kernel_namer   r   resultr   r   r   create_session[   s   
zSessionManager.create_sessionc                 c   s0    | j j|d}t| jj||dV }t|)z'Start a new kernel for a given session.)r   )r   r/   )contents_managerget_kernel_pathr   r,   start_kernelr   r$   )r   r   r   r   r   r/   kernel_pathr   r   r   r   r-   i   s   
z'SessionManager.start_kernel_for_sessionc                 c   s6    | j d|||||f t| j|dV }t|)a  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
        z&INSERT INTO session VALUES (?,?,?,?,?)r   )r   r   r   get_sessionr   r$   )r   r   r   r   r   r   r0   r   r   r   r.   t   s   
zSessionManager.save_sessionc              
   k   s$   |st dg }| D ]}|| jvrt d||| d qdd| }| j|t|  z| j	 }W n t
yG   d}Y nw |du rmg }| D ]\}}|| d| qRtdd	d
| z
t| |V }	W n t
y }
 z	tdd	|
 d}
~
ww t|	)aF  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.
        zmust specify a column to queryzNo such column: %r=?zSELECT * FROM session WHERE z AND N=i  zSession not found: , )	TypeErrorkeys_columnsappendjoinr   r   listvaluesr"   KeyErroritemsr   	HTTPErrorr   r#   r   r$   )r   kwargs
conditionscolumnqueryr&   qkeyvaluer'   er   r   r   r7      s6   


zSessionManager.get_sessionc                 k   s    t | j|dV  |sdS g }| D ]}|| jvr"td||| d qdd| d}| j|t	|
 |g  dS )a  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.
        r6   NzNo such column: r8   zUPDATE session SET r:   z WHERE session_id=?)r   r7   r<   r=   r;   r>   r?   r   r   r@   rA   )r   r   rE   setsrG   rH   r   r   r   update_session   s   
 zSessionManager.update_sessionc                 C   s
   || j vS )zRChecks if the kernel is still considered alive and returns true if its not found. )r,   )r   r   r   r   r   kernel_culled   s   
zSessionManager.kernel_culledFc                 c   s    t | |d V }|r5| jd|d f dj|d |d d}|r1| j|d  tdt	|t | j
|d V }|d |d |d	 |d
 |d}|d
 dkra|d |d	 d|d< t|)z@Takes sqlite database session row and turns it into a dictionaryr   &DELETE FROM session WHERE session_id=?r   zKernel '{kernel_id}' appears to have been culled or died unexpectedly, invalidating session '{session_id}'. The session has been removed.)r   r   z  Continuing...Nr   r   r   )idr   r   r   kernelnotebook)r   r   )r   rO   r   r   formatlogwarningr   r$   rB   r,   kernel_model)r   r&   r!   rO   msgrW   r'   r   r   r   r#      s.   

zSessionManager.row_to_modelc              	   c   sZ    | j d}g }| D ]}zt| |V }|| W q ty'   Y qw t|)z_Returns a list of dictionaries containing all the information from
        the session databasezSELECT * FROM session)	r   r   fetchallr   r#   r>   rB   r   r$   )r   cr0   r&   r'   r   r   r   list_sessions  s   
zSessionManager.list_sessionsc                 c   sB    t | j|dV }t | j|d d V  | jd|f dS )z=Deletes the row in the session database with given session_idr6   rR   rQ   rP   N)r   r7   r,   shutdown_kernelr   r   )r   r   sessionr   r   r   delete_session  s   zSessionManager.delete_session)NNNNN)NNNN)F)__name__
__module____qualname__r   r,   r	   r2   r   r   r=   propertyr   r   r   r   r   	coroutiner(   r+   r1   r-   r.   r7   rN   rO   r#   r[   r^   r   r   r   r   r
      sH    





1
"
r
   )__doc__r)   r   ImportError	pysqlite2r   tornador   r   traitlets.config.configurabler   ipython_genutils.py3compatr   	traitletsr   notebook.utilsr   notebook.traittypesr	   r
   r   r   r   r   <module>   s    