Clean up the styles directory.
[mailman.git] / src / mailman / styles / manager.py
blob1b3130581e49dd1ecf7fa86cd4f246e055adc95d
1 # Copyright (C) 2007-2016 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 """Style manager."""
20 from mailman import public
21 from mailman.interfaces.configuration import ConfigurationUpdatedEvent
22 from mailman.interfaces.styles import (
23 DuplicateStyleError, IStyle, IStyleManager)
24 from mailman.utilities.modules import find_components
25 from zope.component import getUtility
26 from zope.interface import implementer
27 from zope.interface.verify import verifyObject
30 @public
31 @implementer(IStyleManager)
32 class StyleManager:
33 """The built-in style manager."""
35 def __init__(self):
36 """Install all styles from the configuration files."""
37 self._styles = {}
39 def populate(self):
40 self._styles.clear()
41 # Avoid circular imports.
42 from mailman.config import config
43 # Calculate the Python import paths to search.
44 paths = filter(None, (path.strip()
45 for path in config.styles.paths.splitlines()))
46 for path in paths:
47 for style_class in find_components(path, IStyle):
48 style = style_class()
49 verifyObject(IStyle, style)
50 assert style.name not in self._styles, (
51 'Duplicate style "{}" found in {}'.format(
52 style.name, style_class))
53 self._styles[style.name] = style
55 def get(self, name):
56 """See `IStyleManager`."""
57 return self._styles.get(name)
59 @property
60 def styles(self):
61 """See `IStyleManager`."""
62 for style_name in sorted(self._styles):
63 yield self._styles[style_name]
65 def register(self, style):
66 """See `IStyleManager`."""
67 verifyObject(IStyle, style)
68 if style.name in self._styles:
69 raise DuplicateStyleError(style.name)
70 self._styles[style.name] = style
72 def unregister(self, style):
73 """See `IStyleManager`."""
74 # Let KeyErrors percolate up.
75 del self._styles[style.name]
78 @public
79 def handle_ConfigurationUpdatedEvent(event):
80 if isinstance(event, ConfigurationUpdatedEvent):
81 getUtility(IStyleManager).populate()