source: main/waeup.sirp/trunk/src/waeup/sirp/image/image.py @ 6525

Last change on this file since 6525 was 6312, checked in by uli, 13 years ago
  • Add a destructor that removes the bound file of the bound Blob when a WAeUPImageFile gets deleted. Otherwise for each once created WAeUPImageFile that was not stored into ZODB we would get a lingering copy of the file contents in the system temporary dir.
  • Update docs.
File size: 2.9 KB
Line 
1##
2## image.py
3## Login : <uli@pu.smp.net>
4## Started on  Mon Apr 25 12:32:42 2011 Uli Fouquet
5## $Id$
6##
7## Copyright (C) 2011 Uli Fouquet
8## This program is free software; you can redistribute it and/or modify
9## it under the terms of the GNU General Public License as published by
10## the Free Software Foundation; either version 2 of the License, or
11## (at your option) any later version.
12##
13## This program is distributed in the hope that it will be useful,
14## but WITHOUT ANY WARRANTY; without even the implied warranty of
15## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16## GNU General Public License for more details.
17##
18## You should have received a copy of the GNU General Public License
19## along with this program; if not, write to the Free Software
20## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21##
22"""
23Components for handling image files.
24"""
25import os
26from ZODB.blob import Blob
27from hurry.file import HurryFile
28from zope.interface import implements
29from waeup.sirp.image.interfaces import IWAeUPImageFile
30
31class WAeUPImageFile(HurryFile):
32    """A file prepared for storing image files.
33
34    This file type is built upon :class:`hurry.file.HurryFile`. It
35    stores the file contents given by `data` in a ZODB Blob.
36
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.
40    """
41    implements(IWAeUPImageFile)
42
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
Note: See TracBrowser for help on using the repository browser.