9
9
from logging import Logger
10
10
from pathlib import Path
11
11
from time import time_ns
12
- from typing import TYPE_CHECKING , Any , ClassVar
12
+ from typing import TYPE_CHECKING , Any
13
13
14
14
from importlib_metadata import entry_points
15
15
from jupyterlab_chat .models import Message
@@ -60,7 +60,6 @@ class PersonaManager(LoggingConfigurable):
60
60
_personas : dict [str , BasePersona ]
61
61
file_id : str
62
62
63
-
64
63
def __init__ (
65
64
self ,
66
65
* args ,
@@ -319,7 +318,7 @@ def load_from_dir(root_dir: str, log: Logger) -> list[type[BasePersona]]:
319
318
`PersonaManager`.
320
319
321
320
Scans the root_dir for .py files containing `persona` in their name that do
322
- _not_ start with a single `_` (i.e. private modules are skipped). Then, it
321
+ _not_ start with a single `_` (i.e. private modules are skipped). Then, it
323
322
dynamically imports them, and extracts any class declarations that are
324
323
subclasses of `BasePersona`.
325
324
@@ -343,22 +342,26 @@ def load_from_dir(root_dir: str, log: Logger) -> list[type[BasePersona]]:
343
342
py_files = []
344
343
for f in all_py_files :
345
344
fname_lower = Path (f ).stem .lower ()
346
- if "persona" in fname_lower and not (fname_lower .startswith ("_" ) or fname_lower .startswith ("." )):
345
+ if "persona" in fname_lower and not (
346
+ fname_lower .startswith ("_" ) or fname_lower .startswith ("." )
347
+ ):
347
348
py_files .append (f )
348
349
349
350
except Exception as e :
350
351
# On exception with glob operation, return empty list
351
- log .error (f"{ type (e ).__name__ } occurred while searching for Python files in { root_dir } " )
352
+ log .error (
353
+ f"{ type (e ).__name__ } occurred while searching for Python files in { root_dir } "
354
+ )
352
355
return persona_classes
353
356
354
357
if py_files :
355
358
log .info (f"Found files from { root_dir } : { [Path (f ).name for f in py_files ]} " )
356
-
359
+
357
360
# Temporarily add root_dir to sys.path for imports
358
361
root_dir_in_path = root_dir in sys .path
359
362
if not root_dir_in_path :
360
363
sys .path .insert (0 , root_dir )
361
-
364
+
362
365
try :
363
366
# For each .py file, dynamically import the module and extract all
364
367
# BasePersona subclasses.
@@ -386,14 +389,15 @@ def load_from_dir(root_dir: str, log: Logger) -> list[type[BasePersona]]:
386
389
log .info (f"Found persona class '{ obj .__name__ } ' in '{ py_file } '" )
387
390
persona_classes .append (obj )
388
391
389
- except Exception as e :
392
+ except Exception :
390
393
# On exception, log error and continue to next file
391
- log .exception (f"Unable to load persona classes from '{ py_file } ', exception details printed below." )
394
+ log .exception (
395
+ f"Unable to load persona classes from '{ py_file } ', exception details printed below."
396
+ )
392
397
continue
393
398
finally :
394
399
# Remove root_dir from sys.path if we added it
395
400
if not root_dir_in_path and root_dir in sys .path :
396
401
sys .path .remove (root_dir )
397
402
398
403
return persona_classes
399
-
0 commit comments