pyircbot/pyircbot/modulebase.py

75 lines
2.0 KiB
Python
Raw Normal View History

"""
.. module:: ModuleBase
:synopsis: Base class that modules will extend
.. moduleauthor:: Dave Pedu <dave@davepedu.com>
"""
2013-12-28 09:58:20 -08:00
import logging
import os
2015-06-18 20:43:17 -07:00
from .pyircbot import PyIRCBot
2013-12-28 09:58:20 -08:00
class ModuleBase:
2014-10-02 16:53:54 -07:00
"""All modules will extend this class
: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
"""
2013-12-28 09:58:20 -08:00
def __init__(self, bot, moduleName):
self.moduleName=moduleName
2014-10-02 16:53:54 -07:00
"""Assigned name of this module"""
2013-12-28 09:58:20 -08:00
self.bot = bot
2014-10-02 16:53:54 -07:00
"""Reference to the master PyIRCBot object"""
2013-12-28 09:58:20 -08:00
self.hooks=[]
2014-10-02 16:53:54 -07:00
"""Hooks (aka listeners) this module has"""
2013-12-28 09:58:20 -08:00
self.services=[]
2014-10-02 16:53:54 -07:00
"""If this module provides services usable by another module, they're stored
here"""
2013-12-28 09:58:20 -08:00
self.config={}
2014-10-02 16:53:54 -07:00
"""Configuration dictionary. Blank until loadConfig is called"""
2013-12-28 09:58:20 -08:00
self.log = logging.getLogger("Module.%s" % self.moduleName)
2014-10-02 16:53:54 -07:00
"""Logger object for this module"""
2014-10-05 01:02:24 -07:00
self.loadConfig()
2013-12-28 09:58:20 -08:00
self.log.info("Loaded module %s" % self.moduleName)
def loadConfig(self):
2014-10-02 16:53:54 -07:00
"""Loads this module's config into self.config"""
2013-12-28 09:58:20 -08:00
configPath = self.bot.getConfigPath(self.moduleName)
2015-06-18 20:43:17 -07:00
if not configPath == None:
self.config = PyIRCBot.load(configPath)
2013-12-28 09:58:20 -08:00
def ondisable(self):
2014-10-02 16:53:54 -07:00
"""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.
"""
2013-12-28 09:58:20 -08:00
pass
def getConfigPath(self):
2014-10-02 16:53:54 -07:00
"""Returns the absolute path of this module's YML config file"""
2013-12-28 09:58:20 -08:00
return self.bot.getConfigPath(self.moduleName)
def getFilePath(self, f=None):
2014-10-02 16:53:54 -07:00
"""Returns the absolute path to a file in this Module's data dir
: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"""
2013-12-28 09:58:20 -08:00
return self.bot.getDataPath(self.moduleName) + (f if f else '')
class ModuleHook:
def __init__(self, hook, method):
self.hook=hook
self.method=method