@@ -182,7 +182,15 @@ def on_comm_open(self, comm, open_msg) -> None:
182
182
183
183
def receive_message (self , msg ) -> None :
184
184
"""
185
- Handle messages sent by the client to the positron.environment comm.
185
+ Handle messages received from the client via the positron.environment comm.
186
+
187
+ Message Types:
188
+ "inspect" - Inspect the user variable at the requested path
189
+ "refresh" - Refresh the list of user variables in the environment
190
+ "view" - Format the variable at the requested path for the data viewer
191
+ "clipboard_format" - Format the variable at the requested path for the client clipboard
192
+ "clear" - Clear all user variables in the environment
193
+ "delete" - Delete user variables in the environment by name
186
194
"""
187
195
data = msg ["content" ]["data" ]
188
196
@@ -214,6 +222,10 @@ def receive_message(self, msg) -> None:
214
222
self ._send_error (f"Unknown message type '{ msgType } '" )
215
223
216
224
def send_update (self , assigned : dict , removed : set ) -> None :
225
+ """
226
+ Sends the list of variables that have changed in the current user session through the
227
+ environment comm to the client.
228
+ """
217
229
# Ensure the number of changes does not exceed our maximum items
218
230
if len (assigned ) < MAX_ITEMS and len (removed ) < MAX_ITEMS :
219
231
self ._send_update (assigned , removed )
@@ -264,7 +276,7 @@ def _send_message(self, msg: EnvironmentMessage) -> None:
264
276
265
277
self .env_comm .send (msg )
266
278
267
- def _send_error (self , message : str ) -> None :
279
+ def _send_error (self , error_message : str ) -> None :
268
280
"""
269
281
Send an error message through the envirvonment comm to the client.
270
282
@@ -277,7 +289,7 @@ def _send_error(self, message: str) -> None:
277
289
...
278
290
}
279
291
"""
280
- msg = EnvironmentMessageError (message )
292
+ msg = EnvironmentMessageError (error_message )
281
293
self ._send_message (msg )
282
294
283
295
def _send_update (self , assigned : Mapping , removed : Iterable ) -> None :
@@ -312,12 +324,24 @@ def _send_update(self, assigned: Mapping, removed: Iterable) -> None:
312
324
def _delete_all_vars (self , parent ) -> None :
313
325
"""
314
326
Deletes all of the variables in the current user session.
327
+
328
+ Args:
329
+ parent:
330
+ A dict providing the parent context for the response,
331
+ e.g. the client message requesting the clear operation
315
332
"""
316
333
self .kernel .delete_all_vars (parent )
317
334
318
335
def _delete_vars (self , names : Iterable , parent ) -> None :
319
336
"""
320
337
Deletes the requested variables by name from the current user session.
338
+
339
+ Args:
340
+ names:
341
+ A list of variable names to delete
342
+ parent:
343
+ A dict providing the parent context for the response,
344
+ e.g. the client message requesting the delete operation
321
345
"""
322
346
if names is None :
323
347
return
@@ -328,12 +352,15 @@ def _delete_vars(self, names: Iterable, parent) -> None:
328
352
def _inspect_var (self , path : Sequence ) -> None :
329
353
"""
330
354
Describes the variable at the requested path in the current user session.
355
+
356
+ Args:
357
+ path:
358
+ A list of names describing the path to the variable.
331
359
"""
332
360
if path is None :
333
361
return
334
362
335
363
is_known , value = self .kernel .find_var (path )
336
-
337
364
if is_known :
338
365
self ._send_details (path , value )
339
366
else :
@@ -357,8 +384,14 @@ def _send_formatted_var(
357
384
Formats the variable at the requested path in the current user session
358
385
using the requested clipboard format and sends the result through the
359
386
environment comm to the client.
360
- """
361
387
388
+ Args:
389
+ path:
390
+ A list of names describing the path to the variable.
391
+ clipboard_format:
392
+ The format to use for the clipboard copy, described as a mime type.
393
+ Defaults to "text/plain".
394
+ """
362
395
if path is None :
363
396
return
364
397
@@ -372,7 +405,7 @@ def _send_formatted_var(
372
405
message = f"Cannot find variable at '{ path } ' to format"
373
406
self ._send_error (message )
374
407
375
- def _send_details (self , path : Sequence , context : Any = None ):
408
+ def _send_details (self , path : Sequence , value : Any = None ):
376
409
"""
377
410
Sends a detailed list of children of the value (or just the value
378
411
itself, if is a leaf node on the path) as a message through the
@@ -397,15 +430,21 @@ def _send_details(self, path: Sequence, context: Any = None):
397
430
}
398
431
...
399
432
}
433
+
434
+ Args:
435
+ path:
436
+ A list of names describing the path to the variable.
437
+ value:
438
+ The variable's value to summarize.
400
439
"""
401
440
402
441
children = []
403
- inspector = get_inspector (context )
404
- if inspector is not None and inspector .has_children (context ):
405
- children = inspector .summarize_children (context , self ._summarize_variable )
442
+ inspector = get_inspector (value )
443
+ if inspector .has_children (value ):
444
+ children = inspector .summarize_children (value , self ._summarize_variable )
406
445
else :
407
446
# Otherwise, treat as a simple value at given path
408
- summary = self ._summarize_variable ("" , context )
447
+ summary = self ._summarize_variable ("" , value )
409
448
if summary is not None :
410
449
children .append (summary )
411
450
# TODO: Handle scalar objects with a specific message type
@@ -414,11 +453,19 @@ def _send_details(self, path: Sequence, context: Any = None):
414
453
self ._send_message (msg )
415
454
416
455
def _summarize_variables (self , variables : Mapping , max_items : int = MAX_ITEMS ) -> list :
456
+ """
457
+ Summarizes the given variables into a list of EnvironmentVariable objects.
458
+
459
+ Args:
460
+ variables:
461
+ A mapping of variable names to values.
462
+ max_items:
463
+ The maximum number of items to summarize.
464
+ """
417
465
summaries = []
418
466
419
467
for key , value in variables .items ():
420
- # Ensure the number of items summarized is within our
421
- # max limit
468
+ # Ensure the number of items summarized is within our max limit
422
469
if len (summaries ) >= max_items :
423
470
break
424
471
@@ -428,22 +475,28 @@ def _summarize_variables(self, variables: Mapping, max_items: int = MAX_ITEMS) -
428
475
429
476
return summaries
430
477
431
- def _summarize_variable (self , key , value ) -> Optional [EnvironmentVariable ]:
478
+ def _summarize_variable (self , key : Any , value : Any ) -> Optional [EnvironmentVariable ]:
479
+ """
480
+ Summarizes the given variable into an EnvironmentVariable object.
481
+
482
+ Returns:
483
+ An EnvironmentVariable summary, or None if the variable should be skipped.
484
+ """
432
485
# Hide module types for now
433
486
if isinstance (value , types .ModuleType ):
434
487
return None
435
488
436
- display_name = str (key )
437
-
438
489
try :
439
490
# Use an inspector to summarize the value
440
491
ins = get_inspector (value )
441
492
493
+ display_name = ins .get_display_name (key )
442
494
kind_str = ins .get_kind (value )
443
495
kind = getattr (EnvironmentVariableKind , kind_str .upper ())
444
496
display_value , is_truncated = ins .get_display_value (value )
445
497
display_type = ins .get_display_type (value )
446
498
type_info = ins .get_type_info (value )
499
+ access_key = ins .get_access_key (key )
447
500
length = ins .get_length (value )
448
501
size = ins .get_size (value )
449
502
has_children = ins .has_children (value )
@@ -455,7 +508,7 @@ def _summarize_variable(self, key, value) -> Optional[EnvironmentVariable]:
455
508
display_type = display_type ,
456
509
kind = kind ,
457
510
type_info = type_info ,
458
- access_key = display_name ,
511
+ access_key = access_key ,
459
512
length = length ,
460
513
size = size ,
461
514
has_children = has_children ,
@@ -466,17 +519,18 @@ def _summarize_variable(self, key, value) -> Optional[EnvironmentVariable]:
466
519
except Exception as err :
467
520
logging .warning (err , exc_info = True )
468
521
return EnvironmentVariable (
469
- display_name = display_name ,
522
+ display_name = str ( key ) ,
470
523
display_value = get_qualname (value ),
471
524
kind = EnvironmentVariableKind .OTHER ,
472
525
)
473
526
474
- def _format_value (self , value , clipboard_format : ClipboardFormat ) -> str :
527
+ def _format_value (self , value : Any , clipboard_format : ClipboardFormat ) -> str :
528
+ """
529
+ Formats the given value using the requested clipboard format.
530
+ """
475
531
inspector = get_inspector (value )
476
532
477
533
if clipboard_format == ClipboardFormat .HTML :
478
534
return inspector .to_html (value )
479
- elif clipboard_format == ClipboardFormat .PLAIN :
480
- return inspector .to_tsv (value )
481
535
else :
482
- return str (value )
536
+ return inspector . to_plaintext (value )
0 commit comments