move core modules to src/core_modules, make them uneffected by white/black list

This commit is contained in:
jesopo 2019-12-10 05:27:35 +00:00
parent 9d16f7d523
commit 638eee0d68
45 changed files with 43 additions and 32 deletions

View file

@ -123,14 +123,15 @@ events = EventManager.EventRoot(log).wrap()
exports = Exports.Exports()
timers = Timers.Timers(database, events, log)
module_directories = [os.path.join(directory, "modules")]
core_modules = os.path.join(directory, "src", "core_modules")
extra_modules = [os.path.join(directory, "modules")]
if args.external:
module_directories.append(os.path.abspath(args.external))
extra_modules.append(os.path.abspath(args.external))
if "external-modules" in config:
module_directories.append(os.path.abspath(config["external-modules"]))
extra_modules.append(os.path.abspath(config["external-modules"]))
modules = ModuleManager.ModuleManager(events, exports, timers, config, log,
module_directories)
core_modules, extra_modules)
bot = IRCBot.Bot(directory, args, cache, config, database, events,
exports, log, modules, timers)

View file

@ -102,20 +102,21 @@ class ModuleManager(object):
timers: Timers.Timers,
config: Config.Config,
log: Logging.Log,
directories: typing.List[str]):
core_modules: str,
extra_modules: typing.List[str]):
self.events = events
self.exports = exports
self.config = config
self.timers = timers
self.log = log
self.directories = directories
self._core_modules = core_modules
self._extra_modules = extra_modules
self.modules = {} # type: typing.Dict[str, LoadedModule]
def list_modules(self) -> typing.List[ModuleDefinition]:
def _list_modules(self, directory: str
) -> typing.Dict[str, ModuleDefinition]:
modules = []
for directory in self.directories:
for file_module in glob.glob(os.path.join(directory, "*.py")):
modules.append(self.define_module(ModuleType.FILE, file_module))
@ -123,7 +124,24 @@ class ModuleManager(object):
directory, "*", "__init__.py")):
modules.append(self.define_module(ModuleType.DIRECTORY,
directory_module))
return sorted(modules, key=lambda module: module.name)
return {definition.name: definition for definition in modules}
def list_modules(self, whitelist: typing.List[str],
blacklist: typing.List[str]) -> typing.Dict[str, ModuleDefinition]:
core_modules = self._list_modules(self._core_modules)
extra_modules = {}
for directory in self._extra_modules:
for name, module in self._list_modules(directory).items():
if (not name in extra_modules and
(name in whitelist or
(not whitelist and not name in blacklist))):
extra_modules[name] = module
modules = {}
modules.update(extra_modules)
modules.update(core_modules)
return modules
def define_module(self, type: ModuleType, filename: str
) -> ModuleDefinition:
@ -180,10 +198,6 @@ class ModuleManager(object):
return module
return None
def _get_magic(self, obj: typing.Any, magic: str, default: typing.Any
) -> typing.Any:
return getattr(obj, magic) if hasattr(obj, magic) else default
def _check_hashflags(self, bot: "IRCBot.Bot", definition: ModuleDefinition
) -> None:
for hashflag, value in definition.hashflags:
@ -275,7 +289,8 @@ class ModuleManager(object):
self.log.debug("Module '%s' loaded", [loaded_module.name])
return loaded_module
def _dependency_sort(self, definitions: typing.List[ModuleDefinition]):
def _dependency_sort(self, definitions: typing.Dict[str, ModuleDefinition]
) -> typing.List[ModuleDefinition]:
definitions_ordered = []
definition_names = {d.name: d for d in definitions}
@ -416,22 +431,17 @@ class ModuleManager(object):
def _list_valid_modules(self, bot: "IRCBot.Bot",
whitelist: typing.List[str], blacklist: typing.List[str]):
module_definitions = self.list_modules()
module_definitions = self.list_modules(whitelist, blacklist)
loadable_definitions = []
nonloadable_definitions = []
for definition in module_definitions:
if definition.name in whitelist or (
not whitelist and not definition.name in blacklist):
for name, definition in module_definitions.items():
try:
self._check_hashflags(bot, definition)
except ModuleNotLoadableWarning:
nonloadable_definitions.append(definition)
continue
loadable_definitions.append(definition)
else:
nonloadable_definitions.append(definition)
return (self._dependency_sort(loadable_definitions),
nonloadable_definitions)