[Checkins] SVN: z3c.blobfile/trunk/src/z3c/blobfile/ Fixed tests and provided support for file and chunk reading

Uwe Oestermeier u.oestermeier at iwm-kmrc.de
Sat Nov 10 13:57:31 EST 2007


Log message for revision 81728:
  Fixed tests and provided support for file and chunk reading

Changed:
  U   z3c.blobfile/trunk/src/z3c/blobfile/file.py
  U   z3c.blobfile/trunk/src/z3c/blobfile/image.py
  D   z3c.blobfile/trunk/src/z3c/blobfile/test_image.py
  U   z3c.blobfile/trunk/src/z3c/blobfile/tests.py

-=-
Modified: z3c.blobfile/trunk/src/z3c/blobfile/file.py
===================================================================
--- z3c.blobfile/trunk/src/z3c/blobfile/file.py	2007-11-10 18:23:08 UTC (rev 81727)
+++ z3c.blobfile/trunk/src/z3c/blobfile/file.py	2007-11-10 18:57:30 UTC (rev 81728)
@@ -22,6 +22,7 @@
 from zope.interface import implements
 import zope.app.publication.interfaces
 from zope.app.file import interfaces
+from zope.app.file.file import FileChunk
 
 from ZODB.blob import Blob
 
@@ -128,113 +129,52 @@
         fp = self._data.open('w')
         fp.write(data)
         fp.close()
+
+    def open(self, mode="r"):
+        return self._data.open(mode)
+
+    def _setData(self, data):
+        # Handle case when data is a string
+        if isinstance(data, unicode):
+            data = data.encode('UTF-8')
+
+        if isinstance(data, str):
+            fp = self._data.open('w')
+            fp.write(data)
+            fp.close()
+
+        if data is None:
+            raise TypeError('Cannot set None data on a file.')
+
+        # Handle case when data is already a FileChunk
         
-    def read(self):
+        if isinstance(data, FileChunk):
+            fp = self._data.open('w')
+            chunk = data
+            while chunk:
+                fp.write(chunk._data)
+                chunk = chunk.next
+            fp.close()
+            return
+
+        # Handle case when data is a file object
+        seek = data.seek
+        read = data.read
+
+        fp = self._data.open('w')
+        block = data.read(MAXCHUNKSIZE)
+        while block:
+            fp.write(block)
+        fp.close()
+
+    def _getData(self):
         fp = self._data.open('r')
         data = fp.read()
         fp.close()
         return data
 
-    def write(self, data):
-        fp = self._data.open('w')
-        fp.write(data)
-        fp.close()
-        
-    data = property(read, write)
-    
-   
+    data = property(_getData, _setData)    
 
-    
-#     def _getData(self):
-#         if isinstance(self._data, FileChunk):
-#             return str(self._data)
-#         else:
-#             return self._data
-# 
-#     def _setData(self, data) :
-# 
-#         # Handle case when data is a string
-#         if isinstance(data, unicode):
-#             data = data.encode('UTF-8')
-# 
-#         if isinstance(data, str):
-#             self._data, self._size = FileChunk(data), len(data)
-#             return
-# 
-#         # Handle case when data is None
-#         if data is None:
-#             raise TypeError('Cannot set None data on a file.')
-# 
-#         # Handle case when data is already a FileChunk
-#         if isinstance(data, FileChunk):
-#             size = len(data)
-#             self._data, self._size = data, size
-#             return
-# 
-#         # Handle case when data is a file object
-#         seek = data.seek
-#         read = data.read
-# 
-#         seek(0, 2)
-#         size = end = data.tell()
-# 
-#         if size <= 2*MAXCHUNKSIZE:
-#             seek(0)
-#             if size < MAXCHUNKSIZE:
-#                 self._data, self._size = read(size), size
-#                 return
-#             self._data, self._size = FileChunk(read(size)), size
-#             return
-# 
-#         # Make sure we have an _p_jar, even if we are a new object, by
-#         # doing a sub-transaction commit.
-#         transaction.savepoint(optimistic=True)
-# 
-#         jar = self._p_jar
-# 
-#         if jar is None:
-#             # Ugh
-#             seek(0)
-#             self._data, self._size = FileChunk(read(size)), size
-#             return
-# 
-#         # Now we're going to build a linked list from back
-#         # to front to minimize the number of database updates
-#         # and to allow us to get things out of memory as soon as
-#         # possible.
-#         next = None
-#         while end > 0:
-#             pos = end - MAXCHUNKSIZE
-#             if pos < MAXCHUNKSIZE:
-#                 pos = 0 # we always want at least MAXCHUNKSIZE bytes
-#             seek(pos)
-#             data = FileChunk(read(end - pos))
-# 
-#             # Woooop Woooop Woooop! This is a trick.
-#             # We stuff the data directly into our jar to reduce the
-#             # number of updates necessary.
-#             jar.add(data)
-# 
-#             # This is needed and has side benefit of getting
-#             # the thing registered:
-#             data.next = next
-# 
-#             # Now make it get saved in a sub-transaction!
-#             transaction.savepoint(optimistic=True)
-# 
-#             # Now make it a ghost to free the memory.  We
-#             # don't need it anymore!
-#             data._p_changed = None
-# 
-#             next = data
-#             end = pos
-# 
-#         self._data, self._size = next, size
-#         return
-
-#    data = property(_getData, _setData)
-    
-    
     @property
     def size(self):
         if self._data == "":
@@ -268,4 +208,4 @@
         self.context = context
 
     def write(self, data):
-        self.context.write(data)
+        self.context._setData(data)

Modified: z3c.blobfile/trunk/src/z3c/blobfile/image.py
===================================================================
--- z3c.blobfile/trunk/src/z3c/blobfile/image.py	2007-11-10 18:23:08 UTC (rev 81727)
+++ z3c.blobfile/trunk/src/z3c/blobfile/image.py	2007-11-10 18:57:30 UTC (rev 81728)
@@ -40,10 +40,11 @@
         self.contentType, self._width, self._height = getImageInfo(data)
         self.data = data
 
-    def write(self, data):
-        super(Image, self).write(data)
-
-        contentType, self._width, self._height = getImageInfo(self._data)
+    def _setData(self, data):
+        fp = self._data.open('w')
+        fp.write(data)
+        fp.close()
+        contentType, self._width, self._height = getImageInfo(data)
         if contentType:
             self.contentType = contentType
 
@@ -51,7 +52,7 @@
         '''See interface `IImage`'''
         return (self._width, self._height)
 
-    data = property(File.read, write)
+    data = property(File._getData, _setData)
 
 class ImageSized(object):
     implements(ISized)

Deleted: z3c.blobfile/trunk/src/z3c/blobfile/test_image.py
===================================================================
--- z3c.blobfile/trunk/src/z3c/blobfile/test_image.py	2007-11-10 18:23:08 UTC (rev 81727)
+++ z3c.blobfile/trunk/src/z3c/blobfile/test_image.py	2007-11-10 18:57:30 UTC (rev 81728)
@@ -1,184 +0,0 @@
-##############################################################################
-#
-# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
-# All Rights Reserved.
-#
-# This software is subject to the provisions of the Zope Public License,
-# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
-# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
-# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
-# FOR A PARTICULAR PURPOSE.
-#
-##############################################################################
-"""Test Image content component
-
-$Id: test_image.py 76693 2007-06-14 13:39:36Z mgedmin $
-"""
-import unittest
-from zope.interface.verify import verifyClass
-from zope.app.file.interfaces import IImage
-from zope.app.file.image import Image, FileFactory, ImageSized
-from zope.app.file.file import File, FileWriteFile, FileReadFile
-
-zptlogo = (
-    'GIF89a\x10\x00\x10\x00\xd5\x00\x00\xff\xff\xff\xff\xff\xfe\xfc\xfd\xfd'
-    '\xfa\xfb\xfc\xf7\xf9\xfa\xf5\xf8\xf9\xf3\xf6\xf8\xf2\xf5\xf7\xf0\xf4\xf6'
-    '\xeb\xf1\xf3\xe5\xed\xef\xde\xe8\xeb\xdc\xe6\xea\xd9\xe4\xe8\xd7\xe2\xe6'
-    '\xd2\xdf\xe3\xd0\xdd\xe3\xcd\xdc\xe1\xcb\xda\xdf\xc9\xd9\xdf\xc8\xd8\xdd'
-    '\xc6\xd7\xdc\xc4\xd6\xdc\xc3\xd4\xda\xc2\xd3\xd9\xc1\xd3\xd9\xc0\xd2\xd9'
-    '\xbd\xd1\xd8\xbd\xd0\xd7\xbc\xcf\xd7\xbb\xcf\xd6\xbb\xce\xd5\xb9\xcd\xd4'
-    '\xb6\xcc\xd4\xb6\xcb\xd3\xb5\xcb\xd2\xb4\xca\xd1\xb2\xc8\xd0\xb1\xc7\xd0'
-    '\xb0\xc7\xcf\xaf\xc6\xce\xae\xc4\xce\xad\xc4\xcd\xab\xc3\xcc\xa9\xc2\xcb'
-    '\xa8\xc1\xca\xa6\xc0\xc9\xa4\xbe\xc8\xa2\xbd\xc7\xa0\xbb\xc5\x9e\xba\xc4'
-    '\x9b\xbf\xcc\x98\xb6\xc1\x8d\xae\xbaFgs\x00\x00\x00\x00\x00\x00\x00\x00'
-    '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
-    '\x00,\x00\x00\x00\x00\x10\x00\x10\x00\x00\x06z@\x80pH,\x12k\xc8$\xd2f\x04'
-    '\xd4\x84\x01\x01\xe1\xf0d\x16\x9f\x80A\x01\x91\xc0ZmL\xb0\xcd\x00V\xd4'
-    '\xc4a\x87z\xed\xb0-\x1a\xb3\xb8\x95\xbdf8\x1e\x11\xca,MoC$\x15\x18{'
-    '\x006}m\x13\x16\x1a\x1f\x83\x85}6\x17\x1b $\x83\x00\x86\x19\x1d!%)\x8c'
-    '\x866#\'+.\x8ca`\x1c`(,/1\x94B5\x19\x1e"&*-024\xacNq\xba\xbb\xb8h\xbeb'
-    '\x00A\x00;'
-    )
-
-class TestImage(unittest.TestCase):
-
-    def _makeImage(self, *args, **kw):
-        return Image(*args, **kw)
-
-    def testEmpty(self):
-        file = self._makeImage()
-        self.assertEqual(file.contentType, '')
-        self.assertEqual(file.data, '')
-
-    def testConstructor(self):
-        file = self._makeImage('Data')
-        self.assertEqual(file.contentType, '')
-        self.assertEqual(file.data, 'Data')
-
-    def testMutators(self):
-        image = self._makeImage()
-
-        image.contentType = 'image/jpeg'
-        self.assertEqual(image.contentType, 'image/jpeg')
-
-        image._setData(zptlogo)
-        self.assertEqual(image.data, zptlogo)
-        self.assertEqual(image.contentType, 'image/gif')
-        self.assertEqual(image.getImageSize(), (16, 16))
-
-    def testInterface(self):
-        self.failUnless(IImage.implementedBy(Image))
-        self.failUnless(verifyClass(IImage, Image))
-
-class TestFileAdapters(unittest.TestCase):
-
-    def _makeFile(self, *args, **kw):
-        return Image(*args, **kw)
-
-    def test_ReadFile(self):
-        file = self._makeFile()
-        content = "This is some file\ncontent."
-        file.data = content
-        file.contentType = 'text/plain'
-        self.assertEqual(FileReadFile(file).read(), content)
-        self.assertEqual(FileReadFile(file).size(), len(content))
-
-    def test_WriteFile(self):
-        file = self._makeFile()
-        content = "This is some file\ncontent."
-        FileWriteFile(file).write(content)
-        self.assertEqual(file.data, content)
-
-class DummyImage(object):
-
-    def __init__(self, width, height, bytes):
-        self.width = width
-        self.height = height
-        self.bytes = bytes
-
-    def getSize(self):
-        return self.bytes
-
-    def getImageSize(self):
-        return self.width, self.height
-
-
-class TestFileFactory(unittest.TestCase):
-
-    def test_image(self):
-        factory = FileFactory(None)
-        f = factory("spam.txt", "image/foo", "hello world")
-        self.assert_(isinstance(f, Image), f)
-        f = factory("spam.txt", "", zptlogo)
-        self.assert_(isinstance(f, Image), f)
-
-    def test_text(self):
-        factory = FileFactory(None)
-        f = factory("spam.txt", "", "hello world")
-        self.assert_(isinstance(f, File), f)
-        self.assert_(not isinstance(f, Image), f)
-        f = factory("spam.txt", "", "\0\1\2\3\4")
-        self.assert_(isinstance(f, File), f)
-        self.assert_(not isinstance(f, Image), f)
-        f = factory("spam.txt", "text/splat", zptlogo)
-        self.assert_(isinstance(f, File), f)
-        self.assert_(not isinstance(f, Image), f)
-        f = factory("spam.txt", "application/splat", zptlogo)
-        self.assert_(isinstance(f, File), f)
-        self.assert_(not isinstance(f, Image), f)
-
-class TestSized(unittest.TestCase):
-
-    def testInterface(self):
-        from zope.size.interfaces import ISized
-        self.failUnless(ISized.implementedBy(ImageSized))
-        self.failUnless(verifyClass(ISized, ImageSized))
-
-    def test_zeroSized(self):
-        s = ImageSized(DummyImage(0, 0, 0))
-        self.assertEqual(s.sizeForSorting(), ('byte', 0))
-        self.assertEqual(s.sizeForDisplay(), u'0 KB ${width}x${height}')
-        self.assertEqual(s.sizeForDisplay().mapping['width'], '0')
-        self.assertEqual(s.sizeForDisplay().mapping['height'], '0')
-
-    def test_arbitrarySize(self):
-        s = ImageSized(DummyImage(34, 56, 78))
-        self.assertEqual(s.sizeForSorting(), ('byte', 78))
-        self.assertEqual(s.sizeForDisplay(), u'1 KB ${width}x${height}')
-        self.assertEqual(s.sizeForDisplay().mapping['width'], '34')
-        self.assertEqual(s.sizeForDisplay().mapping['height'], '56')
-
-    def test_unknownSize(self):
-        s = ImageSized(DummyImage(-1, -1, 23))
-        self.assertEqual(s.sizeForSorting(), ('byte', 23))
-        self.assertEqual(s.sizeForDisplay(), u'1 KB ${width}x${height}')
-        self.assertEqual(s.sizeForDisplay().mapping['width'], '?')
-        self.assertEqual(s.sizeForDisplay().mapping['height'], '?')
-
-    def test_getImageInfo(self):
-        from zope.app.file.image import getImageInfo
-        t, w, h = getImageInfo("\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00C")
-
-    def test_getImageInfo_bmp(self):
-        from zope.app.file.image import getImageInfo
-        t, w, h = getImageInfo('BMl\x05\x00\x00\x00\x00\x00\x006\x04\x00\x00('
-                               '\x00\x00\x00\x10\x00\x00\x00\x10\x00\x00\x00'
-                               '\x01\x00\x08\x00\x01\x00\x00\x006\x01\x00\x00'
-                               '\x12\x0b\x00\x00\x12\x0b\x00\x00\x00\x01\x00'
-                               '... and so on ...')
-        self.assertEqual(t, "image/x-ms-bmp")
-        self.assertEqual(w, 16)
-        self.assertEqual(h, 16)
-
-
-def test_suite():
-    return unittest.TestSuite((
-        unittest.makeSuite(TestImage),
-        unittest.makeSuite(TestFileAdapters),
-        unittest.makeSuite(TestFileFactory),
-        unittest.makeSuite(TestSized)
-        ))
-
-if __name__=='__main__':
-    unittest.TextTestRunner().run(test_suite())

Modified: z3c.blobfile/trunk/src/z3c/blobfile/tests.py
===================================================================
--- z3c.blobfile/trunk/src/z3c/blobfile/tests.py	2007-11-10 18:23:08 UTC (rev 81727)
+++ z3c.blobfile/trunk/src/z3c/blobfile/tests.py	2007-11-10 18:57:30 UTC (rev 81728)
@@ -56,17 +56,17 @@
         self.assertEqual(file.contentType, '')
         self.assertEqual(file.data, 'Data')
 
-#     def testMutators(self):
-#         image = self._makeImage()
-# 
-#         image.contentType = 'image/jpeg'
-#         self.assertEqual(image.contentType, 'image/jpeg')
-# 
-#         image.write(zptlogo)
-#         self.assertEqual(image.data, zptlogo)
-#         self.assertEqual(image.contentType, 'image/gif')
-#         self.assertEqual(image.getImageSize(), (16, 16))
+    def testMutators(self):
+        image = self._makeImage()
 
+        image.contentType = 'image/jpeg'
+        self.assertEqual(image.contentType, 'image/jpeg')
+
+        image._setData(zptlogo)
+        self.assertEqual(image.data, zptlogo)
+        self.assertEqual(image.contentType, 'image/gif')
+        self.assertEqual(image.getImageSize(), (16, 16))
+
     def testInterface(self):
         self.failUnless(IImage.implementedBy(Image))
         self.failUnless(verifyClass(IImage, Image))



More information about the Checkins mailing list