Add per list member roster visibility option
[mailman.git] / src / mailman / rest / plugins.py
blob780d1c43e3defe8cddbe99fb8d1b536fc439c509
1 # Copyright (C) 2010-2019 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 """REST for plugins, dynamically proxies requests to plugin's rest_object."""
20 from lazr.config import as_boolean
21 from mailman.config import config
22 from mailman.rest.helpers import CollectionMixin, NotFound, etag, okay
23 from operator import itemgetter
24 from public import public
27 @public
28 class AllPlugins(CollectionMixin):
29 """Read-only list of all plugin configs."""
31 def _resource_as_dict(self, plugin_config):
32 """See `CollectionMixin`."""
33 name, plugin_section = plugin_config
34 resource = {
35 'name': name,
36 'class': plugin_section['class'],
37 'enabled': as_boolean(plugin_section['enabled']),
39 # Add the path to the plugin's own configuration file, if one was
40 # given.
41 plugin_config = plugin_section['configuration'].strip()
42 if len(plugin_config) > 0:
43 resource['configuration'] = plugin_config
44 return resource
46 def _get_collection(self, request):
47 """See `CollectionMixin`."""
48 # plugin_configs returns a 2-tuple of (name, section), so sort
49 # alphabetically on the plugin name.
50 return sorted(config.plugin_configs, key=itemgetter(0))
52 def on_get(self, request, response):
53 """/plugins"""
54 resource = self._make_collection(request)
55 okay(response, etag(resource))
58 @public
59 class APlugin:
60 """REST proxy to the plugin's rest_object."""
62 def __init__(self, plugin_name):
63 self._resource = None
64 if plugin_name in config.plugins:
65 plugin = config.plugins[plugin_name]
66 self._resource = plugin.resource
67 # If the plugin doesn't exist or doesn't provide a resource, just proxy
68 # to NotFound.
69 if self._resource is None:
70 self._resource = NotFound()
72 def __getattr__(self, attrib):
73 return getattr(self._resource, attrib)
75 def __dir__(self):
76 return dir(self._resource)