From 0d4f53b51892866b5cc85ace229b23f4b9bac896 Mon Sep 17 00:00:00 2001 From: J08nY Date: Thu, 1 Jun 2017 15:46:48 +0200 Subject: Add per-plugin hooks, add docs about plugins. - Removes pre_hook, post_hook and ext_dir. With the latter being unused. Warns on startup if the hooks are present in config. - Adds IPlugin interface with pre_hook and post_hook methods. - Adds 'class' config parameter to plugins, which can be set to a class implementing the IPlugin interface, it will be initialized once on Mailman's startup, then before the DB setup the pre_hook will be run, after DB and other components the post_hook will be run. Plugin instances are stored in the config.plugins dict, with keys being their configuration section names. --- src/mailman/core/plugins.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/mailman/core/plugins.py (limited to 'src/mailman/core/plugins.py') diff --git a/src/mailman/core/plugins.py b/src/mailman/core/plugins.py new file mode 100644 index 000000000..565c590b5 --- /dev/null +++ b/src/mailman/core/plugins.py @@ -0,0 +1,42 @@ +# Copyright (C) 2008-2017 by the Free Software Foundation, Inc. +# +# This file is part of GNU Mailman. +# +# GNU Mailman is free software: you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free +# Software Foundation, either version 3 of the License, or (at your option) +# any later version. +# +# GNU Mailman is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along with +# GNU Mailman. If not, see . + +"""Various plugin helpers.""" + +from lazr.config import as_boolean +from mailman.config import config +from mailman.interfaces.plugin import IPlugin +from mailman.utilities.modules import find_name +from public import public +from zope.interface.verify import verifyObject + + +@public +def initialize(): + """Initialize all enabled plugins.""" + for plugin_config in config.plugin_configs: + plugin_class_path = plugin_config['class'] + if as_boolean(plugin_config.enable) and plugin_class_path: + plugin_class = find_name(plugin_class_path) + plugin = plugin_class() + verifyObject(IPlugin, plugin) + name = plugin_config.name.split('.')[-1] + plugin.name = name + assert plugin.name not in config.plugins, ( + 'Duplicate plugin "{}" found in {}'.format( + plugin.name, plugin_class)) + config.plugins[plugin.name] = plugin -- cgit v1.2.3-70-g09d2 From a2668307017e8b3a0c705e5cff49a8fa7dbeac7d Mon Sep 17 00:00:00 2001 From: J08nY Date: Tue, 6 Jun 2017 15:51:10 +0200 Subject: Make config.plugin_configs yield a dict with plugin names. - Allows to better loop over pluging configs and their names. --- src/mailman/config/config.py | 6 ++++-- src/mailman/core/plugins.py | 3 +-- src/mailman/rest/plugins.py | 13 +++++++------ src/mailman/utilities/plugins.py | 2 +- 4 files changed, 13 insertions(+), 11 deletions(-) (limited to 'src/mailman/core/plugins.py') diff --git a/src/mailman/config/config.py b/src/mailman/config/config.py index 405b689ef..a5e3ed3a7 100644 --- a/src/mailman/config/config.py +++ b/src/mailman/config/config.py @@ -250,8 +250,10 @@ class Configuration: @property def plugin_configs(self): - """Iterate over all the plugin configuration sections.""" - return self._config.getByCategory('plugin', []) + """Return all the plugin configuration sections.""" + plugin_sections = self._config.getByCategory('plugin', []) + for section in plugin_sections: + yield section.category_and_section_names[1], section @property def language_configs(self): diff --git a/src/mailman/core/plugins.py b/src/mailman/core/plugins.py index 565c590b5..e891f380d 100644 --- a/src/mailman/core/plugins.py +++ b/src/mailman/core/plugins.py @@ -28,13 +28,12 @@ from zope.interface.verify import verifyObject @public def initialize(): """Initialize all enabled plugins.""" - for plugin_config in config.plugin_configs: + for name, plugin_config in config.plugin_configs: plugin_class_path = plugin_config['class'] if as_boolean(plugin_config.enable) and plugin_class_path: plugin_class = find_name(plugin_class_path) plugin = plugin_class() verifyObject(IPlugin, plugin) - name = plugin_config.name.split('.')[-1] plugin.name = name assert plugin.name not in config.plugins, ( 'Duplicate plugin "{}" found in {}'.format( diff --git a/src/mailman/rest/plugins.py b/src/mailman/rest/plugins.py index 2209e634e..fa2605f24 100644 --- a/src/mailman/rest/plugins.py +++ b/src/mailman/rest/plugins.py @@ -29,18 +29,19 @@ class AllPlugins(CollectionMixin): def _resource_as_dict(self, plugin_config): """See `CollectionMixin`.""" + name, plugin_section = plugin_config resource = { - 'name': plugin_config.name.split('.')[-1], - 'class': plugin_config['class'], - 'enable': as_boolean(plugin_config['enable']), - 'path': plugin_config['path'], - 'configuration': plugin_config['configuration'] + 'name': name, + 'class': plugin_section['class'], + 'enable': as_boolean(plugin_section['enable']), + 'path': plugin_section['path'], + 'configuration': plugin_section['configuration'] } return resource def _get_collection(self, request): """See `CollectionMixin`.""" - return config.plugin_configs + return sorted(config.plugin_configs) def on_get(self, request, response): """/plugins""" diff --git a/src/mailman/utilities/plugins.py b/src/mailman/utilities/plugins.py index a8449af31..98a34fbca 100644 --- a/src/mailman/utilities/plugins.py +++ b/src/mailman/utilities/plugins.py @@ -38,7 +38,7 @@ def find_pluggable_components(subpackage, interface): """ yield from find_components('mailman.' + subpackage, interface) package_roots = [plugin.path - for plugin in config.plugin_configs + for name, plugin in config.plugin_configs if as_boolean(plugin.enable) and plugin.path] for package_root in package_roots: package = package_root + '.' + subpackage -- cgit v1.2.3-70-g09d2