testing/mochitest/pywebsocket/mod_pywebsocket/memorizingfile.py

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/testing/mochitest/pywebsocket/mod_pywebsocket/memorizingfile.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,99 @@
     1.4 +#!/usr/bin/env python
     1.5 +#
     1.6 +# Copyright 2011, Google Inc.
     1.7 +# All rights reserved.
     1.8 +#
     1.9 +# Redistribution and use in source and binary forms, with or without
    1.10 +# modification, are permitted provided that the following conditions are
    1.11 +# met:
    1.12 +#
    1.13 +#     * Redistributions of source code must retain the above copyright
    1.14 +# notice, this list of conditions and the following disclaimer.
    1.15 +#     * Redistributions in binary form must reproduce the above
    1.16 +# copyright notice, this list of conditions and the following disclaimer
    1.17 +# in the documentation and/or other materials provided with the
    1.18 +# distribution.
    1.19 +#     * Neither the name of Google Inc. nor the names of its
    1.20 +# contributors may be used to endorse or promote products derived from
    1.21 +# this software without specific prior written permission.
    1.22 +#
    1.23 +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.24 +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.25 +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.26 +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    1.27 +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.28 +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    1.29 +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.30 +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.31 +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.32 +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    1.33 +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.34 +
    1.35 +
    1.36 +"""Memorizing file.
    1.37 +
    1.38 +A memorizing file wraps a file and memorizes lines read by readline.
    1.39 +"""
    1.40 +
    1.41 +
    1.42 +import sys
    1.43 +
    1.44 +
    1.45 +class MemorizingFile(object):
    1.46 +    """MemorizingFile wraps a file and memorizes lines read by readline.
    1.47 +
    1.48 +    Note that data read by other methods are not memorized. This behavior
    1.49 +    is good enough for memorizing lines SimpleHTTPServer reads before
    1.50 +    the control reaches WebSocketRequestHandler.
    1.51 +    """
    1.52 +
    1.53 +    def __init__(self, file_, max_memorized_lines=sys.maxint):
    1.54 +        """Construct an instance.
    1.55 +
    1.56 +        Args:
    1.57 +            file_: the file object to wrap.
    1.58 +            max_memorized_lines: the maximum number of lines to memorize.
    1.59 +                Only the first max_memorized_lines are memorized.
    1.60 +                Default: sys.maxint.
    1.61 +        """
    1.62 +
    1.63 +        self._file = file_
    1.64 +        self._memorized_lines = []
    1.65 +        self._max_memorized_lines = max_memorized_lines
    1.66 +        self._buffered = False
    1.67 +        self._buffered_line = None
    1.68 +
    1.69 +    def __getattribute__(self, name):
    1.70 +        if name in ('_file', '_memorized_lines', '_max_memorized_lines',
    1.71 +                    '_buffered', '_buffered_line', 'readline',
    1.72 +                    'get_memorized_lines'):
    1.73 +            return object.__getattribute__(self, name)
    1.74 +        return self._file.__getattribute__(name)
    1.75 +
    1.76 +    def readline(self, size=-1):
    1.77 +        """Override file.readline and memorize the line read.
    1.78 +
    1.79 +        Note that even if size is specified and smaller than actual size,
    1.80 +        the whole line will be read out from underlying file object by
    1.81 +        subsequent readline calls.
    1.82 +        """
    1.83 +
    1.84 +        if self._buffered:
    1.85 +            line = self._buffered_line
    1.86 +            self._buffered = False
    1.87 +        else:
    1.88 +            line = self._file.readline()
    1.89 +            if line and len(self._memorized_lines) < self._max_memorized_lines:
    1.90 +                self._memorized_lines.append(line)
    1.91 +        if size >= 0 and size < len(line):
    1.92 +            self._buffered = True
    1.93 +            self._buffered_line = line[size:]
    1.94 +            return line[:size]
    1.95 +        return line
    1.96 +
    1.97 +    def get_memorized_lines(self):
    1.98 +        """Get lines memorized so far."""
    1.99 +        return self._memorized_lines
   1.100 +
   1.101 +
   1.102 +# vi:sts=4 sw=4 et

mercurial