2015-01-03 19:17:46 +00:00
|
|
|
# Nemubot is a smart and modulable IM bot.
|
|
|
|
# Copyright (C) 2012-2015 Mercier Pierre-Olivier
|
2012-08-14 03:51:55 +00:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
from importlib.abc import Finder
|
2014-12-29 06:23:11 +00:00
|
|
|
from importlib.machinery import SourceFileLoader
|
2014-08-14 10:49:38 +00:00
|
|
|
import logging
|
2012-08-14 03:51:55 +00:00
|
|
|
import os
|
|
|
|
|
2014-08-27 05:57:00 +00:00
|
|
|
logger = logging.getLogger("nemubot.importer")
|
2014-08-14 10:49:38 +00:00
|
|
|
|
2015-01-03 19:17:46 +00:00
|
|
|
|
2012-08-14 03:51:55 +00:00
|
|
|
class ModuleFinder(Finder):
|
2015-01-03 19:17:46 +00:00
|
|
|
|
2015-02-10 02:46:50 +00:00
|
|
|
def __init__(self, modules_paths, add_module):
|
|
|
|
self.modules_paths = modules_paths
|
|
|
|
self.add_module = add_module
|
2012-08-14 03:51:55 +00:00
|
|
|
|
|
|
|
def find_module(self, fullname, path=None):
|
2017-08-27 16:22:53 +00:00
|
|
|
if path is not None and fullname.startswith("nemubot.module."):
|
|
|
|
module_name = fullname.split(".", 2)[2]
|
2015-02-10 02:46:50 +00:00
|
|
|
for mpath in self.modules_paths:
|
2017-08-27 16:22:53 +00:00
|
|
|
if os.path.isfile(os.path.join(mpath, module_name + ".py")):
|
2015-02-10 02:46:50 +00:00
|
|
|
return ModuleLoader(self.add_module, fullname,
|
2017-08-27 16:22:53 +00:00
|
|
|
os.path.join(mpath, module_name + ".py"))
|
|
|
|
elif os.path.isfile(os.path.join(os.path.join(mpath, module_name), "__init__.py")):
|
2015-02-10 02:46:50 +00:00
|
|
|
return ModuleLoader(self.add_module, fullname,
|
2014-12-29 06:23:11 +00:00
|
|
|
os.path.join(
|
2017-08-27 16:22:53 +00:00
|
|
|
os.path.join(mpath, module_name),
|
2014-12-29 06:23:11 +00:00
|
|
|
"__init__.py"))
|
2012-08-14 03:51:55 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
2014-12-29 06:23:11 +00:00
|
|
|
class ModuleLoader(SourceFileLoader):
|
|
|
|
|
2015-02-10 02:46:50 +00:00
|
|
|
def __init__(self, add_module, fullname, path):
|
|
|
|
self.add_module = add_module
|
2014-12-29 06:23:11 +00:00
|
|
|
SourceFileLoader.__init__(self, fullname, path)
|
|
|
|
|
2012-08-14 03:51:55 +00:00
|
|
|
|
2015-05-22 21:56:24 +00:00
|
|
|
def _load(self, module, name):
|
2012-08-14 03:51:55 +00:00
|
|
|
# Add the module to the global modules list
|
2015-06-02 20:24:45 +00:00
|
|
|
self.add_module(module)
|
2017-08-27 16:22:53 +00:00
|
|
|
logger.info("Module '%s' successfully imported from %s.", name.split(".", 2)[2], self.path)
|
2012-08-14 03:51:55 +00:00
|
|
|
return module
|
2015-05-22 21:56:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Python 3.4
|
|
|
|
def exec_module(self, module):
|
2017-08-27 16:22:53 +00:00
|
|
|
super().exec_module(module)
|
2015-05-22 21:56:24 +00:00
|
|
|
self._load(module, module.__spec__.name)
|
|
|
|
|
|
|
|
|
|
|
|
# Python 3.3
|
|
|
|
def load_module(self, fullname):
|
2017-08-27 16:22:53 +00:00
|
|
|
module = super().load_module(fullname)
|
2015-05-22 21:56:24 +00:00
|
|
|
return self._load(module, module.__name__)
|