2016-03-30 18:31:23 +00:00
|
|
|
import glob, imp, inspect, os, sys
|
2016-03-29 11:56:58 +00:00
|
|
|
|
|
|
|
class ModuleManager(object):
|
|
|
|
def __init__(self, bot, directory="modules"):
|
|
|
|
self.bot = bot
|
|
|
|
self.directory = directory
|
|
|
|
self.modules = {}
|
|
|
|
def list_modules(self):
|
|
|
|
return sorted(glob.glob(os.path.join(self.directory, "*.py")))
|
|
|
|
def load_module(self, filename):
|
|
|
|
name = os.path.basename(filename).rsplit(".py", 1)[0]
|
|
|
|
with open(filename) as module_file:
|
|
|
|
while True:
|
|
|
|
line = module_file.readline().strip()
|
2016-03-30 18:31:23 +00:00
|
|
|
line_split = line.split(" ")
|
2016-03-29 11:56:58 +00:00
|
|
|
if line and line.startswith("#--"):
|
|
|
|
# this is a hashflag
|
|
|
|
if line == "#--ignore":
|
|
|
|
# nope, ignore this module.
|
|
|
|
return None
|
2016-03-30 18:31:23 +00:00
|
|
|
elif line_split[0] == "#--require-config" and len(
|
|
|
|
line_split) > 1:
|
|
|
|
if not line_split[1].lower() in self.bot.config:
|
|
|
|
# nope, required config option not present.
|
|
|
|
return None
|
2016-03-29 11:56:58 +00:00
|
|
|
else:
|
|
|
|
break
|
|
|
|
module = imp.load_source("bitbot_%s" % name, filename)
|
|
|
|
assert hasattr(module, "Module"
|
|
|
|
), "module '%s' doesn't have a Module class."
|
|
|
|
assert inspect.isclass(module.Module
|
|
|
|
), "module '%s' has a Module attribute but it is not a class."
|
|
|
|
module_object = module.Module(self.bot)
|
|
|
|
if not hasattr(module_object, "_name"):
|
|
|
|
module_object._name = name.title()
|
|
|
|
return module_object
|
|
|
|
def load_modules(self):
|
|
|
|
for filename in self.list_modules():
|
|
|
|
module = self.load_module(filename)
|
|
|
|
if module:
|
|
|
|
assert not module._name in self.modules, (
|
|
|
|
"module name '%s' attempted to be used twice.")
|
|
|
|
self.modules[module._name] = module
|
2016-03-30 18:31:23 +00:00
|
|
|
else:
|
|
|
|
sys.stderr.write("module '%s' not loaded.\n" % filename)
|