Plan 9 from Bell Labs’s /usr/web/sources/contrib/bichued/root/sys/lib/python/mercurial/byterange.pyc

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


�
�c@s�ddkZddkZddkZddkZddkZyddklZWn%ej
oZ	ddklZnXde
fd��YZdeifd��YZ
ddd��YZd	eifd
��YZddklZlZlZlZlZlZlZddkZddkZddkZddkZddkZdeifd
��YZdeifd��YZda d�Z!d�Z"d�Z#dS(i�N(tStringIOt
RangeErrorcBseZdZRS(s6Error raised when an unsatisfiable range is requested.(t__name__t
__module__t__doc__(((s&/sys/lib/python/mercurial/byterange.pyR!stHTTPRangeHandlercBs eZdZd�Zd�ZRS(s�Handler that enables HTTP Range headers.

    This was extremely simple. The Range header is a HTTP feature to
    begin with so all this class does is tell urllib2 that the
    "206 Partial Content" reponse from the HTTP server is what we
    expected.

    Example:
        import urllib2
        import byterange

        range_handler = range.HTTPRangeHandler()
        opener = urllib2.build_opener(range_handler)

        # install it
        urllib2.install_opener(opener)

        # create Request and set Range header
        req = urllib2.Request('http://www.python.org/')
        req.header['Range'] = 'bytes=30-50'
        f = urllib2.urlopen(req)
    cCs1ti|||i��}||_||_|S(N(turllibt
addinfourltget_full_urltcodetmsg(tselftreqtfpR	R
thdrstr((s&/sys/lib/python/mercurial/byterange.pythttp_error_206=s		cCstd��dS(NsRequested Range Not Satisfiable(R(RRR
R	R
R((s&/sys/lib/python/mercurial/byterange.pythttp_error_416Ds(RRRRR(((s&/sys/lib/python/mercurial/byterange.pyR%s	tRangeableFileObjectcBsheZdZd�Zd�Zd�Zdd�Zdd�Zdd�Zd	�Z	d
�Z
d�ZRS(sFile object wrapper to enable raw range handling.
    This was implemented primarilary for handling range
    specifications for file:// urls. This object effectively makes
    a file object look like it consists only of a range of bytes in
    the stream.

    Examples:
        # expose 10 bytes, starting at byte position 20, from
        # /etc/aliases.
        >>> fo = RangeableFileObject(file('/etc/passwd', 'r'), (20,30))
        # seek seeks within the range (to position 23 in this case)
        >>> fo.seek(3)
        # tell tells where your at _within the range_ (position 3 in
        # this case)
        >>> fo.tell()
        # read EOFs if an attempt is made to read past the last
        # byte in the range. the following will return only 7 bytes.
        >>> fo.read(30)
    cCs>||_t|�\|_|_d|_|i|i�dS(s}Create a RangeableFileObject.
        fo       -- a file like object. only the read() method need be
                    supported but supporting an optimized seek() is
                    preferable.
        rangetup -- a (firstbyte,lastbyte) tuple specifying the range
                    to work over.
        The file object provided is assumed to be at byte offset 0.
        iN(tfotrange_tuple_normalizet	firstbytetlastbytetrealpost_do_seek(RRtrangetup((s&/sys/lib/python/mercurial/byterange.pyt__init__]s			cCs7t|i|�ot|i|�Snt|��dS(s�This effectively allows us to wrap at the instance level.
        Any attribute not found in _this_ object will be searched for
        in self.fo.  This includes methods.N(thasattrRtgetattrtAttributeError(Rtname((s&/sys/lib/python/mercurial/byterange.pyt__getattr__kscCs|i|iS(s4Return the position within the range.
        This is different from fo.seek in that position 0 is the
        first byte position of the range tuple. For example, if
        this object was created with a range tuple of (500,899),
        tell() will return 0 when at byte position 500 of the file.
        (RR(R((s&/sys/lib/python/mercurial/byterange.pyttellssicCs�|djpt�|djo|i|}n<|djo|i|}n|djotd��n|io||ijo
|i}n|i||i�dS(seSeek within the byte range.
        Positioning is identical to that described under tell().
        iiis$seek from end of file not supported.N(iii(tAssertionErrorRRtIOErrorRR(Rtoffsettwhencet
realoffset((s&/sys/lib/python/mercurial/byterange.pytseek|s



i�cCs:|i|�}|ii|�}|it|�7_|S(s`Read within the range.
        This method will limit the size read based on the range.
        (t_calc_read_sizeRtreadRtlen(Rtsizetrslt((s&/sys/lib/python/mercurial/byterange.pyR(�scCs:|i|�}|ii|�}|it|�7_|S(sfRead lines within the range.
        This method will limit the size read based on the range.
        (R'RtreadlineRR)(RR*R+((s&/sys/lib/python/mercurial/byterange.pyR,�scCs^|ioP|djo/|i||ijo|i|i}qVqZ|i|i}n|S(sSHandles calculating the amount of data to read based on
        the range.
        i�(RR(RR*((s&/sys/lib/python/mercurial/byterange.pyR'�s

cCsb|djpt�t|id�p|i|�n|ii|i|�|i|7_dS(s�Seek based on whether wrapped object supports seek().
        offset is relative to the current position (self.realpos).
        iR&N(R!RRt_poor_mans_seekR&R(RR#((s&/sys/lib/python/mercurial/byterange.pyR�s
cCs�d}d}xp||job|||jo||}n|ii|�}t|�|jotd��n||7}qWdS(s�Seek by calling the wrapped file objects read() method.
        This is used for file like objects that do not have native
        seek support. The wrapped objects read() method is called
        to manually seek to the desired position.
        offset -- read this number of bytes from the wrapped
                  file object.
        raise RangeError if we encounter EOF before reaching the
        specified offset.
        iisRequested Range Not SatisfiableN(RR(R)R(RR#tpostbufsizetbuf((s&/sys/lib/python/mercurial/byterange.pyR-�s
(RRRRRR R&R(R,R'RR-(((s&/sys/lib/python/mercurial/byterange.pyRHs								tFileRangeHandlercBseZdZd�ZRS(s~FileHandler subclass that adds Range support.
    This class handles Range headers exactly like an HTTP
    server would.
    cCs�ddk}ddk}|i�}|i�}ti|�}ti|�}|ti}|i	i
|ti�}	|i|�d}
|oOti
|�\}}|pti|�|i�jotid��q�nt|d�}|iidd�}
t|
�}
|
djpt�|
o}|
\}}|djo
|}n|djp||jp
||jotd��n||}t|||f�}n|id|
pd	||	f�}ti||d
|�S(Ni�isfile not on local hosttrbtRangetsRequested Range Not Satisfiables6Content-Type: %s
Content-Length: %d
Last-Modified: %s
s
text/plainsfile:((t	mimetypestemailtget_hosttget_selectorRturl2pathnametoststattST_SIZEtUtilst
formatdatetST_MTIMEt
guess_typet	splitporttsockett
gethostbynamet	get_namesturllib2tURLErrortopentheaderstgettNonetrange_header_to_tupleR!RRtmessage_from_stringR(RRR5R6thosttfilet	localfiletstatsR*tmodifiedtmtypetportRtbrangetfbtlbRH((s&/sys/lib/python/mercurial/byterange.pytopen_local_file�s:
#

'
(RRRRW(((s&/sys/lib/python/mercurial/byterange.pyR1�s(RAt	splitusertsplitpasswdt	splitattrtunquotetaddclosehookRtFTPRangeHandlercBseZd�Zd�ZRS(cCs�|i�}|ptdd��nt|�\}}|djo
ti}nt|�\}}|ot|�\}}nd}t|�}t|pd�}t|pd�}yt	i
|�}Wn't	ij
o}ti
|��nXt|i��\}}|id�}	tt|	�}	|	d |	d}	}
|	o|	do|	d}	ny+|i|||||	�}|
odpd	}xP|D]H}
t|
�\}
}|
i�d
jo|djo|i�}q�q�Wd}t|iidd��}|djpt�|o'|\}}|djo
|}qJn|i|
||�\}}|o�|\}}|djo[|djp
|djotd��n|}||}|djotd��q�||}t|d|f�}nd}ti|i��d}|o|d|7}n|dj	o|djo|d|7}nti |�}t!|||i��SWn4ti"j
o%}td|�t#i$�d�nXdS(Ns	ftp errors
no host givenR4t/i�iitItDttypetatAtitdR3s@Requested Range Not Satisfiable due to unobtainable file length.sRequested Range Not SatisfiablesContent-Type: %s
sContent-Length: %d
i(RbRcRdR_ReR`((%R7R"RARJtftplibtFTP_PORTRXRYR[RBRCterrorRERFRZR8tsplittmaptconnect_ftptlowertupperRKRHRIR!tretrfileRRR5R@RR6RLRt
all_errorstsystexc_info(RRRMRStusertpasswdR
tpathtattrstdirsRNtfwRatattrtvaluetrestt	range_tupRURVR
tretrlenRHRR((s&/sys/lib/python/mercurial/byterange.pytftp_opensz







cCst|||||�}|S(N(t
ftpwrapper(RRrRsRMRSRvRw((s&/sys/lib/python/mercurial/byterange.pyRkMs(RRR}Rk(((s&/sys/lib/python/mercurial/byterange.pyR]s	LR~cBseZdd�ZRS(c
Cs/|i�|djod}d}nd|}d}y|ii|�Wn0tij
o!|i�|ii|�nXd}|o,|o$y|ii|�Wn4tij
o%}t	d|�t
i�d�nX|ii|�y#d	|}|ii||�}Wq�tij
o�}t
|�id
�o;|i||�\}}	t||df�}||	fSq�t
|�id�p t	d|�t
i�d�q�q�Xn|pA|iid�|od
|}nd}|ii|�}nd|_t|did�|i�|dfS(NReR`sTYPE AisTYPE is	ftp errorisRETR t501R4t550sLIST tLISTR2(ReR`(tendtransfertftptvoidcmdRfRotinitRJtnlstt
error_permR"RpRqtntransfercmdtstrt
startswithRnRtbusyR\tmakefile(
RRNRaRztcmdtisdirtconntreasonR
R|((s&/sys/lib/python/mercurial/byterange.pyRnVsJ




!
)	N(RRRJRn(((s&/sys/lib/python/mercurial/byterange.pyR~QscCs�|djodSntdjoddk}|id�anti|�}|oNt|idd��}|o'|do|d|ddf}n|SndS(s�Get a (firstbyte,lastbyte) tuple from a Range header value.

    Range headers have the form "bytes=<firstbyte>-<lastbyte>". This
    function pulls the firstbyte and lastbyte values and returns
    a (firstbyte,lastbyte) tuple. If lastbyte is not specified in
    the header value, it is returned as an empty string in the
    tuple.

    Return None if range_header is None
    Return () if range_header does not conform to the range spec
    pattern.

    i�Ns^bytes=(\d{1,})-(\d*)iii((RJt_rangeretretcompiletmatchRtgroup(trange_headerR�R�ttup((s&/sys/lib/python/mercurial/byterange.pyRK�s

cCs_|djodSnt|�}|o3|do|d|ddf}nd|SndS(s�Convert a range tuple to a Range header value.
    Return a string of the form "bytes=<firstbyte>-<lastbyte>" or None
    if no range is needed.
    iisbytes=%s-%sN(RJR(R{((s&/sys/lib/python/mercurial/byterange.pytrange_tuple_to_header�s
cCs�|djodSn|d}|djo
d}n
t|�}y|d}Wntj
o
d}n6X|djo
d}n|djot|�}n||fdjodSn||jotd||f��n||fS(s6Normalize a (first_byte,last_byte) range tuple.
    Return a tuple whose first element is guaranteed to be an int
    and whose second element will be '' (meaning: the last byte) or
    an int. Finally, return None if the normalized tuple == (0,'')
    as that is equivelant to retrieving the entire file.
    iR4isInvalid byte range: %s-%sN(NR4(iR4(RJtintt
IndexErrorR(R{RURV((s&/sys/lib/python/mercurial/byterange.pyR�s&







(($R:R;RREtemail.UtilsR6t	cStringIORtImportErrorR
R"RtBaseHandlerRRtFileHandlerR1RARXRYRZR[R\RRfRBRpR5t
FTPHandlerR]R~RJR�RKR�R(((s&/sys/lib/python/mercurial/byterange.pys<module>s0#�,4Q:		

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.