From 40091588e4b37efbd3906b3417d1e56b0e9463d6 Mon Sep 17 00:00:00 2001 From: dave Date: Thu, 2 Oct 2014 16:53:54 -0700 Subject: [PATCH] Documenting more things --- .gitignore | 1 + docs/.DS_Store | Bin 0 -> 6148 bytes docs/._.DS_Store | Bin 0 -> 4096 bytes docs/api/jsonrpc.rst | 11 ++ docs/api/modulebase.rst | 4 +- docs/api/modules/services.rst | 9 ++ docs/api/rpc.rst | 13 ++- pyircbot/core/modulebase.py | 40 +++++-- pyircbot/core/pyircbot.py | 214 +++++++++++++++++++++++++++------- pyircbot/core/rpc.py | 78 ++++++++++--- pyircbot/modules/Error.py | 8 ++ pyircbot/modules/Services.py | 8 ++ 12 files changed, 320 insertions(+), 66 deletions(-) create mode 100644 docs/.DS_Store create mode 100644 docs/._.DS_Store create mode 100644 docs/api/jsonrpc.rst create mode 100644 docs/api/modules/services.rst diff --git a/.gitignore b/.gitignore index 53ed46e..ae4c2e3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ libs *__pycache__* docs/_build +.DS_Store diff --git a/docs/.DS_Store b/docs/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..6997a9a56f7687f629542fd96b968314447a63ae GIT binary patch literal 6148 zcmeHK%Wl&^6ur|VZOo$tiA6yyyyKxrG;Kj-g%!f88zdvTpeVKDsIlaDtk`LULPUgw z#PdH8tdaN(mMr=ad;)jwOj}Q4kh(z#=1MbXd>?aWj^ddZ09b1jGyrM)+W@ zsa#oJsalqG&RV@-J$3th>}FmzN?YFWiFkVGWqn_bA8{TXk3Ii!*c*3hm+td8^TN0{ zq~b8>Vao1K7zcdZ;-ff7cU|e+34^RplPU+&R;^BLZ8F)svsJenx0}7)em^^+j!E?&42TUm_VyPaEEck<9+mtHJ8AH1VU|9P`Pyy$*k*wt+%2#I1K<$guPj2obdK-q zVnzY)+#LCRD*m~Qf_0Q&h`M&?_;1jDDN?|zY6|#Nq=2UaDeg1F{U$oss*7B?XaS!V zUC=le)p;G9_*ACejq2G`RheX?fKlLoP(YjyHa3Bd!kI)mIpUUMD(CBS&Ar2nfk zCHYtr_y2=mfB)-6=E*2v6!@nUVCA;kZsC^H-MX+jao5_|_OJ;Pc{7Prf}OgKX&|oR dHEd)Uv&8_>Q8<%`9yIeufMhV8QQ(g%@DuVN@m&A_ literal 0 HcmV?d00001 diff --git a/docs/._.DS_Store b/docs/._.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..4363dec6463f3970694bf1981eac71afc1b28f91 GIT binary patch literal 4096 zcmZQz6=P>$Vqox1Ojhs@R)|o50+1L3ClDJkFz{^v(m+1nBL)UWIk*Y|peR=07!nc$ z)SwB`!BBx!(Wu~P2#kinXb6mkz-S1JhQMeDjE2By2#kinXb6mkz-S0iIRqGi=7BI6 z$c1EN7Aq8`7U!21C8sK+> "+text) self.send( (text+"\r\n").encode("ascii")) @@ -143,7 +159,10 @@ class PyIRCBot(asynchat.async_chat): self.log.warning("Send attempted while disconnected. >> "+text) def process_data(self, data): - " called per line of irc sent through " + """Process one line of tet irc sent us + + :param data: the data to process + :type data: str""" if data.strip() == "": return @@ -172,12 +191,9 @@ class PyIRCBot(asynchat.async_chat): self.fire_hook(command, args=args, prefix=prefix, trailing=trailing) - - - - " Module related code " def initHooks(self): + """Defines hooks that modules can listen for events of""" self.hooks = [ '_CONNECT', # Called when the bot connects to IRC on the socket level 'NOTICE', # :irc.129irc.com NOTICE AUTH :*** Looking up your hostname... @@ -218,6 +234,17 @@ class PyIRCBot(asynchat.async_chat): self.hookcalls[command]=[] def fire_hook(self, command, args=None, prefix=None, trailing=None): + """Run any listeners for a specific hook + + :param command: the hook to fire + :type command: str + :param args: the list of arguments, if any, the command was passed + :type args: list + :param prefix: prefix of the sender of this command + :type prefix: str + :param trailing: data payload of the command + :type trailing: str""" + for hook in self.hookcalls[command]: try: hook(args, prefix, trailing) @@ -225,7 +252,7 @@ class PyIRCBot(asynchat.async_chat): self.log.warning("Error processing hook: \n%s"% self.trace()) def initModules(self): - " load modules specified in config " + """load modules specified in instance config""" " storage of imported modules " self.modules = {} " instances of modules " @@ -239,7 +266,10 @@ class PyIRCBot(asynchat.async_chat): self.loadmodule(modulename) def importmodule(self, name): - " import a module by name " + """Import a module + + :param moduleName: Name of the module to import + :type moduleName: str""" " check if already exists " if not name in self.modules: " attempt to load " @@ -257,7 +287,10 @@ class PyIRCBot(asynchat.async_chat): return (False, "Module already imported") def deportmodule(self, name): - " remove a module from memory by name " + """Remove a module's code from memory. If the module is loaded it will be unloaded silently. + + :param moduleName: Name of the module to import + :type moduleName: str""" " unload if necessary " if name in self.moduleInstances: self.unloadmodule(name) @@ -271,7 +304,10 @@ class PyIRCBot(asynchat.async_chat): del sys.modules[name] def loadmodule(self, name): - " load a module and activate it " + """Activate a module. + + :param moduleName: Name of the module to activate + :type moduleName: str""" " check if already loaded " if name in self.moduleInstances: self.log.warning( "Module %s already loaded" % name ) @@ -287,7 +323,10 @@ class PyIRCBot(asynchat.async_chat): self.loadModuleHooks(self.moduleInstances[name]) def unloadmodule(self, name): - " unload a module by name " + """Deactivate a module. + + :param moduleName: Name of the module to deactivate + :type moduleName: str""" if name in self.moduleInstances: " notify the module of disabling " self.moduleInstances[name].ondisable() @@ -304,7 +343,10 @@ class PyIRCBot(asynchat.async_chat): return (False, "Module not loaded") def reloadmodule(self, name): - " unload then load a module by name " + """Deactivate and activate a module. + + :param moduleName: Name of the target module + :type moduleName: str""" " make sure it's imporeted" if name in self.modules: " remember if it was loaded before" @@ -319,7 +361,10 @@ class PyIRCBot(asynchat.async_chat): return (False, "Module is not loaded") def redomodule(self, name): - " reload a modules code from disk " + """Reload a running module from disk + + :param moduleName: Name of the target module + :type moduleName: str""" " remember if it was loaded before " loadedbefore = name in self.moduleInstances " unload/deport " @@ -334,16 +379,30 @@ class PyIRCBot(asynchat.async_chat): return (True, None) def loadModuleHooks(self, module): + """**Internal.** Enable (connect) hooks of a module + + :param module: module object to hook in + :type module: object""" " activate a module's hooks " for hook in module.hooks: self.addHook(hook.hook, hook.method) def unloadModuleHooks(self, module): + """**Internal.** Disable (disconnect) hooks of a module + + :param module: module object to unhook + :type module: object""" " remove a modules hooks " for hook in module.hooks: self.removeHook(hook.hook, hook.method) def addHook(self, command, method): + """**Internal.** Enable (connect) a single hook of a module + + :param command: command this hook will trigger on + :type command: str + :param method: callable method object to hook in + :type method: object""" " add a single hook " if command in self.hooks: self.hookcalls[command].append(method) @@ -352,6 +411,12 @@ class PyIRCBot(asynchat.async_chat): return False def removeHook(self, command, method): + """**Internal.** Disable (disconnect) a single hook of a module + + :param command: command this hook triggers on + :type command: str + :param method: callable method that should be removed + :type method: object""" " remove a single hook " if command in self.hooks: for hookedMethod in self.hookcalls[command]: @@ -362,13 +427,21 @@ class PyIRCBot(asynchat.async_chat): return False def getmodulebyname(self, name): - " return a module specified by the name " + """Get a module object by name + + :param name: name of the module to return + :type name: str + :returns: object -- the module object""" if not name in self.moduleInstances: return None return self.moduleInstances[name] def getmodulesbyservice(self, service): - " get a list of modules that provide the specified service " + """Get a list of modules that provide the specified service + + :param service: name of the service searched for + :type service: str + :returns: list -- a list of module objects""" validModules = [] for module in self.moduleInstances: if service in self.moduleInstances[module].services: @@ -376,13 +449,18 @@ class PyIRCBot(asynchat.async_chat): return validModules def getBestModuleForService(self, service): + """Get the first module that provides the specified service + + :param service: name of the service searched for + :type service: str + :returns: object -- the module object, if found. None if not found.""" m = self.getmodulesbyservice(service) if len(m)>0: return m[0] return None def closeAllModules(self): - " Deport all modules (for shutdown). Modules are unloaded in the opposite order listed in the config. " + """ Deport all modules (for shutdown). Modules are unloaded in the opposite order listed in the config. """ loaded = list(self.moduleInstances.keys()) loadOrder = self.botconfig["modules"] loadOrder.reverse() @@ -395,17 +473,29 @@ class PyIRCBot(asynchat.async_chat): " Filesystem Methods " def getDataPath(self, moduleName): + """Return the absolute path for a module's data dir + + :param moduleName: the module who's data dir we want + :type moduleName: str""" if not os.path.exists("%s/data/%s" % (self.botconfig["bot"]["datadir"], moduleName)): os.mkdir("%s/data/%s/" % (self.botconfig["bot"]["datadir"], moduleName)) return "%s/data/%s/" % (self.botconfig["bot"]["datadir"], moduleName) def getConfigPath(self, moduleName): + """Return the absolute path for a module's config file + + :param moduleName: the module who's config file we want + :type moduleName: str""" return "%s/config/%s.yml" % (self.botconfig["bot"]["datadir"], moduleName) " Utility methods " @staticmethod def decodePrefix(prefix): - " Returns an object with nick, username, hostname attributes" + """Given a prefix like nick!username@hostname, return an object with these properties + + :param prefix: the prefix to disassemble + :type prefix: str + :returns: object -- an UserPrefix object with the properties `nick`, `username`, `hostname` or a ServerPrefix object with the property `hostname`""" if "!" in prefix: ob = type('UserPrefix', (object,), {}) ob.nick, prefix = prefix.split("!") @@ -418,10 +508,19 @@ class PyIRCBot(asynchat.async_chat): @staticmethod def trace(): + """Return the stack trace of the bot as a string""" return traceback.format_exc() @staticmethod def messageHasCommand(command, message, requireArgs=False): + """Check if a message has a command with or without args in it + + :param command: the command string to look for, like !ban + :type command: str + :param message: the message string to look in, like "!ban Lil_Mac" + :type message: str + :param requireArgs: if true, only validate if the command use has any amount of trailing text + :type requireArgs: bool""" # Check if the message at least starts with the command messageBeginning = message[0:len(command)] if messageBeginning!=command: @@ -452,26 +551,63 @@ class PyIRCBot(asynchat.async_chat): " Data Methods " def get_nick(self): + """Get the bot's current nick + + :returns: str - the bot's current nickname""" return self.config["nick"] " Action Methods " def act_PONG(self, data): + """Use the `/pong` command - respond to server pings + + :param data: the string or number the server sent with it's ping + :type data: str""" self.sendRaw("PONG :%s" % data) def act_USER(self, username, hostname, realname): + """Use the USER protocol command. Used during connection + + :param username: the bot's username + :type username: str + :param hostname: the bot's hostname + :type hostname: str + :param realname: the bot's realname + :type realname: str""" self.sendRaw("USER %s %s %s :%s" % (username, hostname, self.botconfig["connection"]["server"], realname)) def act_NICK(self, newNick): + """Use the `/nick` command + + :param newNick: new nick for the bot + :type newNick: str""" self.sendRaw("NICK %s" % newNick) def act_JOIN(self, channel): + """Use the `/join` command + + :param channel: the channel to attempt to join + :type channel: str""" self.sendRaw("JOIN %s"%channel) def act_PRIVMSG(self, towho, message): + """Use the `/msg` command + + :param towho: the target #channel or user's name + :type towho: str + :param message: the message to send + :type message: str""" self.sendRaw("PRIVMSG %s :%s"%(towho,message)) def act_MODE(self, channel, mode, extra=None): + """Use the `/mode` command + + :param channel: the channel this mode is for + :type channel: str + :param mode: the mode string. Example: +b + :type mode: str + :param extra: additional argument if the mode needs it. Example: user@*!* + :type extra: str""" if extra != None: self.sendRaw("MODE %s %s %s" % (channel,mode,extra)) else: @@ -480,22 +616,20 @@ class PyIRCBot(asynchat.async_chat): def act_ACTION(self, channel, action): """Use the `/me ` command - :param channel: The channel name or target's name the message is sent to + :param channel: the channel name or target's name the message is sent to :type channel: str - :param action: The text to send - :type action: str - """ + :param action: the text to send + :type action: str""" self.sendRaw("PRIVMSG %s :\x01ACTION %s"%(channel,action)) def act_KICK(self, channel, who, comment=""): """Use the `/kick ` command - :param channel: The channel from which the user will be kicked + :param channel: the channel from which the user will be kicked :type channel: str - :param who: The nickname of the user to kick + :param who: the nickname of the user to kick :type action: str - :param comment: The kick message - :type comment: str - """ + :param comment: the kick message + :type comment: str""" self.sendRaw("KICK %s %s :%s" % (channel, who, comment)) diff --git a/pyircbot/core/rpc.py b/pyircbot/core/rpc.py index b64a1c0..72d97e6 100644 --- a/pyircbot/core/rpc.py +++ b/pyircbot/core/rpc.py @@ -32,63 +32,115 @@ class BotRPC(Thread): self.start() def run(self): + """Internal, starts the RPC server""" self.server.serve() def importModule(self, moduleName): + """Import a module + + :param moduleName: Name of the module to import + :type moduleName: str""" self.log.info("RPC: calling importModule(%s)"%moduleName) return self.bot.importmodule(moduleName) def deportModule(self, moduleName): + """Remove a module's code from memory. If the module is loaded it will be unloaded silently. + + :param moduleName: Name of the module to import + :type moduleName: str""" self.log.info("RPC: calling deportModule(%s)"%moduleName) self.bot.deportmodule(moduleName) def loadModule(self, moduleName): + """Activate a module. + + :param moduleName: Name of the module to activate + :type moduleName: str""" self.log.info("RPC: calling loadModule(%s)"%moduleName) return self.bot.loadmodule(moduleName) def unloadModule(self, moduleName): + """Deactivate a module. + + :param moduleName: Name of the module to deactivate + :type moduleName: str""" self.log.info("RPC: calling unloadModule(%s)"%moduleName) self.bot.unloadmodule(moduleName) def reloadModule(self, moduleName): + """Deactivate and activate a module. + + :param moduleName: Name of the target module + :type moduleName: str""" self.log.info("RPC: calling reloadModule(%s)"%moduleName) self.bot.unloadmodule(moduleName) return self.bot.loadmodule(moduleName) def redoModule(self, moduleName): + """Reload a running module from disk + + :param moduleName: Name of the target module + :type moduleName: str""" self.log.info("RPC: calling redoModule(%s)"%moduleName) return self.bot.redomodule(moduleName) def getLoadedModules(self): + """Return a list of active modules + + :returns: list -- ['ModuleName1', 'ModuleName2']""" self.log.info("RPC: calling getLoadedModules()") return list(self.bot.moduleInstances.keys()) - def pluginCommand(self, pluginName, methodName, argList): - plugin = self.bot.getmodulebyname(pluginName) + def pluginCommand(self, moduleName, methodName, argList): + """Run a method of an active module + + :param moduleName: Name of the target module + :type moduleName: str + :param methodName: Name of the target method + :type methodName: str + :param argList: List of positional arguments to call the method with + :type argList: list + :returns: mixed -- Any basic type the target method may return""" + plugin = self.bot.getmodulebyname(moduleName) if not plugin: return (False, "Plugin not found") method = getattr(plugin, methodName) if not method: return (False, "Method not found") - self.log.info("RPC: calling %s.%s(%s)" % (pluginName, methodName, argList)) + self.log.info("RPC: calling %s.%s(%s)" % (moduleName, methodName, argList)) return (True, method(*argList)) - def getPluginVar(self, pluginName, pluginVarName): - plugin = self.bot.getmodulebyname(pluginName) - if pluginName == "_core": + def getPluginVar(self, moduleName, moduleVarName): + """Extract a property from an active module and return it + + :param moduleName: Name of the target module + :type moduleName: str + :param moduleVarName: Name of the target property + :type moduleVarName: str + :returns: mixed -- Any basic type extracted from an active module""" + plugin = self.bot.getmodulebyname(moduleName) + if moduleName == "_core": plugin = self.bot if not plugin: return (False, "Plugin not found") - self.log.info("RPC: getting %s.%s" % (pluginName, pluginVarName)) - return (True, getattr(plugin, pluginVarName)) + self.log.info("RPC: getting %s.%s" % (moduleName, moduleVarName)) + return (True, getattr(plugin, moduleVarName)) - def setPluginVar(self, pluginName, pluginVarName, value): - plugin = self.bot.getmodulebyname(pluginName) - if pluginName == "_core": + def setPluginVar(self, moduleName, moduleVarName, value): + """Set a property of an active module + + :param moduleName: Name of the target module + :type moduleName: str + :param moduleVarName: Name of the target property + :type moduleVarName: str + :param value: Value the target property will be set to + :type value: str""" + plugin = self.bot.getmodulebyname(moduleName) + if moduleName == "_core": plugin = self.bot if not plugin: return (False, "Plugin not found") - self.log.info("RPC: setting %s.%s = %s )" % (pluginName, pluginVarName, value)) - setattr(plugin, pluginVarName, value) + self.log.info("RPC: setting %s.%s = %s )" % (moduleName, moduleVarName, value)) + setattr(plugin, moduleVarName, value) return (True, "Var set") diff --git a/pyircbot/modules/Error.py b/pyircbot/modules/Error.py index ee4d1c7..7708518 100644 --- a/pyircbot/modules/Error.py +++ b/pyircbot/modules/Error.py @@ -15,6 +15,14 @@ class Error(ModuleBase): self.hooks=[ModuleHook("PRIVMSG", self.error)] def error(self, args, prefix, trailing): + """If the message recieved from IRC has the string "error" in it, cause a ZeroDivisionError + + :param args: IRC args received + :type args: list + :param prefix: IRC prefix of sender + :type prefix: str + :param trailing: IRC message body + :type trailing: str""" if "error" in trailing: print(10/0) diff --git a/pyircbot/modules/Services.py b/pyircbot/modules/Services.py index 040b7ee..7e6e13e 100644 --- a/pyircbot/modules/Services.py +++ b/pyircbot/modules/Services.py @@ -1,4 +1,12 @@ #!/usr/bin/env python +""" +.. module:: Services + :synopsis: Provides the ability to configure a nickname, password, channel auto-join + +.. moduleauthor:: Dave Pedu + +""" + from modulebase import ModuleBase,ModuleHook from time import sleep