[5971] | 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 | ## |
---|
[5976] | 22 | """ |
---|
| 23 | Components for handling image files. |
---|
| 24 | """ |
---|
[6309] | 25 | import os |
---|
| 26 | from ZODB.blob import Blob |
---|
[5971] | 27 | from hurry.file import HurryFile |
---|
[5976] | 28 | from zope.interface import implements |
---|
[5979] | 29 | from waeup.sirp.image.interfaces import IWAeUPImageFile |
---|
[5971] | 30 | |
---|
[5979] | 31 | class WAeUPImageFile(HurryFile): |
---|
[5971] | 32 | """A file prepared for storing image files. |
---|
| 33 | |
---|
[6312] | 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. |
---|
[5971] | 40 | """ |
---|
[5979] | 41 | implements(IWAeUPImageFile) |
---|
[6309] | 42 | |
---|
| 43 | def __init__(self, filename, data): |
---|
| 44 | self.filename = filename |
---|
| 45 | self._file = Blob(data) |
---|
| 46 | self.headers = {} |
---|
| 47 | |
---|
[6312] | 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 | |
---|
[6309] | 68 | def _getFile(self): |
---|
| 69 | return self._file.open('r') |
---|
| 70 | |
---|
[6312] | 71 | #: A Python file-object already openend for reading containing the |
---|
| 72 | #: stored file. |
---|
[6309] | 73 | file = property(_getFile) |
---|
| 74 | |
---|
| 75 | @property |
---|
| 76 | def size(self): |
---|
[6312] | 77 | """The size of the stored file in bytes. |
---|
| 78 | """ |
---|
[6309] | 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): |
---|
[6312] | 86 | """The contents of the stored file. |
---|
| 87 | """ |
---|
[6309] | 88 | f = self._file.open('r') |
---|
| 89 | result = f.read() |
---|
| 90 | f.close() |
---|
| 91 | return result |
---|