Barry Warsaw pushed to branch rename-metadata-key at mailman / Mailman Core


Commits:
b9ef98f2 by Barry Warsaw at 2017-07-30T21:38:15-04:00
Fix flake8.

- - - - -
8ea476a3 by Barry Warsaw at 2017-07-30T21:52:14-04:00
Refactor into a base class.

- - - - -


4 changed files:

- src/mailman/chains/base.py
- src/mailman/chains/dmarc.py
- src/mailman/chains/moderation.py
- src/mailman/chains/tests/test_dmarc.py


Changes:

=====================================
src/mailman/chains/base.py
=====================================
--- a/src/mailman/chains/base.py
+++ b/src/mailman/chains/base.py
@@ -107,6 +107,31 @@ class TerminalChainBase:
 
 @public
 @abstract_component
+@implementer(IChain)
+class JumpChainBase:
+    """A base chain that simplifies jumping to another chain."""
+    def jump_to(self, mlist, msg, msgsdata):
+        """Return the chain to jump to.
+
+        This must be overridden by subclasses.
+
+        :param mlist: The mailing list.
+        :param msg: The message.
+        :param msgdata: The message metadata.
+        :return: The name of the chain to jump to.
+        :rtype: str
+        """
+        raise NotImplementedError
+
+    def get_links(self, mlist, msg, msgdata):
+        jump_chain = self.jump_to(mlist, msg, msgdata)
+        return iter([
+            Link('truth', LinkAction.jump, jump_chain),
+            ])
+
+
+@public
+@abstract_component
 @implementer(IMutableChain)
 class Chain:
     """Generic chain base class."""


=====================================
src/mailman/chains/dmarc.py
=====================================
--- a/src/mailman/chains/dmarc.py
+++ b/src/mailman/chains/dmarc.py
@@ -17,28 +17,23 @@
 
 """DMARC mitigation chain."""
 
-from mailman.chains.base import Link
+from mailman.chains.base import JumpChainBase
 from mailman.core.i18n import _
-from mailman.interfaces.chain import IChain, LinkAction
 from public import public
-from zope.interface import implementer
 
 
 @public
-@implementer(IChain)
-class DMARCMitigationChain:
+class DMARCMitigationChain(JumpChainBase):
     """Perform DMARC mitigation."""
 
     name = 'dmarc'
     description = _('Process DMARC reject or discard mitigations')
 
-    def get_links(self, mlist, msg, msgdata):
+    def jump_to(self, mlist, msg, msgdata):
         # Which action should be taken?
         jump_chain = msgdata['dmarc_action']
         assert jump_chain in ('discard', 'reject'), (
             '{}: Invalid DMARC action: {} for sender: {}'.format(
                 mlist.list_id, jump_chain,
                 msgdata.get('moderation_sender', '(unknown)')))
-        return iter([
-            Link('truth', LinkAction.jump, jump_chain),
-            ])
+        return jump_chain


=====================================
src/mailman/chains/moderation.py
=====================================
--- a/src/mailman/chains/moderation.py
+++ b/src/mailman/chains/moderation.py
@@ -34,17 +34,14 @@ made as to the disposition of the message.  `defer` is the 
default for
 members, while `hold` is the default for nonmembers.
 """
 
-from mailman.chains.base import Link
+from mailman.chains.base import JumpChainBase
 from mailman.core.i18n import _
 from mailman.interfaces.action import Action
-from mailman.interfaces.chain import IChain, LinkAction
 from public import public
-from zope.interface import implementer
 
 
 @public
-@implementer(IChain)
-class ModerationChain:
+class ModerationChain(JumpChainBase):
     """Dynamically produce a link jumping to the appropriate terminal chain.
 
     The terminal chain will be one of the Accept, Hold, Discard, or Reject
@@ -53,8 +50,7 @@ class ModerationChain:
     name = 'moderation'
     description = _('Moderation chain')
 
-    def get_links(self, mlist, msg, msgdata):
-        """See `IChain`."""
+    def jump_to(self, mlist, msg, msgdata):
         # Get the moderation action from the message metadata.  It can only be
         # one of the expected values (i.e. not Action.defer).  See the
         # moderation.py rule for details.  This is stored in the metadata as a
@@ -71,6 +67,4 @@ class ModerationChain:
             '{}: Invalid moderation action: {} for sender: {}'.format(
                 mlist.list_id, action,
                 msgdata.get('moderation_sender', '(unknown)')))
-        return iter([
-            Link('truth', LinkAction.jump, jump_chain),
-            ])
+        return jump_chain


=====================================
src/mailman/chains/tests/test_dmarc.py
=====================================
--- a/src/mailman/chains/tests/test_dmarc.py
+++ b/src/mailman/chains/tests/test_dmarc.py
@@ -22,10 +22,10 @@ import unittest
 from mailman.app.lifecycle import create_list
 from mailman.core.chains import process as process_chain
 from mailman.interfaces.chain import DiscardEvent, RejectEvent
-from mailman.testing.layers import ConfigLayer
 from mailman.testing.helpers import (
     event_subscribers, get_queue_messages,
     specialized_message_from_string as mfs)
+from mailman.testing.layers import ConfigLayer
 
 
 class TestDMARC(unittest.TestCase):



View it on GitLab: 
https://gitlab.com/mailman/mailman/compare/99ab464a91374efb95ace61d609eb1ec92b8ff1b...8ea476a32f2adc438a9be887aec8127b8ebdae73

---
View it on GitLab: 
https://gitlab.com/mailman/mailman/compare/99ab464a91374efb95ace61d609eb1ec92b8ff1b...8ea476a32f2adc438a9be887aec8127b8ebdae73
You're receiving this email because of your account on gitlab.com.
_______________________________________________
Mailman-checkins mailing list
Mailman-checkins@python.org
Unsubscribe: 
https://mail.python.org/mailman/options/mailman-checkins/archive%40jab.org

Reply via email to