<s> import re <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> _rx_range = re . compile ( '<STR_LIT>' , flags = re . I ) <EOL> _rx_content_range = re . compile ( r'<STR_LIT>' ) <EOL> class Range ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , start , end ) : <EOL> assert end is None or end >= <NUM_LIT:0> , "<STR_LIT>" % end <EOL> self . start = start <EOL> self . end = end <EOL> def range_for_length ( self , length ) : <EOL> """<STR_LIT>""" <EOL> if length is None : <EOL> return None <EOL> start , end = self . start , self . end <EOL> if end is None : <EOL> end = length <EOL> if start < <NUM_LIT:0> : <EOL> start += length <EOL> if _is_content_range_valid ( start , end , length ) : <EOL> stop = min ( end , length ) <EOL> return ( start , stop ) <EOL> else : <EOL> return None <EOL> def content_range ( self , length ) : <EOL> """<STR_LIT>""" <EOL> range = self . range_for_length ( length ) <EOL> if range is None : <EOL> return None <EOL> return ContentRange ( range [ <NUM_LIT:0> ] , range [ <NUM_LIT:1> ] , length ) <EOL> def __str__ ( self ) : <EOL> s , e = self . start , self . end <EOL> if e is None : <EOL> r = '<STR_LIT>' % s <EOL> if s >= <NUM_LIT:0> : <EOL> r += '<STR_LIT:->' <EOL> return r <EOL> return '<STR_LIT>' % ( s , e - <NUM_LIT:1> ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( <EOL> self . __class__ . __name__ , <EOL> self . start , self . end ) <EOL> def __iter__ ( self ) : <EOL> return iter ( ( self . start , self . end ) ) <EOL> @ classmethod <EOL> def parse ( cls , header ) : <EOL> """<STR_LIT>""" <EOL> m = _rx_range . match ( header or '<STR_LIT>' ) <EOL> if not m : <EOL> return None <EOL> start , end = m . groups ( ) <EOL> if not start : <EOL> return cls ( - int ( end ) , None ) <EOL> start = int ( start ) <EOL> if not end : <EOL> return cls ( start , None ) <EOL> end = int ( end ) + <NUM_LIT:1> <EOL> if start >= end : <EOL> return None <EOL> return cls ( start , end ) <EOL> class ContentRange ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , start , stop , length ) : <EOL> if not _is_content_range_valid ( start , stop , length ) : <EOL> raise ValueError ( <EOL> "<STR_LIT>" % ( start , stop , length ) ) <EOL> self . start = start <EOL> self . stop = stop <EOL> self . length = length <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , self ) <EOL> def __str__ ( self ) : <EOL> if self . length is None : <EOL> length = '<STR_LIT:*>' <EOL> else : <EOL> length = self . length <EOL> if self . start is None : <EOL> assert self . stop is None <EOL> return '<STR_LIT>' % length <EOL> stop = self . stop - <NUM_LIT:1> <EOL> return '<STR_LIT>' % ( self . start , stop , length ) <EOL> def __iter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return iter ( [ self . start , self . stop , self . length ] ) <EOL> @ classmethod <EOL> def parse ( cls , value ) : <EOL> """<STR_LIT>""" <EOL> m = _rx_content_range . match ( value or '<STR_LIT>' ) <EOL> if not m : <EOL> return None <EOL> s , e , l = m . groups ( ) <EOL> if s : <EOL> s = int ( s ) <EOL> e = int ( e ) + <NUM_LIT:1> <EOL> l = l and int ( l ) <EOL> if not _is_content_range_valid ( s , e , l , response = True ) : <EOL> return None <EOL> return cls ( s , e , l ) <EOL> def _is_content_range_valid ( start , stop , length , response = False ) : <EOL> if ( start is None ) != ( stop is None ) : <EOL> return False <EOL> elif start is None : <EOL> return length is None or length >= <NUM_LIT:0> <EOL> elif length is None : <EOL> return <NUM_LIT:0> <= start < stop <EOL> elif start >= stop : <EOL> return False <EOL> elif response and stop > length : <EOL> return False <EOL> else : <EOL> return <NUM_LIT:0> <= start < length </s>
