Switch to using __init__.py as main file of directory modules, so they behave
like a package (also put the modules manually in sys.modules
This commit is contained in:
parent
607c6eddf9
commit
c311521a31
1 changed files with 4 additions and 3 deletions
|
@ -75,7 +75,7 @@ class ModuleManager(object):
|
||||||
modules.append((ModuleType.FILE, file_module))
|
modules.append((ModuleType.FILE, file_module))
|
||||||
|
|
||||||
for directory_module in glob.glob(os.path.join(
|
for directory_module in glob.glob(os.path.join(
|
||||||
self.directory, "*", "module.py")):
|
self.directory, "*", "__init__.py")):
|
||||||
directory = os.path.dirname(directory_module)
|
directory = os.path.dirname(directory_module)
|
||||||
modules.append((ModuleType.DIRECTORY, directory))
|
modules.append((ModuleType.DIRECTORY, directory))
|
||||||
return sorted(modules, key=lambda module: module[1])
|
return sorted(modules, key=lambda module: module[1])
|
||||||
|
@ -94,8 +94,8 @@ class ModuleManager(object):
|
||||||
def _load_module(self, bot: "IRCBot.Bot", name: str) -> LoadedModule:
|
def _load_module(self, bot: "IRCBot.Bot", name: str) -> LoadedModule:
|
||||||
path = self._module_path(name)
|
path = self._module_path(name)
|
||||||
if os.path.isdir(path) and os.path.isfile(os.path.join(
|
if os.path.isdir(path) and os.path.isfile(os.path.join(
|
||||||
path, "module.py")):
|
path, "__init__.py")):
|
||||||
path = os.path.join(path, "module.py")
|
path = os.path.join(path, "__init__.py")
|
||||||
else:
|
else:
|
||||||
path = "%s.py" % path
|
path = "%s.py" % path
|
||||||
|
|
||||||
|
@ -120,6 +120,7 @@ class ModuleManager(object):
|
||||||
import_name = self._import_name(name)
|
import_name = self._import_name(name)
|
||||||
import_spec = importlib.util.spec_from_file_location(import_name, path)
|
import_spec = importlib.util.spec_from_file_location(import_name, path)
|
||||||
module = importlib.util.module_from_spec(import_spec)
|
module = importlib.util.module_from_spec(import_spec)
|
||||||
|
sys.modules[import_name] = module
|
||||||
import_spec.loader.exec_module(module)
|
import_spec.loader.exec_module(module)
|
||||||
|
|
||||||
module_object_pointer = getattr(module, "Module", None)
|
module_object_pointer = getattr(module, "Module", None)
|
||||||
|
|
Loading…
Reference in a new issue