plugin: allow excluding some handling conditions

This commit is contained in:
dece 2020-11-02 13:23:19 +01:00
parent f055ef76c4
commit 127ad5c895

View file

@ -105,10 +105,27 @@ class Plugin:
return True return True
return False return False
def should_handle_command(self, message, no_content=False): def should_handle_command(
"""Store Command in object and return True if it should handle it.""" self,
message,
no_content=False,
exclude_conditions=None,
):
"""Store Command in object and return True if it should handle it.
If no_content is True, the command does not parse command contents and
put all the command message (without suffix) to the command identifier.
This is useful for commands that have multiple words in their identifier
and do not have any content, or when the content parsing should be done
by the plugin itself.
If exclude_conditions is a collection of plugin namespaces, the
conditions of these plugins are not tested. A basic scenario is the
wakeup command from sleep plugin that requires excluding its own
condition (i.e. do not handle commands when sleeping).
"""
# Are the config conditions to handle a command valid? # Are the config conditions to handle a command valid?
if not self.__respects_handling_conditions(): if not self.__respects_handling_conditions(exclude_conditions):
return False return False
# Is it a valid command? # Is it a valid command?
@ -140,10 +157,12 @@ class Plugin:
content = " ".join(words[2:-1]) content = " ".join(words[2:-1])
return Command(ident, content) return Command(ident, content)
def __respects_handling_conditions(self): def __respects_handling_conditions(self, exclude_conditions=None):
"""Check if question conditions are valid.""" """Check if question conditions are valid."""
conditions = self.config.get("handling_conditions", {}) conditions = self.config.get("handling_conditions", {})
for plugin_ns, plugin_conditions in conditions.items(): for plugin_ns, plugin_conditions in conditions.items():
if exclude_conditions and plugin_ns in exclude_conditions:
continue
for condition_key, condition_value in plugin_conditions.items(): for condition_key, condition_value in plugin_conditions.items():
value = self.get_runtime_value(condition_key, ns=plugin_ns) value = self.get_runtime_value(condition_key, ns=plugin_ns)
if condition_value != value: if condition_value != value: