From f9b646b0b8d8d7ca3922ac2c6b8b6ddb6b6406eb Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Mon, 4 Jan 2016 00:51:50 -0500 Subject: [PATCH] Improve config file command/task exclusion logic. --- CHANGELOG | 1 + docs/customizing.rst | 7 +++++++ earwigbot/managers.py | 20 +++++++++++++------- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 0f5709d..42d1a6e 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,7 @@ v0.3 (unreleased): - Copyvio detector: improved sentence splitting algorithm. +- Improved config file command/task exclusion logic. - IRC > !cidr: Added; new command for calculating range blocks. - IRC > !notes: Improved help and added aliases. - IRC > !remind: Added !remind all. Fixed multithreading efficiency issues. diff --git a/docs/customizing.rst b/docs/customizing.rst index ed5e6f3..56c48a0 100644 --- a/docs/customizing.rst +++ b/docs/customizing.rst @@ -174,6 +174,13 @@ The bot has a wide selection of built-in commands and plugins to act as sample code and/or to give ideas. Start with test_, and then check out chanops_ and afc_status_ for some more complicated scripts. +By default, the bot loads every built-in and custom command available. You can +disable *all* built-in commands with the config entry +:py:attr:`config.commands["disable"]` set to ``True``, or a subset of commands +by setting it to a list of command class names or module names. If using the +former method, you can specifically enable certain built-in commands with +:py:attr:`config.commands["enable"]` set to a list of command module names. + Custom bot tasks ---------------- diff --git a/earwigbot/managers.py b/earwigbot/managers.py index c980f7a..611645e 100644 --- a/earwigbot/managers.py +++ b/earwigbot/managers.py @@ -72,14 +72,21 @@ class _ResourceManager(object): for resource in self._resources.itervalues(): yield resource + def _is_disabled(self, name): + """Check whether a resource should be disabled.""" + conf = getattr(self.bot.config, self._resource_name) + disabled = conf.get("disable", []) + enabled = conf.get("enable", []) + return name not in enabled and (disabled is True or name in disabled) + def _load_resource(self, name, path, klass): """Instantiate a resource class and add it to the dictionary.""" res_type = self._resource_name[:-1] # e.g. "command" or "task" if hasattr(klass, "name"): - res_config = getattr(self.bot.config, self._resource_name) - if getattr(klass, "name") in res_config.get("disable", []): + classname = getattr(klass, "name") + if self._is_disabled(name) and self._is_disabled(classname): log = "Skipping disabled {0} {1}" - self.logger.debug(log.format(res_type, getattr(klass, "name"))) + self.logger.debug(log.format(res_type, classname)) return try: resource = klass(self.bot) # Create instance of resource @@ -119,8 +126,6 @@ class _ResourceManager(object): def _load_directory(self, dir): """Load all valid resources in a given directory.""" self.logger.debug("Loading directory {0}".format(dir)) - res_config = getattr(self.bot.config, self._resource_name) - disabled = res_config.get("disable", []) processed = [] for name in listdir(dir): if not name.endswith(".py") and not name.endswith(".pyc"): @@ -128,7 +133,7 @@ class _ResourceManager(object): if name.startswith("_") or name.startswith("."): continue modname = sub("\.pyc?$", "", name) # Remove extension - if modname in disabled: + if self._is_disabled(modname): log = "Skipping disabled module {0}".format(modname) self.logger.debug(log) processed.append(modname) @@ -162,7 +167,8 @@ class _ResourceManager(object): self._unload_resources() builtin_dir = path.join(path.dirname(__file__), name) plugins_dir = path.join(self.bot.config.root_dir, name) - if getattr(self.bot.config, name).get("disable") is True: + conf = getattr(self.bot.config, name) + if conf.get("disable") is True and not conf.get("enable"): log = "Skipping disabled builtins directory: {0}" self.logger.debug(log.format(builtin_dir)) else: