mercurial/httprangereader.py
author Matt Mackall <mpm@selenic.com>
Fri, 01 Dec 2006 01:28:19 -0600
changeset 3738 cb48cd27d3f4
parent 2859 345bac2bc4ec
permissions -rw-r--r--
use ui buffering in changeset printer delete old uibuffer class delete old stringio class move show to _show add new show that uses ui.pushbuffer to buffer add new flush that writes buffered data add props arg to changeset_printer to match _templater

# httprangereader.py - just what it says
#
# Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
#
# This software may be used and distributed according to the terms
# of the GNU General Public License, incorporated herein by reference.

import byterange, urllib2

class httprangereader(object):
    def __init__(self, url):
        self.url = url
        self.pos = 0
    def seek(self, pos):
        self.pos = pos
    def read(self, bytes=None):
        opener = urllib2.build_opener(byterange.HTTPRangeHandler())
        urllib2.install_opener(opener)
        req = urllib2.Request(self.url)
        end = ''
        if bytes:
            end = self.pos + bytes - 1
        req.add_header('Range', 'bytes=%d-%s' % (self.pos, end))
        f = urllib2.urlopen(req)
        data = f.read()
        if bytes:
            data = data[:bytes]
        return data