Changeset 6536


Ignore:
Timestamp:
23 Jul 2011, 02:40:14 (13 years ago)
Author:
uli
Message:

The nice thing about all the past trouble: WAeUPImageFile is now much
more lightweight.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/image/image.py

    r6312 r6536  
    2323Components for handling image files.
    2424"""
    25 import os
    26 from ZODB.blob import Blob
    2725from hurry.file import HurryFile
     26from hurry.file.interfaces import IFileRetrieval
     27from zope.component import getUtility
    2828from zope.interface import implements
    2929from waeup.sirp.image.interfaces import IWAeUPImageFile
     
    3232    """A file prepared for storing image files.
    3333
    34     This file type is built upon :class:`hurry.file.HurryFile`. It
    35     stores the file contents given by `data` in a ZODB Blob.
     34    This file type is built upon :class:`hurry.file.HurryFile` but
     35    implements a derived marker interface to distuingish it from
     36    regular hurry files.
    3637
    37     The `filename` passed can be retrieved later as the `filename`
    38     attribute although the contents of this parameter makes no
    39     difference for us.
     38    To create a :class:`WAeUPImageFile` you should use
     39    :func:`createWAeUPImageFile`.
    4040    """
    4141    implements(IWAeUPImageFile)
    4242
    43     def __init__(self, filename, data):
    44         self.filename = filename
    45         self._file = Blob(data)
    46         self.headers = {}
    47 
    48     def __del__(self):
    49         """Remove the real filesystem file bound to the blob.
    50 
    51         Blob content is initially written to some real filesystem
    52         file. This file might linger around in temporary dirs when
    53         :class:`WAeUPImageFile` instances are created without storing
    54         them in ZODB afterwards. This often happens in tests.
    55 
    56         We remove that file too when we are about to be deleted.
    57         """
    58         if self._p_oid is not None or self._file._p_oid is not None:
    59             # Don't mess up internal ZODB structure
    60             return
    61         f = self._file.open('r')
    62         name = getattr(f, 'name', None)
    63         f.close()
    64         if name is not None and os.path.exists(name) and os.path.isfile(name):
    65             os.unlink(name)
    66         return
    67 
    68     def _getFile(self):
    69         return self._file.open('r')
    70 
    71     #: A Python file-object already openend for reading containing the
    72     #: stored file.
    73     file = property(_getFile)
    74 
    75     @property
    76     def size(self):
    77         """The size of the stored file in bytes.
    78         """
    79         f = self._file.open('r')
    80         size = int(os.fstat(f.fileno()).st_size)
    81         f.close()
    82         return size
    83 
    84     @property
    85     def data(self):
    86         """The contents of the stored file.
    87         """
    88         f = self._file.open('r')
    89         result = f.read()
    90         f.close()
    91         return result
     43def createWAeUPImageFile(filename, f):
     44    retrieval = getUtility(IFileRetrieval)
     45    return retrieval.createFile(filename, f)
Note: See TracChangeset for help on using the changeset viewer.