michael@0: #!/usr/bin/env python michael@0: # michael@0: # Copyright 2011, Google Inc. michael@0: # All rights reserved. michael@0: # michael@0: # Redistribution and use in source and binary forms, with or without michael@0: # modification, are permitted provided that the following conditions are michael@0: # met: michael@0: # michael@0: # * Redistributions of source code must retain the above copyright michael@0: # notice, this list of conditions and the following disclaimer. michael@0: # * Redistributions in binary form must reproduce the above michael@0: # copyright notice, this list of conditions and the following disclaimer michael@0: # in the documentation and/or other materials provided with the michael@0: # distribution. michael@0: # * Neither the name of Google Inc. nor the names of its michael@0: # contributors may be used to endorse or promote products derived from michael@0: # this software without specific prior written permission. michael@0: # michael@0: # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: michael@0: """Memorizing file. michael@0: michael@0: A memorizing file wraps a file and memorizes lines read by readline. michael@0: """ michael@0: michael@0: michael@0: import sys michael@0: michael@0: michael@0: class MemorizingFile(object): michael@0: """MemorizingFile wraps a file and memorizes lines read by readline. michael@0: michael@0: Note that data read by other methods are not memorized. This behavior michael@0: is good enough for memorizing lines SimpleHTTPServer reads before michael@0: the control reaches WebSocketRequestHandler. michael@0: """ michael@0: michael@0: def __init__(self, file_, max_memorized_lines=sys.maxint): michael@0: """Construct an instance. michael@0: michael@0: Args: michael@0: file_: the file object to wrap. michael@0: max_memorized_lines: the maximum number of lines to memorize. michael@0: Only the first max_memorized_lines are memorized. michael@0: Default: sys.maxint. michael@0: """ michael@0: michael@0: self._file = file_ michael@0: self._memorized_lines = [] michael@0: self._max_memorized_lines = max_memorized_lines michael@0: self._buffered = False michael@0: self._buffered_line = None michael@0: michael@0: def __getattribute__(self, name): michael@0: if name in ('_file', '_memorized_lines', '_max_memorized_lines', michael@0: '_buffered', '_buffered_line', 'readline', michael@0: 'get_memorized_lines'): michael@0: return object.__getattribute__(self, name) michael@0: return self._file.__getattribute__(name) michael@0: michael@0: def readline(self, size=-1): michael@0: """Override file.readline and memorize the line read. michael@0: michael@0: Note that even if size is specified and smaller than actual size, michael@0: the whole line will be read out from underlying file object by michael@0: subsequent readline calls. michael@0: """ michael@0: michael@0: if self._buffered: michael@0: line = self._buffered_line michael@0: self._buffered = False michael@0: else: michael@0: line = self._file.readline() michael@0: if line and len(self._memorized_lines) < self._max_memorized_lines: michael@0: self._memorized_lines.append(line) michael@0: if size >= 0 and size < len(line): michael@0: self._buffered = True michael@0: self._buffered_line = line[size:] michael@0: return line[:size] michael@0: return line michael@0: michael@0: def get_memorized_lines(self): michael@0: """Get lines memorized so far.""" michael@0: return self._memorized_lines michael@0: michael@0: michael@0: # vi:sts=4 sw=4 et