[Checkins] SVN: z3c.flashmessage/trunk/ Cleanup, added tests, added ram-based source, added global receiver.

Christian Theune ct at gocept.com
Thu Jul 12 11:28:20 EDT 2007


Log message for revision 77756:
  Cleanup, added tests, added ram-based source, added global receiver.
  

Changed:
  _U  z3c.flashmessage/trunk/
  U   z3c.flashmessage/trunk/src/z3c/flashmessage/README.txt
  A   z3c.flashmessage/trunk/src/z3c/flashmessage/TODO.txt
  U   z3c.flashmessage/trunk/src/z3c/flashmessage/interfaces.py
  U   z3c.flashmessage/trunk/src/z3c/flashmessage/message.py
  A   z3c.flashmessage/trunk/src/z3c/flashmessage/receiver.py
  D   z3c.flashmessage/trunk/src/z3c/flashmessage/session.py
  A   z3c.flashmessage/trunk/src/z3c/flashmessage/source.py

-=-

Property changes on: z3c.flashmessage/trunk
___________________________________________________________________
Name: svn:ignore
   + develop-eggs
bin
parts
.installed.cfg


Modified: z3c.flashmessage/trunk/src/z3c/flashmessage/README.txt
===================================================================
--- z3c.flashmessage/trunk/src/z3c/flashmessage/README.txt	2007-07-12 15:19:56 UTC (rev 77755)
+++ z3c.flashmessage/trunk/src/z3c/flashmessage/README.txt	2007-07-12 15:28:20 UTC (rev 77756)
@@ -2,7 +2,7 @@
 Flash messages
 ==============
 
-Components to display small messages for users.
+Components to display small messages to users.
 
 
 Sending a message to the current user
@@ -11,14 +11,14 @@
 To send a message to the current user, you can use the session-based message
 source. Let's set one up:
 
->>> from z3c.flashmessage.session import SessionMessageSource
+>>> from z3c.flashmessage.source import SessionMessageSource
 >>> source = SessionMessageSource()
 
 >>> source.send(u'The world will come to an end in 40 seconds!')
 
-Then, the user can receive the message:
+The source allows to list all current messages:
 
->>> m = list(source.receive())
+>>> m = list(source.list())
 >>> m
 [<z3c.flashmessage.message.Message object at 0x...>]
 >>> m[0].message
@@ -26,11 +26,64 @@
 >>> m[0].type
 u'message'
 
-The standard message will remove itself from the source when it was received:
+Receiving messages
+==================
 
->>> list(source.receive())
+The standard message that is generated removes itself from the source when it
+is received. The receiver will call `prepare()` on the message before it is
+handed out to the code that receives it:
+
+>>> m[0].prepare(source)
+>>> list(source.list())
 []
 
+There also is another default message that does not delete itself when being
+read:
 
+>>> from z3c.flashmessage.message import PersistentMessage
+>>> source.send(PersistentMessage(u'I will stay forever!'))
+>>> m = list(source.list())[0]
+>>> m.message
+u'I will stay forever!'
+>>> m.prepare(source)
+>>> list(source.list())
+[<z3c.flashmessage.message.PersistentMessage object at 0x...>]
 
+Global receiver
+===============
 
+There is a global receiver that queries all message sources that are set up as
+utilities. Let's set up a session message source as a utility:
+
+>>> from zope.component import provideUtility 
+>>> provideUtility(source)
+>>> source.send(u'Test!')
+
+>>> from z3c.flashmessage.source import RAMMessageSource
+>>> source2 = RAMMessageSource()
+>>> provideUtility(source2, name='other')
+>>> source2.send(u'Test 2!')
+
+>>> from z3c.flashmessage.receiver import GlobalMessageReceiver
+>>> receiver = GlobalMessageReceiver()
+>>> m = list(receiver.receive())
+>>> len(m)
+3
+>>> m[0].message
+u'I will stay forever!'
+>>> m[1].message
+u'Test!'
+>>> m[2].message
+u'Test 2!'
+
+After the receiver handed out the messages, they are gone from the
+sources, because the receiver notifies the messages that they were
+read:
+
+>>> len(list(receiver.receive()))
+1
+
+
+Filtering message types
+=======================
+

Added: z3c.flashmessage/trunk/src/z3c/flashmessage/TODO.txt
===================================================================
--- z3c.flashmessage/trunk/src/z3c/flashmessage/TODO.txt	                        (rev 0)
+++ z3c.flashmessage/trunk/src/z3c/flashmessage/TODO.txt	2007-07-12 15:28:20 UTC (rev 77756)
@@ -0,0 +1,2 @@
+- Timestamps on messages
+


Property changes on: z3c.flashmessage/trunk/src/z3c/flashmessage/TODO.txt
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: z3c.flashmessage/trunk/src/z3c/flashmessage/interfaces.py
===================================================================
--- z3c.flashmessage/trunk/src/z3c/flashmessage/interfaces.py	2007-07-12 15:19:56 UTC (rev 77755)
+++ z3c.flashmessage/trunk/src/z3c/flashmessage/interfaces.py	2007-07-12 15:28:20 UTC (rev 77756)
@@ -38,7 +38,7 @@
 
         """
 
-    def receive(type=None):
+    def list(type=None):
         """Return all messages of the given type from this source.
 
         If type is None, all messages will be returned.
@@ -49,3 +49,17 @@
 
     def delete(message):
         """Remove the given message from the source."""
+
+
+class IMessageReceiver(zope.interface.Interface):
+    """Receive messages.
+
+    Depending on the implementation, this receives messages from various
+    sources.
+
+    """
+
+    def receive(type=None):
+        """Return all messages of the given type relevant to the current
+        request.
+        """

Modified: z3c.flashmessage/trunk/src/z3c/flashmessage/message.py
===================================================================
--- z3c.flashmessage/trunk/src/z3c/flashmessage/message.py	2007-07-12 15:19:56 UTC (rev 77755)
+++ z3c.flashmessage/trunk/src/z3c/flashmessage/message.py	2007-07-12 15:28:20 UTC (rev 77756)
@@ -9,11 +9,10 @@
 
 import z3c.flashmessage.interfaces
 
-
-class Message(persistent.Persistent):
+class BaseMessage(persistent.Persistent):
     """A message that is displayed to the user.
 
-    This is the default message which will delete itself after being received.
+    An (abstract) base class.
 
     """
 
@@ -23,6 +22,14 @@
         self.message = message
         self.type = type
 
+
+class Message(BaseMessage):
+    """A message that is displayed to the user.
+
+    This message will delete itself after being received.
+
+    """
+
     def prepare(self, source):
         """Prepare for being received.
 
@@ -30,3 +37,10 @@
 
         """
         source.delete(self)
+
+
+class PersistentMessage(BaseMessage):
+    """A message that doesn't delete itself when being received."""
+
+    def prepare(self, source):
+        pass

Added: z3c.flashmessage/trunk/src/z3c/flashmessage/receiver.py
===================================================================
--- z3c.flashmessage/trunk/src/z3c/flashmessage/receiver.py	                        (rev 0)
+++ z3c.flashmessage/trunk/src/z3c/flashmessage/receiver.py	2007-07-12 15:28:20 UTC (rev 77756)
@@ -0,0 +1,22 @@
+# -*- coding: latin-1 -*-
+# Copyright (c) 2007 gocept gmbh & co. kg
+# See also LICENSE.txt
+# $Id$
+"""A global message receiver that covers all sources."""
+
+import zope.interface
+
+import z3c.flashmessage.interfaces
+
+
+class GlobalMessageReceiver(object):
+
+    zope.interface.implements(z3c.flashmessage.interfaces.IMessageReceiver)
+
+    def receive(self, type=None):
+        sources = zope.component.getAllUtilitiesRegisteredFor(
+            z3c.flashmessage.interfaces.IMessageSource)
+        for source in sources:
+            for message in source.list(type):
+                message.prepare(source)
+                yield message


Property changes on: z3c.flashmessage/trunk/src/z3c/flashmessage/receiver.py
___________________________________________________________________
Name: svn:eol-style
   + native

Deleted: z3c.flashmessage/trunk/src/z3c/flashmessage/session.py
===================================================================
--- z3c.flashmessage/trunk/src/z3c/flashmessage/session.py	2007-07-12 15:19:56 UTC (rev 77755)
+++ z3c.flashmessage/trunk/src/z3c/flashmessage/session.py	2007-07-12 15:28:20 UTC (rev 77756)
@@ -1,47 +0,0 @@
-# -*- coding: latin-1 -*-
-# Copyright (c) 2007 Infrae, gocept gmbh & co. kg and Contributors
-# See also LICENSE.txt
-# $Id$
-"""A message source that stores messages in the session."""
-
-import zope.interface
-
-import zope.app.session.interfaces
-
-import persistent.list
-
-import z3c.flashmessage.interfaces
-import z3c.flashmessage.message
-
-
-class SessionMessageSource(object):
-
-    zope.interface.implements(z3c.flashmessage.interfaces.IMessageSource)
-
-    @property
-    def _storage(self):
-        request = zope.security.management.getInteraction().participations[0]
-        session = zope.app.session.interfaces.ISession(
-            request)['z3c.flashmessage']
-        messages = session.setdefault('messages',
-                                      persistent.list.PersistentList())
-        return messages
-
-    def send(self, message, type=u"message"):
-        """Send a message to this source."""
-        if not z3c.flashmessage.interfaces.IMessage.providedBy(message):
-            # The programmer has passed in not a message, so we create a
-            # message for him. This is allowed by the API for convenience.
-            message = z3c.flashmessage.message.Message(message, type=type)
-        message.source = self
-        self._storage.append(message)
-
-    def receive(self, type=None):
-        """Return all messages of the given type from this source."""
-        for message in self._storage:
-            message.prepare(self)
-            yield message
-
-    def delete(self, message):
-        """Remove the given message from the source."""
-        self._storage.remove(message)

Copied: z3c.flashmessage/trunk/src/z3c/flashmessage/source.py (from rev 77728, z3c.flashmessage/trunk/src/z3c/flashmessage/session.py)
===================================================================
--- z3c.flashmessage/trunk/src/z3c/flashmessage/source.py	                        (rev 0)
+++ z3c.flashmessage/trunk/src/z3c/flashmessage/source.py	2007-07-12 15:28:20 UTC (rev 77756)
@@ -0,0 +1,63 @@
+# -*- coding: latin-1 -*-
+# Copyright (c) 2007 Infrae, gocept gmbh & co. kg and Contributors
+# See also LICENSE.txt
+# $Id$
+"""A message source that stores messages in the session."""
+
+import zope.interface
+
+import zope.app.session.interfaces
+
+import persistent.list
+
+import z3c.flashmessage.interfaces
+import z3c.flashmessage.message
+
+
+class ListBasedMessageSource(object):
+    """An (abstract) base class that stores messages
+    in a list.
+
+    Sub-classes have to define the attribute `_storage`.
+
+    """
+
+    zope.interface.implements(z3c.flashmessage.interfaces.IMessageSource)
+
+    def send(self, message, type=u"message"):
+        """Send a message to this source."""
+        if not z3c.flashmessage.interfaces.IMessage.providedBy(message):
+            # The programmer has passed in not a message, so we create a
+            # message for him. This is allowed by the API for convenience.
+            message = z3c.flashmessage.message.Message(message, type=type)
+        message.source = self
+        self._storage.append(message)
+
+    def list(self, type=None):
+        """Return all messages of the given type from this source."""
+        return list(self._storage)
+
+    def delete(self, message):
+        """Remove the given message from the source."""
+        self._storage.remove(message)
+
+
+class SessionMessageSource(ListBasedMessageSource):
+
+    @property
+    def _storage(self):
+        request = zope.security.management.getInteraction().participations[0]
+        session = zope.app.session.interfaces.ISession(
+            request)['z3c.flashmessage']
+        messages = session.setdefault('messages',
+                                      persistent.list.PersistentList())
+        return messages
+
+
+class RAMMessageSource(ListBasedMessageSource):
+
+    zope.interface.implements(z3c.flashmessage.interfaces.IMessageSource)
+
+    def __init__(self):
+        super(RAMMessageSource, self).__init__()
+        self._storage = []



More information about the Checkins mailing list