switch module whitelist/blacklist to its own config file

This commit is contained in:
jesopo 2020-02-04 15:31:17 +00:00
parent 150148b4e0
commit 8e611c451e
2 changed files with 20 additions and 12 deletions

View file

@ -6,8 +6,8 @@ URL: str = "https://bitbot.dev"
import enum, queue, os, queue, select, socket, sys, threading, time, traceback
import typing, uuid
from src import EventManager, Exports, IRCServer, Logging, ModuleManager
from src import PollHook, PollSource, Socket, Timers, utils
from src import Config, EventManager, Exports, IRCServer, Logging
from src import ModuleManager, PollHook, PollSource, Socket, Timers, utils
class TriggerResult(enum.Enum):
Return = 1
@ -147,9 +147,16 @@ class Bot(object):
self.log.critical("panic() called: %s", [reason], exc_info=exc_info)
sys.exit(utils.consts.Exit.PANIC)
def get_config(self, name: str) -> Config.Config:
path = os.path.join(self.data_directory, "%s.conf" % name)
config = Config.Config(name, path)
config.load()
return config
def _module_lists(self):
whitelist = self.config.get_list("module-whitelist")
blacklist = self.config.get_list("module-blacklist")
module_lists = self.get_config("modules")
whitelist = module_lists.get_list("whitelist")
blacklist = module_lists.get_list("blacklist")
return whitelist, blacklist

View file

@ -102,10 +102,11 @@ class Module(ModuleManager.BaseModule):
event["stderr"].write(result.message)
def _get_blacklist(self):
return self.bot.config.get_list("module-blacklist")
def _save_blacklist(self, modules):
self.bot.config.set_list("module-blacklist", modules)
self.bot.config.save()
config = self.bot.get_config("modules")
return config, config.get_list("blacklist")
def _save_blacklist(self, config, modules):
config.set_list("blacklist", sorted(modules))
config.save()
@utils.hook("received.command.enablemodule")
@utils.kwarg("min_args", 1)
@ -115,12 +116,12 @@ class Module(ModuleManager.BaseModule):
def enable(self, event):
name = event["args_split"][0].lower()
blacklist = self._get_blacklist()
config, blacklist = self._get_blacklist()
if not name in blacklist:
raise utils.EventError("Module '%s' isn't disabled" % name)
blacklist.remove(name)
self._save_blacklist(blacklist)
self._save_blacklist(config, blacklist)
event["stdout"].write("Module '%s' has been enabled and can now "
"be loaded" % name)
@ -136,11 +137,11 @@ class Module(ModuleManager.BaseModule):
self.bot.modules.unload_module(name)
and_unloaded = " and unloaded"
blacklist = self._get_blacklist()
config, blacklist = self._get_blacklist()
if name in blacklist:
raise utils.EventError("Module '%s' is already disabled" % name)
blacklist.append(name)
self._save_blacklist(blacklist)
self._save_blacklist(config, blacklist)
event["stdout"].write("Module '%s' has been disabled%s" % (
name, and_unloaded))