Truncated stream. Takes a stream and presents a stream that is a slice of the original stream. This is used when uploading media in chunks. In later versions of Python a stream can be passed to httplib in place of the string of data to send. The problem is that httplib just blindly
| 781 | |
| 782 | |
| 783 | class _StreamSlice(object): |
| 784 | """Truncated stream. |
| 785 | |
| 786 | Takes a stream and presents a stream that is a slice of the original stream. |
| 787 | This is used when uploading media in chunks. In later versions of Python a |
| 788 | stream can be passed to httplib in place of the string of data to send. The |
| 789 | problem is that httplib just blindly reads to the end of the stream. This |
| 790 | wrapper presents a virtual stream that only reads to the end of the chunk. |
| 791 | """ |
| 792 | |
| 793 | def __init__(self, stream, begin, chunksize): |
| 794 | """Constructor. |
| 795 | |
| 796 | Args: |
| 797 | stream: (io.Base, file object), the stream to wrap. |
| 798 | begin: int, the seek position the chunk begins at. |
| 799 | chunksize: int, the size of the chunk. |
| 800 | """ |
| 801 | self._stream = stream |
| 802 | self._begin = begin |
| 803 | self._chunksize = chunksize |
| 804 | self._stream.seek(begin) |
| 805 | |
| 806 | def read(self, n=-1): |
| 807 | """Read n bytes. |
| 808 | |
| 809 | Args: |
| 810 | n, int, the number of bytes to read. |
| 811 | |
| 812 | Returns: |
| 813 | A string of length 'n', or less if EOF is reached. |
| 814 | """ |
| 815 | # The data left available to read sits in [cur, end) |
| 816 | cur = self._stream.tell() |
| 817 | end = self._begin + self._chunksize |
| 818 | if n == -1 or cur + n > end: |
| 819 | n = end - cur |
| 820 | return self._stream.read(n) |
| 821 | |
| 822 | |
| 823 | class HttpRequest(object): |
no outgoing calls
searching dependent graphs…