³ò ÷$•Jc@sšddkZddkZddkZddkZddkZyddklZWn%ej oZ ddklZnXde fd„ƒYZ dei fd„ƒYZ ddd„ƒYZd eifd „ƒYZdd klZlZlZlZlZlZlZddkZddkZddkZddkZddkZd 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(R R 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 „Z RS( 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(R Rtrangetup((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(R tname((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(R toffsettwhencet 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(R tsizetrslt((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)(R R*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(R R*((s&/sys/lib/python/mercurial/byterange.pyR'¡s   cCsb|djpt‚t|idƒp|i|ƒn|ii|i|ƒ|i|7_dS(sSeek based on whether wrapped object supports seek(). offset is relative to the current position (self.realpos). iR&N(R!RRt_poor_mans_seekR&R(R R#((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(R R#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| ƒ} | d jpt‚| 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(R R R5R6thosttfilet localfiletstatsR*tmodifiedtmtypetportRtbrangetfbtlbRH((s&/sys/lib/python/mercurial/byterange.pytopen_local_fileÑs:     #    ' (RRRRW(((s&/sys/lib/python/mercurial/byterange.pyR1Ìs(RAt splitusert splitpasswdt splitattrtunquotet addclosehookRtFTPRangeHandlercBseZd„Zd„ZRS(cCsÈ|iƒ}|ptddƒ‚nt|ƒ\}}|djo ti}nt|ƒ\}}|ot|ƒ\}}nd}t|ƒ}t|pdƒ}t|pdƒ}yt i |ƒ}Wn't i j 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ÿ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[RBRCterrorRERFRZR8tsplittmapt connect_ftptlowertupperRKRHRIR!tretrfileRRR5R@RR6RLRt all_errorstsystexc_info(R R RMRStusertpasswdR tpathtattrstdirsRNtfwRatattrtvaluetrestt range_tupRURVR tretrlenRHRR((s&/sys/lib/python/mercurial/byterange.pytftp_opensz            cCst|||||ƒ}|S(N(t ftpwrapper(R RrRsRMRSRvRw((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||d fƒ}|| 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`(t endtransfertftptvoidcmdRfRotinitRJtnlstt error_permR"RpRqt ntransfercmdtstrt startswithRnRtbusyR\tmakefile( R RNRaRztcmdtisdirtconntreasonR 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=-". 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=-" or None if no range is needed. iis bytes=%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;RREt email.UtilsR6t cStringIORt ImportErrorR R"Rt BaseHandlerRRt FileHandlerR1RARXRYRZR[R\RRfRBRpR5t FTPHandlerR]R~RJR‘RKR˜R(((s&/sys/lib/python/mercurial/byterange.pyss0     #„,4     Q: