<s> from __future__ import unicode_literals <EOL> import os <EOL> from io import BytesIO , StringIO , UnsupportedOperation <EOL> from django . core . files . utils import FileProxyMixin <EOL> from django . utils import six <EOL> from django . utils . encoding import ( <EOL> force_bytes , force_str , python_2_unicode_compatible , smart_text , <EOL> ) <EOL> @ python_2_unicode_compatible <EOL> class File ( FileProxyMixin ) : <EOL> DEFAULT_CHUNK_SIZE = <NUM_LIT:64> * <NUM_LIT:2> ** <NUM_LIT:10> <EOL> def __init__ ( self , file , name = None ) : <EOL> self . file = file <EOL> if name is None : <EOL> name = getattr ( file , '<STR_LIT:name>' , None ) <EOL> self . name = name <EOL> if hasattr ( file , '<STR_LIT>' ) : <EOL> self . mode = file . mode <EOL> def __str__ ( self ) : <EOL> return smart_text ( self . name or '<STR_LIT>' ) <EOL> def __repr__ ( self ) : <EOL> return force_str ( "<STR_LIT>" % ( self . __class__ . __name__ , self or "<STR_LIT:None>" ) ) <EOL> def __bool__ ( self ) : <EOL> return bool ( self . name ) <EOL> def __nonzero__ ( self ) : <EOL> return type ( self ) . __bool__ ( self ) <EOL> def __len__ ( self ) : <EOL> return self . size <EOL> def _get_size_from_underlying_file ( self ) : <EOL> if hasattr ( self . file , '<STR_LIT:size>' ) : <EOL> return self . file . size <EOL> if hasattr ( self . file , '<STR_LIT:name>' ) : <EOL> try : <EOL> return os . path . getsize ( self . file . name ) <EOL> except ( OSError , TypeError ) : <EOL> pass <EOL> if hasattr ( self . file , '<STR_LIT>' ) and hasattr ( self . file , '<STR_LIT>' ) : <EOL> pos = self . file . tell ( ) <EOL> self . file . seek ( <NUM_LIT:0> , os . SEEK_END ) <EOL> size = self . file . tell ( ) <EOL> self . file . seek ( pos ) <EOL> return size <EOL> raise AttributeError ( "<STR_LIT>" ) <EOL> def _get_size ( self ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> return self . _size <EOL> self . _size = self . _get_size_from_underlying_file ( ) <EOL> return self . _size <EOL> def _set_size ( self , size ) : <EOL> self . _size = size <EOL> size = property ( _get_size , _set_size ) <EOL> def _get_closed ( self ) : <EOL> return not self . file or self . file . closed <EOL> closed = property ( _get_closed ) <EOL> def chunks ( self , chunk_size = None ) : <EOL> """<STR_LIT>""" <EOL> if not chunk_size : <EOL> chunk_size = self . DEFAULT_CHUNK_SIZE <EOL> try : <EOL> self . seek ( <NUM_LIT:0> ) <EOL> except ( AttributeError , UnsupportedOperation ) : <EOL> pass <EOL> while True : <EOL> data = self . read ( chunk_size ) <EOL> if not data : <EOL> break <EOL> yield data <EOL> def multiple_chunks ( self , chunk_size = None ) : <EOL> """<STR_LIT>""" <EOL> if not chunk_size : <EOL> chunk_size = self . DEFAULT_CHUNK_SIZE <EOL> return self . size > chunk_size <EOL> def __iter__ ( self ) : <EOL> buffer_ = None <EOL> for chunk in self . chunks ( ) : <EOL> for line in chunk . splitlines ( True ) : <EOL> if buffer_ : <EOL> if endswith_cr ( buffer_ ) and not equals_lf ( line ) : <EOL> yield buffer_ <EOL> else : <EOL> line = buffer_ + line <EOL> buffer_ = None <EOL> if endswith_lf ( line ) : <EOL> yield line <EOL> else : <EOL> buffer_ = line <EOL> if buffer_ is not None : <EOL> yield buffer_ <EOL> def __enter__ ( self ) : <EOL> return self <EOL> def __exit__ ( self , exc_type , exc_value , tb ) : <EOL> self . close ( ) <EOL> def open ( self , mode = None ) : <EOL> if not self . closed : <EOL> self . seek ( <NUM_LIT:0> ) <EOL> elif self . name and os . path . exists ( self . name ) : <EOL> self . file = open ( self . name , mode or self . mode ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def close ( self ) : <EOL> self . file . close ( ) <EOL> @ python_2_unicode_compatible <EOL> class ContentFile ( File ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , content , name = None ) : <EOL> if six . PY3 : <EOL> stream_class = StringIO if isinstance ( content , six . text_type ) else BytesIO <EOL> else : <EOL> stream_class = BytesIO <EOL> content = force_bytes ( content ) <EOL> super ( ContentFile , self ) . __init__ ( stream_class ( content ) , name = name ) <EOL> self . size = len ( content ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' <EOL> def __bool__ ( self ) : <EOL> return True <EOL> def __nonzero__ ( self ) : <EOL> return type ( self ) . __bool__ ( self ) <EOL> def open ( self , mode = None ) : <EOL> self . seek ( <NUM_LIT:0> ) <EOL> def close ( self ) : <EOL> pass <EOL> def endswith_cr ( line ) : <EOL> """<STR_LIT>""" <EOL> return line . endswith ( '<STR_LIT:\r>' if isinstance ( line , six . text_type ) else b'<STR_LIT:\r>' ) <EOL> def endswith_lf ( line ) : <EOL> """<STR_LIT>""" <EOL> return line . endswith ( '<STR_LIT:\n>' if isinstance ( line , six . text_type ) else b'<STR_LIT:\n>' ) <EOL> def equals_lf ( line ) : <EOL> """<STR_LIT>""" <EOL> return line == ( '<STR_LIT:\n>' if isinstance ( line , six . text_type ) else b'<STR_LIT:\n>' ) </s>
