[Checkins] SVN: z3c.filetype/trunk/src/z3c/filetype/ added created events

Bernd Dorn bernd.dorn at fhv.at
Mon Aug 14 10:58:29 EDT 2006


Log message for revision 69475:
  added created events

Changed:
  U   z3c.filetype/trunk/src/z3c/filetype/README.txt
  U   z3c.filetype/trunk/src/z3c/filetype/api.py
  U   z3c.filetype/trunk/src/z3c/filetype/configure.zcml
  U   z3c.filetype/trunk/src/z3c/filetype/event.py
  U   z3c.filetype/trunk/src/z3c/filetype/interfaces/__init__.py

-=-
Modified: z3c.filetype/trunk/src/z3c/filetype/README.txt
===================================================================
--- z3c.filetype/trunk/src/z3c/filetype/README.txt	2006-08-14 14:42:14 UTC (rev 69474)
+++ z3c.filetype/trunk/src/z3c/filetype/README.txt	2006-08-14 14:58:29 UTC (rev 69475)
@@ -79,12 +79,12 @@
   [<InterfaceClass z3c.filetype.interfaces.filetypes.ITARFile>]
 
 
-Applying filetype interfaces to objects
-=======================================
+Applying filetype interfaces to objects via events
+==================================================
 
-There is also a convinience function which applies filetype interfaces
-to an object. This object needs to implement ITypeableFile. This also
-fires events, so let us setup the event handling.
+There are event handlers which apply filetype interfaces to an
+object. This object needs to implement ITypeableFile. So let us setup
+the event handling.
 
   >>> from zope.component import eventtesting
   >>> eventtesting.setUp()
@@ -97,16 +97,31 @@
   ...         self.data = f
   >>> foo = Foo(f)
 
-The applyInterfaces method returns a boolean if changes occured.
+There is also an event handler registered for IObjectCreatedEvent and
+IObjectModified on  ITypeableFile. We register them here in the test.
 
-  >>> api.applyInterfaces(foo)
-  True
+  >>> from zope import component
+  >>> component.provideHandler(api.handleCreated)
+  >>> component.provideHandler(api.handleModified)
 
-And an event should be have been fired.
+So we need to fire an IObjectCreatedEvent. Which is normally done by a
+factory.
 
-  >>> eventtesting.getEvents()
-  [<z3c.filetype.event.FileTypeModifiedEvent object at ...>]
+  >>> from zope.lifecycleevent import ObjectCreatedEvent
+  >>> from zope.lifecycleevent import ObjectModifiedEvent
+  >>> from zope.event import notify
+  >>> eventtesting.clearEvents()
+  >>> notify(ObjectCreatedEvent(foo))
+  >>> sorted(eventtesting.getEvents())
+  [<z3c.filetype.event.FileTypeModifiedEvent object at ...>,
+   <zope.app.event.objectevent.ObjectCreatedEvent object at ...>]
 
+The object now implements the according interface. This is achieved by
+the evennthandler which calls applyInterfaces.
+
+  >>> sorted((interface.directlyProvidedBy(foo)))
+  [<InterfaceClass z3c.filetype.interfaces.filetypes.ITARFile>]
+
 A second applyInteraces does nothing.
 
   >>> eventtesting.clearEvents()
@@ -115,34 +130,13 @@
   >>> eventtesting.getEvents()
   []
 
-Now the object should implement the right interface according to the
-ata contained.
 
-  >>> sorted((interface.directlyProvidedBy(foo)))
-  [<InterfaceClass z3c.filetype.interfaces.filetypes.ITARFile>]
+If we change the object the interface changes too. We need to fire
+an IObjectModifiedevent. Which is normally done by the implementation.
 
-If we change the object the interface changes too.
-
   >>> foo.data = file(os.path.join(testData,'test.flv'))
-  >>> api.applyInterfaces(foo)
-  True
-  >>> sorted((interface.directlyProvidedBy(foo)))
-  [<InterfaceClass z3c.filetype.interfaces.filetypes.IFLVFile>]
-
-
-There is also an event handler registered on IObjectModified for
-ITypeableFile. We register it here in the test.
-
-  >>> from zope import component
-  >>> component.provideHandler(api.handleModified)
-  >>> foo.data = file(os.path.join(testData,'test.html'))
-
-So we need to fire an IObjectModifiedevent. Which is normally done by
-the implementation.
-
-  >>> from zope.lifecycleevent import ObjectModifiedEvent
-  >>> from zope.event import notify
   >>> eventtesting.clearEvents()
+  >>> 
   >>> notify(ObjectModifiedEvent(foo))
 
 Now we have two events, one we fired and one from our handler.
@@ -150,13 +144,12 @@
   >>> eventtesting.getEvents()
   [<zope.app.event.objectevent.ObjectModifiedEvent object at ...>,
    <z3c.filetype.event.FileTypeModifiedEvent object at ...>]
-  
+
 Now the file should implement another filetype.
 
   >>> sorted((interface.directlyProvidedBy(foo)))
-  [<InterfaceClass z3c.filetype.interfaces.filetypes.IHTMLFile>]
+  [<InterfaceClass z3c.filetype.interfaces.filetypes.IFLVFile>]
 
-
 There is also an adapter from ITypedFile to IFileType, which can be
 used to get the default content type for the interface.
 
@@ -164,6 +157,6 @@
   >>> component.provideAdapter(adapters.TypedFileType)
   >>> ft = interfaces.IFileType(foo)
   >>> ft.contentType
-  'text/html'
+  'video/x-flv'
   
 

Modified: z3c.filetype/trunk/src/z3c/filetype/api.py
===================================================================
--- z3c.filetype/trunk/src/z3c/filetype/api.py	2006-08-14 14:42:14 UTC (rev 69474)
+++ z3c.filetype/trunk/src/z3c/filetype/api.py	2006-08-14 14:58:29 UTC (rev 69475)
@@ -4,6 +4,7 @@
 from zope.contenttype import guess_content_type
 from zope import interface, component
 from zope.lifecycleevent.interfaces import IObjectModifiedEvent
+from zope.lifecycleevent.interfaces import IObjectCreatedEvent
 from zope.event import notify
 from event import FileTypeModifiedEvent
 
@@ -104,8 +105,12 @@
 @component.adapter(interfaces.ITypeableFile,IObjectModifiedEvent)
 def handleModified(typeableFile, event):
     """handles modification of data"""
-    #import pdb;pdb.set_trace()
     if interfaces.IFileTypeModifiedEvent.providedBy(event):
         # do nothing if this is already a filetype modification event
         return
     applyInterfaces(typeableFile)
+
+ at component.adapter(interfaces.ITypeableFile,IObjectCreatedEvent)
+def handleCreated(typeableFile, event):
+    """handles modification of data"""
+    applyInterfaces(typeableFile)

Modified: z3c.filetype/trunk/src/z3c/filetype/configure.zcml
===================================================================
--- z3c.filetype/trunk/src/z3c/filetype/configure.zcml	2006-08-14 14:42:14 UTC (rev 69474)
+++ z3c.filetype/trunk/src/z3c/filetype/configure.zcml	2006-08-14 14:58:29 UTC (rev 69475)
@@ -7,5 +7,10 @@
                   zope.lifecycleevent.interfaces.IObjectModifiedEvent"
              handler=".api.handleModified"/>
 
+ <subscriber for=".interfaces.ITypeableFile
+                  zope.lifecycleevent.interfaces.IObjectCreatedEvent"
+             handler=".api.handleCreated"/>
+
+ 
  <adapter factory=".adapters.TypedFileType"/>
 </configure>

Modified: z3c.filetype/trunk/src/z3c/filetype/event.py
===================================================================
--- z3c.filetype/trunk/src/z3c/filetype/event.py	2006-08-14 14:42:14 UTC (rev 69474)
+++ z3c.filetype/trunk/src/z3c/filetype/event.py	2006-08-14 14:58:29 UTC (rev 69475)
@@ -6,4 +6,5 @@
     interface.implements(interfaces.IFileTypeModifiedEvent)
 
 
+
     

Modified: z3c.filetype/trunk/src/z3c/filetype/interfaces/__init__.py
===================================================================
--- z3c.filetype/trunk/src/z3c/filetype/interfaces/__init__.py	2006-08-14 14:42:14 UTC (rev 69474)
+++ z3c.filetype/trunk/src/z3c/filetype/interfaces/__init__.py	2006-08-14 14:58:29 UTC (rev 69475)
@@ -9,7 +9,6 @@
 
     """This event is fired when the filetypes change on an object"""
 
-
 class ITypeableFile(interface.Interface):
 
     """A file object that is typeable"""



More information about the Checkins mailing list