Add some docstrings

This commit is contained in:
Richard Chien 2018-07-04 19:50:42 +08:00
parent 1f4e5f3435
commit e639889944
4 changed files with 23 additions and 19 deletions

View File

@ -67,17 +67,30 @@ class NoneBot(CQHttp):
_bot = None _bot = None
def init(config_object: Any = None) -> NoneBot: def init(config_object: Any = None) -> None:
"""
Initialize NoneBot instance.
This function must be called at the very beginning of code,
otherwise the get_bot() function will return None and nothing
is gonna work properly.
:param config_object: configuration object
"""
global _bot global _bot
_bot = NoneBot(config_object) _bot = NoneBot(config_object)
if _bot.config.DEBUG: if _bot.config.DEBUG:
logger.setLevel(logging.DEBUG) logger.setLevel(logging.DEBUG)
else: else:
logger.setLevel(logging.INFO) logger.setLevel(logging.INFO)
return _bot
def get_bot() -> NoneBot: def get_bot() -> NoneBot:
"""
Get the NoneBot instance.
:raise ValueError: instance not initialized
"""
if _bot is None: if _bot is None:
raise ValueError('NoneBot instance has not been initialized') raise ValueError('NoneBot instance has not been initialized')
# noinspection PyTypeChecker # noinspection PyTypeChecker
@ -85,6 +98,7 @@ def get_bot() -> NoneBot:
def run(host: str = None, port: int = None, *args, **kwargs) -> None: def run(host: str = None, port: int = None, *args, **kwargs) -> None:
"""Run the NoneBot instance."""
get_bot().run(host=host, port=port, *args, **kwargs) get_bot().run(host=host, port=port, *args, **kwargs)

View File

@ -6,9 +6,7 @@ from . import NoneBot, expression
def context_id(ctx: Dict[str, Any]) -> str: def context_id(ctx: Dict[str, Any]) -> str:
""" """Calculate a unique id representing the current user."""
Calculate a unique id representing the current user.
"""
src = '' src = ''
if ctx.get('group_id'): if ctx.get('group_id'):
src += f'/group/{ctx["group_id"]}' src += f'/group/{ctx["group_id"]}'
@ -22,9 +20,7 @@ def context_id(ctx: Dict[str, Any]) -> str:
async def send(bot: NoneBot, ctx: Dict[str, Any], async def send(bot: NoneBot, ctx: Dict[str, Any],
message: Union[str, Dict[str, Any], List[Dict[str, Any]]], message: Union[str, Dict[str, Any], List[Dict[str, Any]]],
*, ignore_failure: bool = True) -> None: *, ignore_failure: bool = True) -> None:
""" """Send a message ignoring failure by default."""
Send a message ignoring failure by default.
"""
try: try:
if ctx.get('post_type') == 'message': if ctx.get('post_type') == 'message':
await bot.send(ctx, message) await bot.send(ctx, message)
@ -46,7 +42,5 @@ async def send(bot: NoneBot, ctx: Dict[str, Any],
async def send_expr(bot: NoneBot, ctx: Dict[str, Any], async def send_expr(bot: NoneBot, ctx: Dict[str, Any],
expr: Union[str, Sequence[str], Callable], expr: Union[str, Sequence[str], Callable],
**kwargs): **kwargs):
""" """Sending a expression message ignoring failure by default."""
Sending a expression message ignoring failure by default.
"""
return await send(bot, ctx, expression.render(expr, **kwargs)) return await send(bot, ctx, expression.render(expr, **kwargs))

View File

@ -14,16 +14,12 @@ class BaseSession:
async def send(self, async def send(self,
message: Union[str, Dict[str, Any], List[Dict[str, Any]]], message: Union[str, Dict[str, Any], List[Dict[str, Any]]],
*, ignore_failure: bool = True) -> None: *, ignore_failure: bool = True) -> None:
""" """Send a message ignoring failure by default."""
Send a message ignoring failure by default.
"""
return await send(self.bot, self.ctx, message, return await send(self.bot, self.ctx, message,
ignore_failure=ignore_failure) ignore_failure=ignore_failure)
async def send_expr(self, async def send_expr(self,
expr: Union[str, Sequence[str], Callable], expr: Union[str, Sequence[str], Callable],
**kwargs): **kwargs):
""" """Sending a expression message ignoring failure by default."""
Sending a expression message ignoring failure by default.
"""
return await send_expr(self.bot, self.ctx, expr, **kwargs) return await send_expr(self.bot, self.ctx, expr, **kwargs)

View File

@ -3,8 +3,8 @@ from os import path
import none import none
from none_demo import config from none_demo import config
bot = none.init(config) none.init(config)
app = bot.asgi app = none.get_bot().asgi
if __name__ == '__main__': if __name__ == '__main__':
none.load_builtin_plugins() none.load_builtin_plugins()