Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | #!/usr/bin/env python |
michael@0 | 2 | # |
michael@0 | 3 | # Copyright 2011, Google Inc. |
michael@0 | 4 | # All rights reserved. |
michael@0 | 5 | # |
michael@0 | 6 | # Redistribution and use in source and binary forms, with or without |
michael@0 | 7 | # modification, are permitted provided that the following conditions are |
michael@0 | 8 | # met: |
michael@0 | 9 | # |
michael@0 | 10 | # * Redistributions of source code must retain the above copyright |
michael@0 | 11 | # notice, this list of conditions and the following disclaimer. |
michael@0 | 12 | # * Redistributions in binary form must reproduce the above |
michael@0 | 13 | # copyright notice, this list of conditions and the following disclaimer |
michael@0 | 14 | # in the documentation and/or other materials provided with the |
michael@0 | 15 | # distribution. |
michael@0 | 16 | # * Neither the name of Google Inc. nor the names of its |
michael@0 | 17 | # contributors may be used to endorse or promote products derived from |
michael@0 | 18 | # this software without specific prior written permission. |
michael@0 | 19 | # |
michael@0 | 20 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 21 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 22 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 23 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 24 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 25 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 26 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 27 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 28 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 29 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 30 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 31 | |
michael@0 | 32 | |
michael@0 | 33 | """Memorizing file. |
michael@0 | 34 | |
michael@0 | 35 | A memorizing file wraps a file and memorizes lines read by readline. |
michael@0 | 36 | """ |
michael@0 | 37 | |
michael@0 | 38 | |
michael@0 | 39 | import sys |
michael@0 | 40 | |
michael@0 | 41 | |
michael@0 | 42 | class MemorizingFile(object): |
michael@0 | 43 | """MemorizingFile wraps a file and memorizes lines read by readline. |
michael@0 | 44 | |
michael@0 | 45 | Note that data read by other methods are not memorized. This behavior |
michael@0 | 46 | is good enough for memorizing lines SimpleHTTPServer reads before |
michael@0 | 47 | the control reaches WebSocketRequestHandler. |
michael@0 | 48 | """ |
michael@0 | 49 | |
michael@0 | 50 | def __init__(self, file_, max_memorized_lines=sys.maxint): |
michael@0 | 51 | """Construct an instance. |
michael@0 | 52 | |
michael@0 | 53 | Args: |
michael@0 | 54 | file_: the file object to wrap. |
michael@0 | 55 | max_memorized_lines: the maximum number of lines to memorize. |
michael@0 | 56 | Only the first max_memorized_lines are memorized. |
michael@0 | 57 | Default: sys.maxint. |
michael@0 | 58 | """ |
michael@0 | 59 | |
michael@0 | 60 | self._file = file_ |
michael@0 | 61 | self._memorized_lines = [] |
michael@0 | 62 | self._max_memorized_lines = max_memorized_lines |
michael@0 | 63 | self._buffered = False |
michael@0 | 64 | self._buffered_line = None |
michael@0 | 65 | |
michael@0 | 66 | def __getattribute__(self, name): |
michael@0 | 67 | if name in ('_file', '_memorized_lines', '_max_memorized_lines', |
michael@0 | 68 | '_buffered', '_buffered_line', 'readline', |
michael@0 | 69 | 'get_memorized_lines'): |
michael@0 | 70 | return object.__getattribute__(self, name) |
michael@0 | 71 | return self._file.__getattribute__(name) |
michael@0 | 72 | |
michael@0 | 73 | def readline(self, size=-1): |
michael@0 | 74 | """Override file.readline and memorize the line read. |
michael@0 | 75 | |
michael@0 | 76 | Note that even if size is specified and smaller than actual size, |
michael@0 | 77 | the whole line will be read out from underlying file object by |
michael@0 | 78 | subsequent readline calls. |
michael@0 | 79 | """ |
michael@0 | 80 | |
michael@0 | 81 | if self._buffered: |
michael@0 | 82 | line = self._buffered_line |
michael@0 | 83 | self._buffered = False |
michael@0 | 84 | else: |
michael@0 | 85 | line = self._file.readline() |
michael@0 | 86 | if line and len(self._memorized_lines) < self._max_memorized_lines: |
michael@0 | 87 | self._memorized_lines.append(line) |
michael@0 | 88 | if size >= 0 and size < len(line): |
michael@0 | 89 | self._buffered = True |
michael@0 | 90 | self._buffered_line = line[size:] |
michael@0 | 91 | return line[:size] |
michael@0 | 92 | return line |
michael@0 | 93 | |
michael@0 | 94 | def get_memorized_lines(self): |
michael@0 | 95 | """Get lines memorized so far.""" |
michael@0 | 96 | return self._memorized_lines |
michael@0 | 97 | |
michael@0 | 98 | |
michael@0 | 99 | # vi:sts=4 sw=4 et |