[Checkins] SVN: z3c.filetype/trunk/src/z3c/filetype/ added
applyInterfaces method
Bernd Dorn
bernd.dorn at fhv.at
Fri Aug 11 10:54:23 EDT 2006
Log message for revision 69404:
added applyInterfaces method
Changed:
U z3c.filetype/trunk/src/z3c/filetype/README.txt
U z3c.filetype/trunk/src/z3c/filetype/TODO.txt
U z3c.filetype/trunk/src/z3c/filetype/api.py
A 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-11 14:03:36 UTC (rev 69403)
+++ z3c.filetype/trunk/src/z3c/filetype/README.txt 2006-08-11 14:54:22 UTC (rev 69404)
@@ -77,3 +77,39 @@
'/.../z3c/filetype/testdata/test.tar'
>>> sorted(api.getInterfacesFor(f.name))
[<InterfaceClass z3c.filetype.interfaces.filetypes.ITARFile>]
+
+
+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.
+
+ >>> from zope.component import eventtesting
+ >>> eventtesting.setUp()
+
+ >>> from z3c.filetype import interfaces
+ >>> from zope import interface
+ >>> class Foo(object):
+ ... interface.implements(interfaces.ITypeableFile)
+ ... def __init__(self, f):
+ ... self.data = f
+ >>> foo = Foo(f)
+
+The applInterfaces method returns a boolean if changes occured.
+
+ >>> api.applyInterfaces(foo)
+ True
+
+And an event should be have been fired.
+
+ >>> eventtesting.getEvents()
+ [<z3c.filetype.event.FileTypeModifiedEvent object at ...>]
+
+ >>> api.applyInterfaces(foo)
+ False
+
+Now the object should implement the right interface according to the
+ata contained.
+
+ >>> interfaces.filetypes.ITARFile.providedBy(foo)
+ True
+
Modified: z3c.filetype/trunk/src/z3c/filetype/TODO.txt
===================================================================
--- z3c.filetype/trunk/src/z3c/filetype/TODO.txt 2006-08-11 14:03:36 UTC (rev 69403)
+++ z3c.filetype/trunk/src/z3c/filetype/TODO.txt 2006-08-11 14:54:22 UTC (rev 69404)
@@ -6,4 +6,4 @@
Adapters from IFileType to IImage, IFile
-
+Freeze api and write interface
Modified: z3c.filetype/trunk/src/z3c/filetype/api.py
===================================================================
--- z3c.filetype/trunk/src/z3c/filetype/api.py 2006-08-11 14:03:36 UTC (rev 69403)
+++ z3c.filetype/trunk/src/z3c/filetype/api.py 2006-08-11 14:54:22 UTC (rev 69404)
@@ -1,7 +1,10 @@
from z3c.filetype import magic
+import interfaces
from interfaces import filetypes
from zope.contenttype import guess_content_type
from zope import interface
+from zope.event import notify
+from event import FileTypeModifiedEvent
magicFile = magic.MagicFile()
@@ -41,6 +44,18 @@
ifaces.update(byMimeType(t))
return ifaces
+def applyInterfaces(obj):
+ assert(interfaces.ITypeableFile.providedBy(obj))
+ ifaces = InterfaceSet(*getInterfacesFor(obj.data))
+ provided = set(interface.directlyProvidedBy(obj))
+ for iface in provided:
+ ifaces.add(iface)
+ if set(ifaces)!=provided:
+ for iface in ifaces:
+ interface.directlyProvides(obj,iface)
+ notify(FileTypeModifiedEvent(obj))
+ return True
+ return False
class InterfaceSet(object):
Added: z3c.filetype/trunk/src/z3c/filetype/event.py
===================================================================
--- z3c.filetype/trunk/src/z3c/filetype/event.py 2006-08-11 14:03:36 UTC (rev 69403)
+++ z3c.filetype/trunk/src/z3c/filetype/event.py 2006-08-11 14:54:22 UTC (rev 69404)
@@ -0,0 +1,8 @@
+from zope.lifecycleevent import ObjectModifiedEvent
+from zope import interface
+import interfaces
+
+class FileTypeModifiedEvent(ObjectModifiedEvent):
+ interface.implements(interfaces.IFileTypeModifiedEvent)
+
+
Property changes on: z3c.filetype/trunk/src/z3c/filetype/event.py
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Modified: z3c.filetype/trunk/src/z3c/filetype/interfaces/__init__.py
===================================================================
--- z3c.filetype/trunk/src/z3c/filetype/interfaces/__init__.py 2006-08-11 14:03:36 UTC (rev 69403)
+++ z3c.filetype/trunk/src/z3c/filetype/interfaces/__init__.py 2006-08-11 14:54:22 UTC (rev 69404)
@@ -1 +1,15 @@
-# interaces package
+from zope.lifecycleevent.interfaces import IObjectModifiedEvent
+from zope import interface
+
+class IFileTypeModifiedEvent(IObjectModifiedEvent):
+
+ """This event is fired when the filetypes change on an object"""
+
+
+class ITypeableFile(interface.Interface):
+
+ """A file object that is typeable"""
+
+ data = interface.Attribute('Data of the file')
+
+
More information about the Checkins
mailing list