1818from traitlets import Instance
1919
2020from notebook .utils import maybe_future
21-
21+ from notebook . traittypes import InstanceFromClasses
2222
2323class SessionManager (LoggingConfigurable ):
2424
2525 kernel_manager = Instance ('notebook.services.kernels.kernelmanager.MappingKernelManager' )
26- contents_manager = Instance ('notebook.services.contents.manager.ContentsManager' )
27-
26+ contents_manager = InstanceFromClasses (
27+ klasses = [
28+ 'notebook.services.contents.manager.ContentsManager' ,
29+ # To make custom ContentsManagers both forward+backward
30+ # compatible, we'll relax the strictness of this trait
31+ # and allow jupyter_server contents managers to pass
32+ # through. If jupyter_server is not installed, this class
33+ # will be ignored.
34+ 'jupyter_server.services.contents.manager.ContentsManager'
35+ ]
36+ )
37+
2838 # Session database initialized below
2939 _cursor = None
3040 _connection = None
3141 _columns = {'session_id' , 'path' , 'name' , 'type' , 'kernel_id' }
32-
42+
3343 @property
3444 def cursor (self ):
3545 """Start a cursor and create a database called 'session'"""
3646 if self ._cursor is None :
3747 self ._cursor = self .connection .cursor ()
38- self ._cursor .execute ("""CREATE TABLE session
48+ self ._cursor .execute ("""CREATE TABLE session
3949 (session_id, path, name, type, kernel_id)""" )
4050 return self ._cursor
4151
@@ -46,7 +56,7 @@ def connection(self):
4656 self ._connection = sqlite3 .connect (':memory:' )
4757 self ._connection .row_factory = sqlite3 .Row
4858 return self ._connection
49-
59+
5060 def close (self ):
5161 """Close the sqlite connection"""
5262 if self ._cursor is not None :
@@ -106,11 +116,11 @@ def start_kernel_for_session(self, session_id, path, name, type, kernel_name):
106116 @gen .coroutine
107117 def save_session (self , session_id , path = None , name = None , type = None , kernel_id = None ):
108118 """Saves the items for the session with the given session_id
109-
119+
110120 Given a session_id (and any other of the arguments), this method
111121 creates a row in the sqlite session database that holds the information
112122 for a session.
113-
123+
114124 Parameters
115125 ----------
116126 session_id : str
@@ -123,7 +133,7 @@ def save_session(self, session_id, path=None, name=None, type=None, kernel_id=No
123133 the type of the session
124134 kernel_id : str
125135 a uuid for the kernel associated with this session
126-
136+
127137 Returns
128138 -------
129139 model : dict
@@ -138,7 +148,7 @@ def save_session(self, session_id, path=None, name=None, type=None, kernel_id=No
138148 @gen .coroutine
139149 def get_session (self , ** kwargs ):
140150 """Returns the model for a particular session.
141-
151+
142152 Takes a keyword argument and searches for the value in the session
143153 database, then returns the rest of the session's info.
144154
@@ -151,7 +161,7 @@ def get_session(self, **kwargs):
151161 Returns
152162 -------
153163 model : dict
154- returns a dictionary that includes all the information from the
164+ returns a dictionary that includes all the information from the
155165 session described by the kwarg.
156166 """
157167 if not kwargs :
@@ -185,17 +195,17 @@ def get_session(self, **kwargs):
185195 @gen .coroutine
186196 def update_session (self , session_id , ** kwargs ):
187197 """Updates the values in the session database.
188-
198+
189199 Changes the values of the session with the given session_id
190- with the values from the keyword arguments.
191-
200+ with the values from the keyword arguments.
201+
192202 Parameters
193203 ----------
194204 session_id : str
195205 a uuid that identifies a session in the sqlite3 database
196206 **kwargs : str
197207 the key must correspond to a column title in session database,
198- and the value replaces the current value in the session
208+ and the value replaces the current value in the session
199209 with session_id.
200210 """
201211 yield maybe_future (self .get_session (session_id = session_id ))
@@ -228,7 +238,7 @@ def row_to_model(self, row, tolerate_culled=False):
228238 # If caller wishes to tolerate culled kernels, log a warning
229239 # and return None. Otherwise, raise KeyError with a similar
230240 # message.
231- self .cursor .execute ("DELETE FROM session WHERE session_id=?" ,
241+ self .cursor .execute ("DELETE FROM session WHERE session_id=?" ,
232242 (row ['session_id' ],))
233243 msg = "Kernel '{kernel_id}' appears to have been culled or died unexpectedly, " \
234244 "invalidating session '{session_id}'. The session has been removed." .\
0 commit comments