nonebot2/nonebot/plugin/export.py

65 lines
1.6 KiB
Python
Raw Normal View History

2022-01-22 15:23:07 +08:00
"""本模块定义了插件导出的内容对象。
在新版插件系统中推荐优先使用直接 import 所需要的插件内容
FrontMatter:
sidebar_position: 4
description: nonebot.plugin.export 模块
"""
import warnings
2021-11-08 01:02:35 +08:00
from . import _current_plugin
2021-03-31 20:38:00 +08:00
class Export(dict):
2022-01-22 15:23:07 +08:00
"""插件导出内容以使得其他插件可以获得。
2021-03-31 20:38:00 +08:00
2022-01-12 18:53:30 +08:00
用法:
```python
2021-03-31 20:38:00 +08:00
nonebot.export().default = "bar"
@nonebot.export()
def some_function():
pass
# this doesn't work before python 3.9
# use
# export = nonebot.export(); @export.sub
# instead
# See also PEP-614: https://www.python.org/dev/peps/pep-0614/
@nonebot.export().sub
def something_else():
pass
2022-01-12 18:53:30 +08:00
```
2021-03-31 20:38:00 +08:00
"""
def __call__(self, func, **kwargs):
self[func.__name__] = func
self.update(kwargs)
return func
def __setitem__(self, key, value):
super().__setitem__(key, Export(value) if isinstance(value, dict) else value)
2021-03-31 20:38:00 +08:00
def __setattr__(self, name, value):
self[name] = Export(value) if isinstance(value, dict) else value
def __getattr__(self, name):
if name not in self:
self[name] = Export()
return self[name]
def export() -> Export:
2022-01-22 15:23:07 +08:00
"""获取当前插件的导出内容对象"""
warnings.warn(
"nonebot.export() is deprecated. "
"See https://github.com/nonebot/nonebot2/issues/935.",
DeprecationWarning,
)
2021-11-08 01:02:35 +08:00
plugin = _current_plugin.get()
if not plugin:
raise RuntimeError("Export outside of the plugin!")
return plugin.export