diff options
Diffstat (limited to 'src/mailman/chains')
| -rw-r--r-- | src/mailman/chains/base.py | 20 | ||||
| -rw-r--r-- | src/mailman/chains/builtin.py | 8 | ||||
| -rw-r--r-- | src/mailman/chains/headers.py | 9 | ||||
| -rw-r--r-- | src/mailman/chains/moderation.py | 4 | ||||
| -rw-r--r-- | src/mailman/chains/tests/test_base.py | 12 |
5 files changed, 21 insertions, 32 deletions
diff --git a/src/mailman/chains/base.py b/src/mailman/chains/base.py index 54bddfe0b..f6c0db2f5 100644 --- a/src/mailman/chains/base.py +++ b/src/mailman/chains/base.py @@ -27,18 +27,22 @@ __all__ = [ from mailman.config import config from mailman.interfaces.chain import ( IChain, IChainIterator, IChainLink, IMutableChain, LinkAction) +from mailman.interfaces.rules import IRule from zope.interface import implementer - @implementer(IChainLink) class Link: """A chain link.""" def __init__(self, rule, action=None, chain=None, function=None): - self.rule = rule + self.rule = (rule + if IRule.providedBy(rule) + else config.rules[rule]) self.action = (LinkAction.defer if action is None else action) - self.chain = chain + self.chain = (chain + if chain is None or IChain.providedBy(chain) + else config.chains[chain]) self.function = function def __repr__(self): @@ -55,7 +59,6 @@ class Link: return message.format(self) - @implementer(IChain, IChainIterator) class TerminalChainBase: """A base chain that always matches and executes a method. @@ -79,21 +82,19 @@ class TerminalChainBase: def __iter__(self): """See `IChainIterator`.""" - truth = config.rules['truth'] # First, yield a link that always runs the process method. - yield Link(truth, LinkAction.run, function=self._process) + yield Link('truth', LinkAction.run, function=self._process) # Now yield a rule that stops all processing. - yield Link(truth, LinkAction.stop) + yield Link('truth', LinkAction.stop) - @implementer(IMutableChain) class Chain: """Generic chain base class.""" def __init__(self, name, description): assert name not in config.chains, ( - 'Duplicate chain name: {0}'.format(name)) + 'Duplicate chain name: {}'.format(name)) self.name = name self.description = description self._links = [] @@ -117,7 +118,6 @@ class Chain: yield from self._links - @implementer(IChainIterator) class ChainIterator: """Generic chain iterator.""" diff --git a/src/mailman/chains/builtin.py b/src/mailman/chains/builtin.py index 317695110..375d83fa4 100644 --- a/src/mailman/chains/builtin.py +++ b/src/mailman/chains/builtin.py @@ -25,7 +25,6 @@ __all__ = [ import logging from mailman.chains.base import Link -from mailman.config import config from mailman.core.i18n import _ from mailman.interfaces.chain import IChain, LinkAction from zope.interface import implementer @@ -73,11 +72,6 @@ class BuiltInChain: """See `IChain`.""" if self._cached_links is None: self._cached_links = links = [] - for rule_name, action, chain_name in self._link_descriptions: - # Get the named rule. - rule = config.rules[rule_name] - # Get the chain, if one is defined. - chain = (None if chain_name is None - else config.chains[chain_name]) + for rule, action, chain in self._link_descriptions: links.append(Link(rule, action, chain)) return iter(self._cached_links) diff --git a/src/mailman/chains/headers.py b/src/mailman/chains/headers.py index c62ddb959..f0275ca15 100644 --- a/src/mailman/chains/headers.py +++ b/src/mailman/chains/headers.py @@ -49,7 +49,7 @@ def make_link(header, pattern, chain=None): :type header: string :param pattern: A regular expression for matching the header value. :type pattern: string - :param chain: When given, this is the chain to jump to if the + :param chain: When given, this is the name of the chain to jump to if the pattern matches the header. :type chain: string :return: The link representing this rule check. @@ -58,7 +58,6 @@ def make_link(header, pattern, chain=None): rule = HeaderMatchRule(header, pattern) if chain is None: return Link(rule) - chain = config.chains[chain] return Link(rule, LinkAction.jump, chain) @@ -85,7 +84,7 @@ class HeaderMatchRule: self.record = True # Register this rule so that other parts of the system can query it. assert self.name not in config.rules, ( - 'Duplicate HeaderMatchRule: {0} [{1}: {2}]'.format( + 'Duplicate HeaderMatchRule: {} [{}: {}]'.format( self.name, self.header, self.pattern)) config.rules[self.name] = self @@ -148,9 +147,7 @@ class HeaderMatchChain(Chain): # action until now, so jump to the chain defined in the configuration # file. For security considerations, this takes precedence over # list-specific matches. - yield Link(config.rules['any'], - LinkAction.jump, - config.chains[config.antispam.jump_chain]) + yield Link('any', LinkAction.jump, config.antispam.jump_chain) # Then return all the list-specific header matches. for entry in mlist.header_matches: yield make_link(entry.header, entry.pattern, entry.chain) diff --git a/src/mailman/chains/moderation.py b/src/mailman/chains/moderation.py index 139a26fda..b4fc5eb33 100644 --- a/src/mailman/chains/moderation.py +++ b/src/mailman/chains/moderation.py @@ -76,8 +76,6 @@ class ModerationChain: '{0}: Invalid moderation action: {1} for sender: {2}'.format( mlist.fqdn_listname, action, msgdata.get('moderation_sender', '(unknown)'))) - truth = config.rules['truth'] - chain = config.chains[jump_chain] return iter([ - Link(truth, LinkAction.jump, chain), + Link('truth', LinkAction.jump, jump_chain), ]) diff --git a/src/mailman/chains/tests/test_base.py b/src/mailman/chains/tests/test_base.py index dc50252c3..86a09678d 100644 --- a/src/mailman/chains/tests/test_base.py +++ b/src/mailman/chains/tests/test_base.py @@ -43,38 +43,38 @@ class TestMiscellaneous(unittest.TestCase): def test_link_repr(self): self.assertEqual( - repr(Link(Any)), '<Link "if any then LinkAction.defer">') + repr(Link(Any())), '<Link "if any then LinkAction.defer">') def test_link_repr_function(self): def function(): pass self.assertEqual( - repr(Link(Any, function=function)), + repr(Link(Any(), function=function)), '<Link "if any then LinkAction.defer" function()>') def test_link_repr_chain(self): self.assertEqual( - repr(Link(Any, chain=AcceptChain)), + repr(Link(Any(), chain=AcceptChain())), '<Link "if any then LinkAction.defer" accept>') def test_link_repr_chain_and_function(self): def function(): pass self.assertEqual( - repr(Link(Any, chain=AcceptChain, function=function)), + repr(Link(Any(), chain=AcceptChain(), function=function)), '<Link "if any then LinkAction.defer" accept function()>') def test_link_repr_chain_all(self): def function(): pass self.assertEqual( - repr(Link(Any, LinkAction.stop, AcceptChain, function)), + repr(Link(Any(), LinkAction.stop, AcceptChain(), function)), '<Link "if any then LinkAction.stop" accept function()>') def test_flush(self): # Test that we can flush the links of a chain. chain = Chain('test', 'just a testing chain') - chain.append_link(Link(Any)) + chain.append_link(Link(Any())) # Iterate over the links of the chain to prove there are some. count = sum(1 for link in chain.get_iterator()) self.assertEqual(count, 1) |
