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() exports = Exports.Exports()
timers = Timers.Timers(database, events, log) 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: if args.external:
module_directories.append(os.path.abspath(args.external)) extra_modules.append(os.path.abspath(args.external))
if "external-modules" in config: 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, modules = ModuleManager.ModuleManager(events, exports, timers, config, log,
module_directories) core_modules, extra_modules)
bot = IRCBot.Bot(directory, args, cache, config, database, events, bot = IRCBot.Bot(directory, args, cache, config, database, events,
exports, log, modules, timers) exports, log, modules, timers)

View file

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