source: QX/RPlain/plot.py @ 954

Revision 954, 2.7 KB checked in by fhoffmann, 2 years ago (diff)

new registerImage (copy of elntest)

Line 
1#
2#  Copyright (c) 2008 Helmut Merz helmutm@cy55.de
3#
4#  This program is free software; you can redistribute it and/or modify
5#  it under the terms of the GNU General Public License as published by
6#  the Free Software Foundation; either version 2 of the License, or
7#  (at your option) any later version.
8#
9#  This program is distributed in the hope that it will be useful,
10#  but WITHOUT ANY WARRANTY; without even the implied warranty of
11#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12#  GNU General Public License for more details.
13#
14#  You should have received a copy of the GNU General Public License
15#  along with this program; if not, write to the Free Software
16#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17#
18
19"""
20Management of filesystem images (plots) and corresponding views.
21
22$Id$
23"""
24
25import os
26import time
27from zope.interface import Interface, implements
28from zope.cachedescriptors.property import Lazy
29
30import randomname, jeep
31
32
33class CachedImage(object):
34    """ Keep information about temporary image files.
35    """
36
37    def __init__(self, path, name=None):
38        self.path = path
39        if name is None:
40            self.name = randomname.generateName(lambda x: x not in cachedImages.keys())
41        else:
42            self.name = name
43        self.timeStamp = int(time.time())
44
45
46cachedImages = jeep.Jeep()
47
48def registerImage(filename, name=None):
49    image = CachedImage(filename, name)
50    cachedImages.append(image)
51    while len(cachedImages) > 10:  # clean up old cache entries
52        old = cachedImages.pop(0).path
53        if os.path.exists(old):
54            os.unlink(old)
55    return image.name
56
57
58class PlotView(object):
59    """ Access to temporary filesystem images.
60    """
61
62    def __init__(self, context, request):
63        self.context = context
64        self.request = request
65        self.traverse_subpath = []
66
67    def publishTraverse(self, request, name):
68        self.traverse_subpath.append(name)
69        return self
70
71    def __call__(self):
72        if self.traverse_subpath:
73            key = self.traverse_subpath[0]
74        else:
75            key = self.request.form.get('image', 'not_found')
76        if '.' in key:  # remove extension possibly added to make Flash happy
77            key = key.split('.', 1)[0]
78        path = cachedImages[key].path
79        self.setHeaders(path)
80        f = open(path, 'rb')
81        data = f.read()
82        f.close()
83        return data
84
85    def setHeaders(self, name=None):
86        response = self.request.response
87        # TODO: get content type from name (extension)
88        response.setHeader('Content-Type', 'image/jpeg')
89        response.setHeader('Expires', 'Sat, 1 Jan 2000 00:00:00 GMT');
90        response.setHeader('Pragma', 'no-cache');
Note: See TracBrowser for help on using the repository browser.