2016-04-14 15:47:33 +00:00
|
|
|
import gc, 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 = {}
|
2016-04-04 11:36:59 +00:00
|
|
|
self.waiting_requirement = {}
|
2016-03-29 11:56:58 +00:00
|
|
|
def list_modules(self):
|
|
|
|
return sorted(glob.glob(os.path.join(self.directory, "*.py")))
|
2016-04-04 11:36:59 +00:00
|
|
|
|
|
|
|
def module_name(self, filename):
|
|
|
|
return os.path.basename(filename).rsplit(".py", 1)[0].lower()
|
|
|
|
|
|
|
|
def _load_module(self, filename):
|
|
|
|
name = self.module_name(filename)
|
2017-07-12 09:00:27 +00:00
|
|
|
|
|
|
|
whitelist = self.bot.config.get("module_whitelist", [])
|
|
|
|
if whitelist and name not in whitelist: return
|
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
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:
|
2016-04-06 15:03:38 +00:00
|
|
|
if not line_split[1].lower() in self.bot.config or not self.bot.config[
|
|
|
|
line_split[1].lower()]:
|
2016-03-30 18:31:23 +00:00
|
|
|
# nope, required config option not present.
|
|
|
|
return None
|
2016-04-04 11:36:59 +00:00
|
|
|
elif line_split[0] == "#--require-module" and len(
|
|
|
|
line_split) > 1:
|
|
|
|
if not "bitbot_%s" % line_split[1].lower() in sys.modules:
|
|
|
|
if not line_split[1].lower() in self.waiting_requirement:
|
|
|
|
self.waiting_requirement[line_split[1].lower()] = set([])
|
|
|
|
self.waiting_requirement[line_split[1].lower()].add(filename)
|
|
|
|
return None
|
2016-03-29 11:56:58 +00:00
|
|
|
else:
|
|
|
|
break
|
2016-04-14 15:47:33 +00:00
|
|
|
import_name = "bitbot_%s" % name
|
|
|
|
module = imp.load_source(import_name, filename)
|
2016-03-29 11:56:58 +00:00
|
|
|
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()
|
2016-04-14 15:47:33 +00:00
|
|
|
module_object._is_unloaded = False
|
|
|
|
module_object._import_name = import_name
|
2016-04-04 11:36:59 +00:00
|
|
|
assert not module_object._name in self.modules, (
|
|
|
|
"module name '%s' attempted to be used twice.")
|
2016-03-29 11:56:58 +00:00
|
|
|
return module_object
|
2016-04-04 11:36:59 +00:00
|
|
|
|
|
|
|
def load_module(self, filename):
|
|
|
|
name = self.module_name(filename)
|
2017-09-05 09:03:38 +00:00
|
|
|
try:
|
|
|
|
module = self._load_module(filename)
|
|
|
|
except ImportError as e:
|
|
|
|
sys.stderr.write("module '%s' not loaded: Could not resolve import.\n" % filename)
|
|
|
|
return
|
2016-04-04 11:36:59 +00:00
|
|
|
if module:
|
|
|
|
self.modules[module._name] = module
|
|
|
|
if name in self.waiting_requirement:
|
|
|
|
for filename in self.waiting_requirement:
|
|
|
|
self.load_module(filename)
|
2017-07-12 09:00:27 +00:00
|
|
|
sys.stderr.write("module '%s' loaded.\n" % filename)
|
2016-04-04 11:36:59 +00:00
|
|
|
else:
|
|
|
|
sys.stderr.write("module '%s' not loaded.\n" % filename)
|
2017-09-05 09:03:38 +00:00
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
def load_modules(self):
|
|
|
|
for filename in self.list_modules():
|
2016-04-04 11:36:59 +00:00
|
|
|
self.load_module(filename)
|
2016-04-14 15:47:33 +00:00
|
|
|
|
|
|
|
def unload_module(self, module):
|
|
|
|
# this is such a bad idea
|
|
|
|
module._is_unloaded = True
|
|
|
|
self.unhook_check(self.bot.events)
|
2016-04-18 15:47:12 +00:00
|
|
|
if hasattr(module, "_cleanup"):
|
|
|
|
module._cleanup()
|
2016-04-14 15:47:33 +00:00
|
|
|
del sys.modules[module._import_name]
|
|
|
|
del self.modules[module._name]
|
|
|
|
del module
|
|
|
|
gc.collect()
|
|
|
|
|
|
|
|
def unhook_check(self, event):
|
|
|
|
for hook in event.get_hooks():
|
|
|
|
if hasattr(hook.function, "__self__") and hasattr(
|
|
|
|
hook.function.__self__, "_is_unloaded"
|
|
|
|
) and hook.function.__self__._is_unloaded:
|
|
|
|
event._unhook(hook)
|
|
|
|
for child in event.get_children():
|
|
|
|
self.unhook_check(event.get_child(child))
|