add external_modules directory

This commit is contained in:
jesopo 2019-09-27 16:03:06 +01:00
parent 16ba52f5dc
commit 82173a7d94
3 changed files with 32 additions and 18 deletions

0
external_modules/.keep Normal file
View file

View file

@ -92,26 +92,27 @@ class ModuleManager(object):
timers: Timers.Timers, timers: Timers.Timers,
config: Config.Config, config: Config.Config,
log: Logging.Log, log: Logging.Log,
directory: str): directories: 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.directory = directory self.directories = directories
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) -> typing.List[ModuleDefinition]:
modules = [] modules = []
for file_module in glob.glob(os.path.join(self.directory, "*.py")): for directory in self.directories:
modules.append(self.define_module(ModuleType.FILE, file_module)) for file_module in glob.glob(os.path.join(directory, "*.py")):
modules.append(self.define_module(ModuleType.FILE, file_module))
for directory_module in glob.glob(os.path.join( for directory_module in glob.glob(os.path.join(
self.directory, "*", "__init__.py")): directory, "*", "__init__.py")):
modules.append(self.define_module(ModuleType.DIRECTORY, modules.append(self.define_module(ModuleType.DIRECTORY,
directory_module)) directory_module))
return sorted(modules, key=lambda module: module.name) return sorted(modules, key=lambda module: module.name)
def define_module(self, type: ModuleType, filename: str def define_module(self, type: ModuleType, filename: str
@ -127,23 +128,33 @@ class ModuleManager(object):
def find_module(self, name: str) -> ModuleDefinition: def find_module(self, name: str) -> ModuleDefinition:
type = ModuleType.FILE type = ModuleType.FILE
path = self._module_path(name) paths = self._module_paths(name)
if os.path.isdir(path): path = None
type = ModuleType.DIRECTORY for path in paths:
path = os.path.join(path, "__init__.py") if os.path.isdir(path):
else: type = ModuleType.DIRECTORY
path = "%s.py" % path path = os.path.join(path, "__init__.py")
else:
possible_path = "%s.py" % path
if os.path.isfile(possible_path):
path = possible_path
if not os.path.exists(path): if path:
break
if not path:
raise ModuleNotFoundException(name) raise ModuleNotFoundException(name)
return self.define_module(type, path) return self.define_module(type, path)
def _module_name(self, path: str) -> str: def _module_name(self, path: str) -> str:
return os.path.basename(path).rsplit(".py", 1)[0].lower() return os.path.basename(path).rsplit(".py", 1)[0].lower()
def _module_path(self, name: str) -> str: def _module_paths(self, name: str) -> str:
return os.path.join(self.directory, name) paths = []
for directory in self.directories:
paths.append(os.path.join(directory, name))
return paths
def _import_name(self, name: str) -> str: def _import_name(self, name: str) -> str:
return "bitbot_%s" % name return "bitbot_%s" % name

View file

@ -86,8 +86,11 @@ config = Config.Config(args.config)
events = EventManager.EventRoot(log).wrap() 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"),
os.path.join(directory, "external_modules")]
modules = ModuleManager.ModuleManager(events, exports, timers, config, log, modules = ModuleManager.ModuleManager(events, exports, timers, config, log,
os.path.join(directory, "modules")) module_directories)
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)