X-Git-Url: https://git.kaliko.me/?a=blobdiff_plain;f=sid%2Fsid.py;h=b9c7df71c55373cfaa989f8ef5582a576ab9fa4d;hb=a19f40b0a9329c7f3c3cf56d7deb5a4bc1ef32ff;hp=534c3a0cce58b02b1ce308681b406f4da3375e8d;hpb=9864db6fe18d6f4cba01f89a8c39dc2f3c8342f1;p=sid.git diff --git a/sid/sid.py b/sid/sid.py index 534c3a0..b9c7df7 100644 --- a/sid/sid.py +++ b/sid/sid.py @@ -23,9 +23,15 @@ import traceback import slixmpp +from sid import __url__ + def botcmd(*args, **kwargs): - """Decorator for bot command functions""" + """Decorator for bot command functions + + :param bool hidden: is the command hidden in global help + :param str name: command name, default to decorated function name + """ def decorate(func, hidden=False, name=None): setattr(func, '_bot_command', True) @@ -42,13 +48,25 @@ def botcmd(*args, **kwargs): class MUCBot(slixmpp.ClientXMPP): - + """ + :param str jid: jid to log with + :param str password: jid password + :param str room: conference room to join + :param str nick: Nickname to use in the room + """ + + #: Class attribute to define bot's command prefix + #: + #: Defaults to "!" prefix = '!' def __init__(self, jid, password, room, nick, log_file=None, log_level=logging.INFO): super(MUCBot, self).__init__(jid, password) + # Clean sphinx autodoc for self documentation + # (cf. MUCBot.help) + self.__doc__ = None self.log = logging.getLogger(__package__) self.plugins = list() self.commands = dict() @@ -56,10 +74,10 @@ class MUCBot(slixmpp.ClientXMPP): self.nick = nick self.__set_logger(log_file, log_level) self.__seen = dict() - self.register_plugin('xep_0030') # Service Discovery - self.register_plugin('xep_0045') # Multi-User Chat - self.register_plugin('xep_0071') # xhtml-im - self.register_plugin('xep_0199') # self Ping + self.register_plugin('xep_0030') # Service Discovery + self.register_plugin('xep_0045') # Multi-User Chat + self.register_plugin('xep_0071') # xhtml-im + self.register_plugin('xep_0199') # self Ping # The session_start event will be triggered when # the bot establishes its connection with the server @@ -92,7 +110,10 @@ class MUCBot(slixmpp.ClientXMPP): self.log.debug('set logger, log level : %s', log_level) def message(self, msg): - """Messages handler""" + """Messages handler + + Parses message received to detect :py:obj:`prefix` + """ if msg['type'] not in ('groupchat', 'chat'): self.log.warning('Unhandled message') return @@ -133,10 +154,8 @@ class MUCBot(slixmpp.ClientXMPP): requesting the roster and broadcasting an initial presence stanza. - Arguments: - event -- An empty dictionary. The session_start - event does not provide any additional - data. + :param dict event: An empty dictionary. The session_start + event does not provide any additional data. """ self.get_roster() self.send_presence() @@ -146,8 +165,9 @@ class MUCBot(slixmpp.ClientXMPP): # password=the_room_password, wait=True) - def register_bot_plugin(self, plugin_class): - self.plugins.append(plugin_class(self)) + :param `sid.plugin.Plugin` plugin_cls: A :py:obj:`sid.plugin.Plugin` class + """ + self.plugins.append(plugin_cls(self)) for name, value in inspect.getmembers(self.plugins[-1]): if inspect.ismethod(value) and getattr(value, '_bot_command', False): @@ -173,8 +193,8 @@ class MUCBot(slixmpp.ClientXMPP): Automatically assigned to the "help" command.""" help_cmd = ('Type {}help '.format(self.prefix) + - ' to get more info about that specific command.\n\n'+ - 'SRC: http://git.kaliko.me/sid.git') + ' to get more info about that specific command.\n\n' + + f'SRC: {__url__}') if not args: if self.__doc__: description = self.__doc__.strip() @@ -193,7 +213,8 @@ class MUCBot(slixmpp.ClientXMPP): text = '{}\n\n{}'.format(description, usage) else: if args[0] in self.commands.keys(): - text = self.commands[args[0]].__doc__.strip() or 'undocumented' + text = self.commands[args[0]].__doc__ or 'undocumented' + text = inspect.cleandoc(text) else: text = 'That command is not defined.' if message['type'] == 'groupchat':