Back to index

moin  1.9.0~rc2
reporterstream.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 """
00003     werkzeug.contrib.reporterstream
00004     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00005 
00006     This module implements a class that can wrap `wsgi.input` in order to be
00007     informed about changes of the stream.  This is useful if you want to
00008     display a progress bar for the upload.
00009 
00010     :copyright: (c) 2009 by the Werkzeug Team, see AUTHORS for more details.
00011     :license: BSD, see LICENSE for more details.
00012 """
00013 from warnings import warn
00014 warn(DeprecationWarning('reporter stream is deprecated.  If you want to continue '
00015                         'using this class copy the module code from the '
00016                         'werkzeug wiki: http://dev.pocoo.org/projects/werkzeug/'
00017                         'wiki/ReporterStream'), stacklevel=2)
00018 
00019 
00020 class BaseReporterStream(object):
00021     """
00022     This class can be used to wrap `wsgi.input` in order to be informed about
00023     changes of the stream.
00024 
00025     Usage::
00026 
00027         from random import randrange
00028 
00029         class ReporterStream(BaseReporterStream):
00030 
00031             def __init__(self, environ):
00032                 super(ReporterStream, self).__init__(environ, 1024 * 16)
00033                 self.transport_id = randrange(0, 100000)
00034 
00035             def processed(self):
00036                 s = self.environ['my.session.service']
00037                 s.store['upload/%s' % self.transport_id] = (self.pos, self.length)
00038                 s.flush()
00039 
00040 
00041     And before accessing `request.form` or similar attributes add the stream:
00042 
00043         stream = ReporterStream(environ)
00044         environ['wsgi.input'] = stream
00045     """
00046 
00047     def __init__(self, environ, threshold):
00048         self.threshold = threshold
00049         self.length = int(environ.get('CONTENT_LENGTH') or 0)
00050         self.pos = 0
00051         self.environ = environ
00052         self._stream = environ['wsgi.input']
00053 
00054     def processed(self):
00055         """Called after pos has changed for threshold or a line was read."""
00056 
00057     def read(self, size=None):
00058         length = self.length
00059         threshold = self.threshold
00060         buffer = []
00061 
00062         if size is None:
00063             while self.pos < length:
00064                 step = min(threshold, length - self.pos)
00065                 data = self._stream.read(step)
00066                 self.pos += step
00067                 self.processed()
00068                 buffer.append(data)
00069         else:
00070             read = 0
00071             while read < size:
00072                 step = min(threshold, length - self.pos)
00073                 step = min(step, size)
00074                 data = self._stream.read(step)
00075                 self.pos += step
00076                 read += step
00077                 self.processed()
00078                 buffer.append(data)
00079 
00080         return ''.join(buffer)
00081 
00082     def readline(self, *args):
00083         line = self._stream.readline(*args)
00084         self.pos += len(line)
00085         self.processed()
00086         return line
00087 
00088     def readlines(self, hint=None):
00089         result = []
00090         while self.pos < self.length:
00091             result.append(self.readline())
00092         return result