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)
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
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/>.
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
31 @implementer(IStyleManager
)
33 """The built-in style manager."""
36 """Install all styles from the configuration files."""
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()))
47 for style_class
in find_components(path
, IStyle
):
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
56 """See `IStyleManager`."""
57 return self
._styles
.get(name
)
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
]
79 def handle_ConfigurationUpdatedEvent(event
):
80 if isinstance(event
, ConfigurationUpdatedEvent
):
81 getUtility(IStyleManager
).populate()