mirror of
https://github.com/csunny/DB-GPT.git
synced 2025-10-23 01:49:58 +00:00
refactor: The first refactored version for sdk release (#907)
Co-authored-by: chengfangyin2 <chengfangyin3@jd.com>
This commit is contained in:
28
dbgpt/util/module_utils.py
Normal file
28
dbgpt/util/module_utils.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from typing import Type
|
||||
from importlib import import_module
|
||||
|
||||
|
||||
def import_from_string(module_path: str, ignore_import_error: bool = False):
|
||||
try:
|
||||
module_path, class_name = module_path.rsplit(".", 1)
|
||||
except ValueError:
|
||||
raise ImportError(f"{module_path} doesn't look like a module path")
|
||||
module = import_module(module_path)
|
||||
|
||||
try:
|
||||
return getattr(module, class_name)
|
||||
except AttributeError:
|
||||
if ignore_import_error:
|
||||
return None
|
||||
raise ImportError(
|
||||
f'Module "{module_path}" does not define a "{class_name}" attribute/class'
|
||||
)
|
||||
|
||||
|
||||
def import_from_checked_string(module_path: str, supper_cls: Type):
|
||||
cls = import_from_string(module_path)
|
||||
if not issubclass(cls, supper_cls):
|
||||
raise ImportError(
|
||||
f'Module "{module_path}" does not the subclass of {str(supper_cls)}'
|
||||
)
|
||||
return cls
|
Reference in New Issue
Block a user