1
1
"""A base class session manager."""
2
+
2
3
# Copyright (c) Jupyter Development Team.
3
4
# Distributed under the terms of the Modified BSD License.
4
5
import os
5
6
import pathlib
6
7
import uuid
8
+ from typing import Any , Dict , List , NewType , Optional , Union
9
+
10
+ KernelName = NewType ("KernelName" , str )
11
+ ModelName = NewType ("ModelName" , str )
7
12
8
13
try :
9
14
import sqlite3
12
17
from pysqlite2 import dbapi2 as sqlite3 # type:ignore[no-redef]
13
18
14
19
from dataclasses import dataclass , fields
15
- from typing import Union
16
20
17
21
from jupyter_core .utils import ensure_async
18
22
from tornado import web
@@ -39,8 +43,8 @@ class KernelSessionRecord:
39
43
associated with them.
40
44
"""
41
45
42
- session_id : Union [ None , str ] = None
43
- kernel_id : Union [ None , str ] = None
46
+ session_id : Optional [ str ] = None
47
+ kernel_id : Optional [ str ] = None
44
48
45
49
def __eq__ (self , other : object ) -> bool :
46
50
"""Whether a record equals another."""
@@ -98,7 +102,9 @@ class KernelSessionRecordList:
98
102
it will be appended.
99
103
"""
100
104
101
- def __init__ (self , * records ):
105
+ _records : List [KernelSessionRecord ]
106
+
107
+ def __init__ (self , * records : KernelSessionRecord ):
102
108
"""Initialize a record list."""
103
109
self ._records = []
104
110
for record in records :
@@ -252,14 +258,26 @@ async def session_exists(self, path):
252
258
exists = True
253
259
return exists
254
260
255
- def new_session_id (self ):
261
+ def new_session_id (self ) -> str :
256
262
"""Create a uuid for a new session"""
257
263
return str (uuid .uuid4 ())
258
264
259
265
async def create_session (
260
- self , path = None , name = None , type = None , kernel_name = None , kernel_id = None
261
- ):
262
- """Creates a session and returns its model"""
266
+ self ,
267
+ path : Optional [str ] = None ,
268
+ name : Optional [ModelName ] = None ,
269
+ type : Optional [str ] = None ,
270
+ kernel_name : Optional [KernelName ] = None ,
271
+ kernel_id : Optional [str ] = None ,
272
+ ) -> Dict [str , Any ]:
273
+ """Creates a session and returns its model
274
+
275
+ name: ModelName(str)
276
+ Usually the model name, like the filename associated with current
277
+ kernel.
278
+
279
+
280
+ """
263
281
session_id = self .new_session_id ()
264
282
record = KernelSessionRecord (session_id = session_id )
265
283
self ._pending_sessions .update (record )
@@ -277,15 +295,52 @@ async def create_session(
277
295
self ._pending_sessions .remove (record )
278
296
return result
279
297
280
- def get_kernel_env (self , path ):
281
- """Return the environment variables that need to be set in the kernel"""
298
+ def get_kernel_env (
299
+ self , path : Optional [str ], name : Optional [ModelName ] = None
300
+ ) -> Dict [str , str ]:
301
+ """Return the environment variables that need to be set in the kernel
302
+
303
+ path : str
304
+ the url path for the given session.
305
+ name: ModelName(str), optional
306
+ Here the name is likely to be the name of the associated file
307
+ with the current kernel at startup time.
308
+
309
+
310
+ """
311
+ if name is not None :
312
+ cwd = self .kernel_manager .cwd_for_path (path )
313
+ path = os .path .join (cwd , name )
314
+ assert isinstance (path , str )
282
315
return {** os .environ , "JPY_SESSION_NAME" : path }
283
316
284
- async def start_kernel_for_session (self , session_id , path , name , type , kernel_name ):
285
- """Start a new kernel for a given session."""
317
+ async def start_kernel_for_session (
318
+ self ,
319
+ session_id : str ,
320
+ path : Optional [str ],
321
+ name : Optional [ModelName ],
322
+ type : Optional [str ],
323
+ kernel_name : Optional [KernelName ],
324
+ ) -> str :
325
+ """Start a new kernel for a given session.
326
+
327
+ session_id : str
328
+ uuid for the session; this method must be given a session_id
329
+ path : str
330
+ the path for the given session - seem to be a session id sometime.
331
+ name : str
332
+ Usually the model name, like the filename associated with current
333
+ kernel.
334
+ type : str
335
+ the type of the session
336
+ kernel_name : str
337
+ the name of the kernel specification to use. The default kernel name will be used if not provided.
338
+
339
+ """
286
340
# allow contents manager to specify kernels cwd
287
341
kernel_path = await ensure_async (self .contents_manager .get_kernel_path (path = path ))
288
- kernel_env = self .get_kernel_env (path )
342
+
343
+ kernel_env = self .get_kernel_env (path , name )
289
344
kernel_id = await self .kernel_manager .start_kernel (
290
345
path = kernel_path ,
291
346
kernel_name = kernel_name ,
@@ -306,9 +361,9 @@ async def save_session(self, session_id, path=None, name=None, type=None, kernel
306
361
uuid for the session; this method must be given a session_id
307
362
path : str
308
363
the path for the given session
309
- name: str
364
+ name : str
310
365
the name of the session
311
- type: string
366
+ type : str
312
367
the type of the session
313
368
kernel_id : str
314
369
a uuid for the kernel associated with this session
@@ -405,13 +460,13 @@ async def update_session(self, session_id, **kwargs):
405
460
query = "UPDATE session SET %s WHERE session_id=?" % (", " .join (sets ))
406
461
self .cursor .execute (query , list (kwargs .values ()) + [session_id ])
407
462
408
- def kernel_culled (self , kernel_id ) :
463
+ async def kernel_culled (self , kernel_id : str ) -> bool :
409
464
"""Checks if the kernel is still considered alive and returns true if its not found."""
410
465
return kernel_id not in self .kernel_manager
411
466
412
467
async def row_to_model (self , row , tolerate_culled = False ):
413
468
"""Takes sqlite database session row and turns it into a dictionary"""
414
- kernel_culled = await ensure_async (self .kernel_culled (row ["kernel_id" ]))
469
+ kernel_culled : bool = await ensure_async (self .kernel_culled (row ["kernel_id" ]))
415
470
if kernel_culled :
416
471
# The kernel was culled or died without deleting the session.
417
472
# We can't use delete_session here because that tries to find
0 commit comments