Back to index

moin  1.9.0~rc2
Public Member Functions | Public Attributes | Private Attributes
MoinMoin.support.werkzeug.utils.LimitedStream Class Reference

List of all members.

Public Member Functions

def __init__
def __iter__
def is_exhausted
def on_exhausted
def exhaust
def read
def readline
def readlines
def next

Public Attributes

 limit
 silent

Private Attributes

 _stream
 _pos

Detailed Description

Wraps a stream so that it doesn't read more than n bytes.  If the
stream is exhausted and the caller tries to get more bytes from it
:func:`on_exhausted` is called which by default returns an empty
string or raises :exc:`~werkzeug.exceptions.BadRequest` if silent
is set to `False`.  The return value of that function is forwarded
to the reader function.  So if it returns an empty string
:meth:`read` will return an empty string as well.

The limit however must never be higher than what the stream can
output.  Otherwise :meth:`readlines` will try to read past the
limit.

The `silent` parameter has no effect if :meth:`is_exhausted` is
overriden by a subclass.

.. admonition:: Note on WSGI compliance

   calls to :meth:`readline` and :meth:`readlines` are not
   WSGI compliant because it passes a size argument to the
   readline methods.  Unfortunately the WSGI PEP is not safely
   implementable without a size argument to :meth:`readline`
   because there is no EOF marker in the stream.  As a result
   of that the use of :meth:`readline` is discouraged.

   For the same reason iterating over the :class:`LimitedStream`
   is not portable.  It internally calls :meth:`readline`.

   We strongly suggest using :meth:`read` only or using the
   :func:`make_line_iter` which savely iterates line-based
   over a WSGI input stream.

:param stream: the stream to wrap.
:param limit: the limit for the stream, must not be longer than
              what the string can provide if the stream does not
              end with `EOF` (like `wsgi.input`)
:param silent: If set to `True` the stream will allow reading
               past the limit and will return an empty string.

Definition at line 429 of file utils.py.


Constructor & Destructor Documentation

def MoinMoin.support.werkzeug.utils.LimitedStream.__init__ (   self,
  stream,
  limit,
  silent = True 
)

Definition at line 469 of file utils.py.

00469 
00470     def __init__(self, stream, limit, silent=True):
00471         self._stream = stream
00472         self._pos = 0
00473         self.limit = limit
00474         self.silent = silent


Member Function Documentation

Definition at line 475 of file utils.py.

00475 
00476     def __iter__(self):
00477         return self

def MoinMoin.support.werkzeug.utils.LimitedStream.exhaust (   self,
  chunk_size = 1024 * 16 
)
Exhaust the stream.  This consumes all the data left until the
limit is reached.

:param chunk_size: the size for a chunk.  It will read the chunk
           until the stream is exhausted and throw away
           the results.

Definition at line 494 of file utils.py.

00494 
00495     def exhaust(self, chunk_size=1024 * 16):
00496         """Exhaust the stream.  This consumes all the data left until the
00497         limit is reached.
00498 
00499         :param chunk_size: the size for a chunk.  It will read the chunk
00500                            until the stream is exhausted and throw away
00501                            the results.
00502         """
00503         to_read = self.limit - self._pos
00504         chunk = chunk_size
00505         while to_read > 0:
00506             chunk = min(to_read, chunk)
00507             self.read(chunk)
00508             to_read -= chunk

If the stream is exhausted this attribute is `True`.

Definition at line 479 of file utils.py.

00479 
00480     def is_exhausted(self):
00481         """If the stream is exhausted this attribute is `True`."""
00482         return self._pos >= self.limit

Definition at line 556 of file utils.py.

00556 
00557     def next(self):
00558         line = self.readline()
00559         if line is None:
00560             raise StopIteration()
00561         return line
00562 

Here is the call graph for this function:

Here is the caller graph for this function:

This is called when the stream tries to read past the limit.
The return value of this function is returned from the reading
function.

Per default this raises a :exc:`~werkzeug.exceptions.BadRequest`.

Definition at line 483 of file utils.py.

00483 
00484     def on_exhausted(self):
00485         """This is called when the stream tries to read past the limit.
00486         The return value of this function is returned from the reading
00487         function.
00488 
00489         Per default this raises a :exc:`~werkzeug.exceptions.BadRequest`.
00490         """
00491         if self.silent:
00492             return ''
00493         raise BadRequest('input stream exhausted')

Here is the caller graph for this function:

def MoinMoin.support.werkzeug.utils.LimitedStream.read (   self,
  size = None 
)
Read `size` bytes or if size is not provided everything is read.

:param size: the number of bytes read.

Definition at line 509 of file utils.py.

00509 
00510     def read(self, size=None):
00511         """Read `size` bytes or if size is not provided everything is read.
00512 
00513         :param size: the number of bytes read.
00514         """
00515         if self._pos >= self.limit:
00516             return self.on_exhausted()
00517         if size is None:
00518             size = self.limit
00519         read = self._stream.read(min(self.limit - self._pos, size))
00520         self._pos += len(read)
00521         return read

Here is the call graph for this function:

Here is the caller graph for this function:

def MoinMoin.support.werkzeug.utils.LimitedStream.readline (   self,
  size = None 
)
Reads one line from the stream.

Definition at line 522 of file utils.py.

00522 
00523     def readline(self, size=None):
00524         """Reads one line from the stream."""
00525         if self._pos >= self.limit:
00526             return self.on_exhausted()
00527         if size is None:
00528             size = self.limit - self._pos
00529         else:
00530             size = min(size, self.limit - self._pos)
00531         line = self._stream.readline(size)
00532         self._pos += len(line)
00533         return line

Here is the call graph for this function:

Here is the caller graph for this function:

Reads a file into a list of strings.  It calls :meth:`readline`
until the file is read to the end.  It does support the optional
`size` argument if the underlaying stream supports it for
`readline`.

Definition at line 534 of file utils.py.

00534 
00535     def readlines(self, size=None):
00536         """Reads a file into a list of strings.  It calls :meth:`readline`
00537         until the file is read to the end.  It does support the optional
00538         `size` argument if the underlaying stream supports it for
00539         `readline`.
00540         """
00541         last_pos = self._pos
00542         result = []
00543         if size is not None:
00544             end = min(self.limit, last_pos + size)
00545         else:
00546             end = self.limit
00547         while 1:
00548             if size is not None:
00549                 size -= last_pos - self._pos
00550             if self._pos >= end:
00551                 break
00552             result.append(self.readline(size))
00553             if size is not None:
00554                 last_pos = self._pos
00555         return result

Here is the call graph for this function:


Member Data Documentation

Definition at line 471 of file utils.py.

Definition at line 470 of file utils.py.

Definition at line 472 of file utils.py.

Definition at line 473 of file utils.py.


The documentation for this class was generated from the following file: