]> kaliko git repositories - sid.git/blobdiff - sid/sid.py
sphinx: Add sphinx docstring
[sid.git] / sid / sid.py
index 604a63a6569d4432fcea52c8c3c3fa07c5482597..fadb79e172ec77de117f9b2696f13d437f5fa6ee 100644 (file)
@@ -2,7 +2,7 @@
 
 # Copyright (C) 2007-2012 Thomas Perl <thp.io/about>
 # Copyright (C) 2010, 2011 AnaĆ«l Verrier <elghinn@free.fr>
-# Copyright (C) 2014-2015 kaliko <kaliko@azylum.org>
+# Copyright (C) 2014, 2015, 2020 kaliko <kaliko@azylum.org>
 
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -21,11 +21,15 @@ import inspect
 import logging
 import traceback
 
-import sleekxmpp
+import slixmpp
 
 
 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)
@@ -41,8 +45,17 @@ def botcmd(*args, **kwargs):
         return lambda func: decorate(func, **kwargs)
 
 
-class MUCBot(sleekxmpp.ClientXMPP):
+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,
@@ -56,9 +69,10 @@ class MUCBot(sleekxmpp.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_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
@@ -83,14 +97,18 @@ class MUCBot(sleekxmpp.ClientXMPP):
         log_fd = open(log_file, 'w') if log_file else None
         chandler = logging.StreamHandler(log_fd)
         formatter = logging.Formatter(
-            '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
+            '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
+            )
         chandler.setFormatter(formatter)
         self.log.addHandler(chandler)
         self.log.setLevel(log_level)
         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
@@ -131,21 +149,20 @@ class MUCBot(sleekxmpp.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()
-        self.plugin['xep_0045'].joinMUC(self.room,
+        self.plugin['xep_0045'].join_muc(self.room,
                                         self.nick,
                                         # If a room password is needed, use:
                                         # 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):
@@ -171,7 +188,8 @@ class MUCBot(sleekxmpp.ClientXMPP):
 
         Automatically assigned to the "help" command."""
         help_cmd = ('Type {}help <command name>'.format(self.prefix) +
-                    ' to get more info about that specific command.')
+                    ' to get more info about that specific command.\n\n'+
+                    'SRC: http://git.kaliko.me/sid.git')
         if not args:
             if self.__doc__:
                 description = self.__doc__.strip()
@@ -190,7 +208,8 @@ class MUCBot(sleekxmpp.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':