2018-09-01 17:58:20 +00:00
|
|
|
import glob, imp, inspect, os, sys, uuid
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2018-09-19 11:35:34 +00:00
|
|
|
BITBOT_HOOKS_MAGIC = "__bitbot_hooks"
|
|
|
|
|
2018-09-19 12:28:18 +00:00
|
|
|
class BaseModule(object):
|
|
|
|
def __init__(self, bot, events, exports):
|
|
|
|
pass
|
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
class ModuleManager(object):
|
2018-09-02 18:54:45 +00:00
|
|
|
def __init__(self, bot, events, exports, directory="modules"):
|
2016-03-29 11:56:58 +00:00
|
|
|
self.bot = bot
|
2018-08-31 11:55:52 +00:00
|
|
|
self.events = events
|
2018-09-02 18:54:45 +00:00
|
|
|
self.exports = exports
|
2016-03-29 11:56:58 +00:00
|
|
|
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
|
|
|
|
2018-09-01 10:29:26 +00:00
|
|
|
def _module_name(self, path):
|
|
|
|
return os.path.basename(path).rsplit(".py", 1)[0].lower()
|
|
|
|
def _module_path(self, name):
|
|
|
|
return os.path.join(self.directory, "%s.py" % name)
|
2016-04-04 11:36:59 +00:00
|
|
|
|
2018-09-01 10:29:26 +00:00
|
|
|
def _load_module(self, name):
|
|
|
|
path = self._module_path(name)
|
2017-07-12 09:00:27 +00:00
|
|
|
|
2018-09-01 10:29:26 +00:00
|
|
|
with open(path) as module_file:
|
2016-03-29 11:56:58 +00:00
|
|
|
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([])
|
2018-09-01 10:29:26 +00:00
|
|
|
self.waiting_requirement[line_split[1].lower()].add(path)
|
2016-04-04 11:36:59 +00:00
|
|
|
return None
|
2016-03-29 11:56:58 +00:00
|
|
|
else:
|
|
|
|
break
|
2018-09-01 10:29:26 +00:00
|
|
|
module = imp.load_source(name, path)
|
2018-08-31 11:55:52 +00:00
|
|
|
|
2018-01-09 15:35:33 +00:00
|
|
|
if not hasattr(module, "Module"):
|
|
|
|
raise ImportError("module '%s' doesn't have a Module class.")
|
|
|
|
if not inspect.isclass(module.Module):
|
|
|
|
raise ImportError("module '%s' has a Module attribute but it is not a class.")
|
2018-08-31 11:55:52 +00:00
|
|
|
|
2018-09-02 18:54:45 +00:00
|
|
|
context = str(uuid.uuid4())
|
2018-09-19 11:35:34 +00:00
|
|
|
context_events = self.events.new_context(context)
|
|
|
|
context_exports = self.exports.new_context(context)
|
|
|
|
module_object = module.Module(self.bot, context_events,
|
|
|
|
context_exports)
|
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
if not hasattr(module_object, "_name"):
|
|
|
|
module_object._name = name.title()
|
2018-09-19 11:35:34 +00:00
|
|
|
for attribute_name in dir(module_object):
|
|
|
|
attribute = getattr(module_object, attribute_name)
|
|
|
|
if inspect.ismethod(attribute) and hasattr(attribute,
|
|
|
|
BITBOT_HOOKS_MAGIC):
|
|
|
|
hooks = getattr(attribute, BITBOT_HOOKS_MAGIC)
|
|
|
|
for hook in hooks:
|
|
|
|
context_events.on(hook["event"]).hook(attribute,
|
|
|
|
**hook["kwargs"])
|
|
|
|
|
2018-09-02 18:54:45 +00:00
|
|
|
module_object._context = context
|
2018-08-31 11:55:52 +00:00
|
|
|
module_object._import_name = 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
|
|
|
|
2018-09-01 10:29:26 +00:00
|
|
|
def load_module(self, name):
|
2017-09-05 09:03:38 +00:00
|
|
|
try:
|
2018-09-01 10:29:26 +00:00
|
|
|
module = self._load_module(name)
|
2017-09-05 09:03:38 +00:00
|
|
|
except ImportError as e:
|
2018-09-01 10:29:26 +00:00
|
|
|
self.bot.log.error("failed to load module \"%s\": %s",
|
|
|
|
[name, e.msg])
|
2017-09-05 09:03:38 +00:00
|
|
|
return
|
2016-04-04 11:36:59 +00:00
|
|
|
if module:
|
2018-08-31 11:55:52 +00:00
|
|
|
self.modules[module._import_name] = module
|
2016-04-04 11:36:59 +00:00
|
|
|
if name in self.waiting_requirement:
|
2018-09-01 10:29:26 +00:00
|
|
|
for requirement_name in self.waiting_requirement:
|
|
|
|
self.load_module(requirement_name)
|
|
|
|
self.bot.log.info("Module '%s' loaded", [name])
|
2016-04-04 11:36:59 +00:00
|
|
|
else:
|
2018-09-01 10:29:26 +00:00
|
|
|
self.bot.log.error("Module '%s' not loaded", [name])
|
2017-09-05 09:03:38 +00:00
|
|
|
|
2018-09-13 12:35:05 +00:00
|
|
|
def load_modules(self, whitelist=[], blacklist=[]):
|
2018-09-01 10:29:26 +00:00
|
|
|
for path in self.list_modules():
|
|
|
|
name = self._module_name(path)
|
2018-09-13 12:35:05 +00:00
|
|
|
if name in whitelist or (not whitelist and not name in blacklist):
|
2018-09-01 10:29:26 +00:00
|
|
|
self.load_module(name)
|
|
|
|
|
|
|
|
def unload_module(self, name):
|
|
|
|
module = self.modules[name]
|
|
|
|
del self.modules[name]
|
|
|
|
|
2018-09-02 18:54:45 +00:00
|
|
|
context = module._context
|
|
|
|
self.events.purge_context(context)
|
|
|
|
self.exports.purge_context(context)
|
2018-09-01 10:29:26 +00:00
|
|
|
|
|
|
|
del sys.modules[name]
|
2018-09-01 17:49:50 +00:00
|
|
|
references = sys.getrefcount(module)
|
2018-09-01 10:29:26 +00:00
|
|
|
del module
|
2018-09-01 17:49:50 +00:00
|
|
|
references -= 1 # 'del module' removes one reference
|
|
|
|
references -= 1 # one of the refs is from getrefcount
|
|
|
|
|
|
|
|
self.bot.log.info("Module '%s' unloaded (%d reference%s)",
|
|
|
|
[name, references, "" if references == 1 else "s"])
|