Target Python 2.6. Make the test suite pass without deprecations.
[mailman.git] / mailman / core / plugins.py
blobcf22ad3776a83b82ffcc0fc772a9940eebaa1c33
1 # Copyright (C) 2007-2008 by the Free Software Foundation, Inc.
3 # This file is part of GNU Mailman.
5 # GNU Mailman is free software: you can redistribute it and/or modify it under
6 # the terms of the GNU General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option)
8 # any later version.
10 # GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 # more details.
15 # You should have received a copy of the GNU General Public License along with
16 # GNU Mailman. If not, see <http://www.gnu.org/licenses/>.
18 """Get a requested plugin."""
20 import pkg_resources
24 def get_plugin(group):
25 """Get the named plugin.
27 In general, this returns exactly one plugin. If no plugins have been
28 added to the named group, the 'stock' plugin will be used. If more than
29 one plugin -- other than the stock one -- exists, an exception will be
30 raised.
32 :param group: The plugin group name.
33 :return: The loaded plugin.
34 :raises RuntimeError: If more than one plugin overrides the stock plugin
35 for the named group.
36 """
37 entry_points = list(pkg_resources.iter_entry_points(group))
38 if len(entry_points) == 0:
39 raise RuntimeError('No entry points found for group: %s' % group)
40 elif len(entry_points) == 1:
41 # Okay, this is the one to use.
42 return entry_points[0].load()
43 elif len(entry_points) == 2:
44 # Find the one /not/ named 'stock'.
45 entry_points = [ep for ep in entry_points if ep.name <> 'stock']
46 if len(entry_points) == 0:
47 raise RuntimeError('No stock plugin found for group: %s' % group)
48 elif len(entry_points) == 2:
49 raise RuntimeError('Too many stock plugins defined')
50 else:
51 raise AssertionError('Insanity')
52 return entry_points[0].load()
53 else:
54 raise RuntimeError('Too many plugins for group: %s' % group)
58 def get_plugins(group):
59 """Get and return all plugins in the named group.
61 :param group: Plugin group name.
62 :return: The loaded plugin.
63 """
64 for entry_point in pkg_resources.iter_entry_points(group):
65 yield entry_point.load()