pyircbot/pyircbot/modulebase.py

77 lines
2.3 KiB
Python
Raw Normal View History

"""
.. module:: ModuleBase
2015-11-01 18:03:11 -08:00
:synopsis: Base class that modules will extend
.. moduleauthor:: Dave Pedu <dave@davepedu.com>
"""
2013-12-28 09:58:20 -08:00
import logging
2015-06-18 20:43:17 -07:00
from .pyircbot import PyIRCBot
2013-12-28 09:58:20 -08:00
2017-01-01 14:01:23 -08:00
2013-12-28 09:58:20 -08:00
class ModuleBase:
2015-11-01 18:03:11 -08:00
"""All modules will extend this class
2017-01-01 14:01:23 -08:00
2015-11-01 18:03:11 -08:00
:param bot: A reference to the main bot passed when this module is created
:type bot: PyIRCBot
:param moduleName: The name assigned to this module
:type moduleName: str
"""
2017-01-01 14:01:23 -08:00
2015-11-01 18:03:11 -08:00
def __init__(self, bot, moduleName):
2017-01-01 14:59:01 -08:00
self.moduleName = moduleName
2015-11-01 18:03:11 -08:00
"""Assigned name of this module"""
2017-01-01 14:01:23 -08:00
2015-11-01 18:03:11 -08:00
self.bot = bot
"""Reference to the master PyIRCBot object"""
2017-01-01 14:01:23 -08:00
2017-01-01 14:59:01 -08:00
self.hooks = []
2015-11-01 18:03:11 -08:00
"""Hooks (aka listeners) this module has"""
2017-01-01 14:01:23 -08:00
2017-01-01 14:59:01 -08:00
self.services = []
2015-11-01 18:03:11 -08:00
"""If this module provides services usable by another module, they're listed
here"""
2017-01-01 14:01:23 -08:00
2017-01-01 14:59:01 -08:00
self.config = {}
2015-11-01 18:03:11 -08:00
"""Configuration dictionary. Autoloaded from `%(datadir)s/%(modulename)s.json`"""
2017-01-01 14:01:23 -08:00
2015-11-01 18:03:11 -08:00
self.log = logging.getLogger("Module.%s" % self.moduleName)
"""Logger object for this module"""
2017-01-01 14:01:23 -08:00
2015-11-01 18:03:11 -08:00
# Autoload config if available
self.loadConfig()
2017-01-01 14:01:23 -08:00
2015-11-01 18:03:11 -08:00
self.log.info("Loaded module %s" % self.moduleName)
2017-01-01 14:01:23 -08:00
2015-11-01 18:03:11 -08:00
def loadConfig(self):
"""Loads this module's config into self.config"""
2017-01-01 14:01:23 -08:00
configPath = self.getConfigPath()
if configPath is not None:
2015-11-01 18:03:11 -08:00
self.config = PyIRCBot.load(configPath)
2017-01-01 14:01:23 -08:00
2015-11-01 18:03:11 -08:00
def ondisable(self):
"""Called when the module should be disabled. Your module should do any sort
of clean-up operations here like ending child threads or saving data files.
"""
pass
2017-01-01 14:01:23 -08:00
2015-11-01 18:03:11 -08:00
def getConfigPath(self):
"""Returns the absolute path of this module's json config file"""
return self.bot.getConfigPath(self.moduleName)
2017-01-01 14:01:23 -08:00
2015-11-01 18:03:11 -08:00
def getFilePath(self, f=None):
"""Returns the absolute path to a file in this Module's data dir
2017-01-01 14:01:23 -08:00
2015-11-01 18:03:11 -08:00
:param f: The file name included in the path
:type channel: str
:Warning: .. Warning:: this does no error checking if the file exists or is\
writable. The bot's data dir *should* always be writable"""
return self.bot.getDataPath(self.moduleName) + (f if f else '')
2015-06-20 00:30:12 -07:00
2017-01-01 14:59:01 -08:00
2013-12-28 09:58:20 -08:00
class ModuleHook:
2015-11-01 18:03:11 -08:00
def __init__(self, hook, method):
2017-01-01 14:59:01 -08:00
self.hook = hook
self.method = method