Skip to content
On this page


AliceBot 内部使用的实用工具。

class ModulePathFinder


用于查找 AliceBot 组件的元路径查找器。

  • Attributes

    • path (ClassVar[List[str]])

method __init__(self, /, *args, **kwargs) {#object.__init__}

Initialize self. See help(type(self)) for accurate signature.

  • Arguments

    • args

    • kwargs

method find_spec(self, fullname, path = None, target = None) {#ModulePathFinder.find_spec}

用于查找指定模块的 spec

  • Arguments

    • fullname (str)

    • path (Optional[Sequence[str]])

    • target (Optional[module])

  • Returns

    Type: Optional[_frozen_importlib.ModuleSpec]

function is_config_class(config_class) {#is_config_class}


  • Arguments

    • config_class (Any) - 待判断的对象。
  • Returns

    Type: typing_extensions.TypeGuard[typing.Type[alicebot.config.ConfigModel]]


function get_classes_from_module(module, super_class) {#get_classes_from_module}


  • Arguments

    • module (module) - Python 模块。

    • super_class (~_TypeT) - 要查找的类的超类。

  • Returns

    Type: List[~_TypeT]


function get_classes_from_module_name(name, super_class, *, reload = False) {#get_classes_from_module_name}


  • Arguments

    • name (str) - 模块名称,格式和 Python import 语句相同。

    • super_class (~_TypeT) - 要查找的类的超类。

    • reload (bool) - 是否重新加载模块。

  • Returns

    Type: List[Tuple[~_TypeT, module]]


  • Raises

    • ImportError - 当导入模块过程中出现错误。

class PydanticEncoder

Bases: json.encoder.JSONEncoder

用于解析 pydantic.BaseModelJSONEncoder 类。

method __init__(self, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None) {#JSONEncoder.__init__}

Constructor for JSONEncoder, with sensible defaults.

If skipkeys is false, then it is a TypeError to attempt encoding of keys that are not str, int, float or None. If skipkeys is True, such items are simply skipped.

If ensure_ascii is true, the output is guaranteed to be str objects with all incoming non-ASCII characters escaped. If ensure_ascii is false, the output can contain non-ASCII characters.

If check_circular is true, then lists, dicts, and custom encoded objects will be checked for circular references during encoding to prevent an infinite recursion (which would cause an RecursionError). Otherwise, no such check takes place.

If allow_nan is true, then NaN, Infinity, and -Infinity will be encoded as such. This behavior is not JSON specification compliant, but is consistent with most JavaScript based encoders and decoders. Otherwise, it will be a ValueError to encode such floats.

If sort_keys is true, then the output of dictionaries will be sorted by key; this is useful for regression tests to ensure that JSON serializations can be compared on a day-to-day basis.

If indent is a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0 will only insert newlines. None is the most compact representation.

If specified, separators should be an (item_separator, key_separator) tuple. The default is (', ', ': ') if indent is None and (',', ': ') otherwise. To get the most compact JSON representation, you should specify (',', '😂 to eliminate whitespace.

If specified, default is a function that gets called for objects that can't otherwise be serialized. It should return a JSON encodable version of the object or raise a TypeError.

  • Arguments

    • skipkeys

    • ensure_ascii

    • check_circular

    • allow_nan

    • sort_keys

    • indent

    • separators

    • default

method default(self, o)

返回 o 的可序列化对象。

  • Arguments

    • o (Any)
  • Returns

    Type: Any

function samefile(path1, path2)

一个 os.path.samefile 的简单包装。

  • Arguments

    • path1 (Union[str, bytes, os.PathLike[str], os.PathLike[bytes]]) - 路径1。

    • path2 (Union[str, bytes, os.PathLike[str], os.PathLike[bytes]]) - 路径2。

  • Returns

    Type: bool


function sync_func_wrapper(func, *, to_thread = False) {#sync_func_wrapper}


  • Arguments

    • func (Callable[[~_P], ~_R]) - 待包装的同步函数。

    • to_thread (bool) - 是否在独立的线程中运行同步函数。默认为 False

  • Returns

    Type: Callable[[~_P], Coroutine[NoneType, NoneType, ~_R]]


function sync_ctx_manager_wrapper(cm, *, to_thread = False) {#sync_ctx_manager_wrapper}


  • Arguments

    • cm (ContextManager[~_T]) - 待包装的同步上下文管理器。

    • to_thread (bool) - 是否在独立的线程中运行同步函数。默认为 False

  • Returns

    Type: AsyncGenerator[~_T, NoneType]


function wrap_get_func(func) {#wrap_get_func}

get() 函数接受的参数包装为一个异步函数。

  • Arguments

    • func (Optional[Callable[[~EventT], Union[bool, Awaitable[bool]]]]) - get() 函数接受的参数。
  • Returns

    Type: Callable[[~EventT], Awaitable[bool]]


function get_annotations(obj) {#get_annotations}


  • Arguments

    • obj (Union[Callable[..., object], Type[Any], module]) - 一个可调用对象、类或模块。
  • Returns

    Type: Dict[str, Any]


  • Raises

    • TypeError - obj 不是一个可调用对象、类或模块。

    • ValueError - 对象的 __annotations__ 不是一个字典或 None

Released under the MIT License.