<s> import os <EOL> from django . utils . encoding import smart_str , smart_unicode <EOL> try : <EOL> from cStringIO import StringIO <EOL> except ImportError : <EOL> from StringIO import StringIO <EOL> class File ( object ) : <EOL> DEFAULT_CHUNK_SIZE = <NUM_LIT:64> * <NUM_LIT:2> ** <NUM_LIT:10> <EOL> def __init__ ( self , file ) : <EOL> self . file = file <EOL> self . _name = file . name <EOL> self . _mode = file . mode <EOL> self . _closed = False <EOL> def __str__ ( self ) : <EOL> return smart_str ( self . name or '<STR_LIT>' ) <EOL> def __unicode__ ( self ) : <EOL> return smart_unicode ( self . name or u'<STR_LIT>' ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , self or "<STR_LIT:None>" ) <EOL> def __nonzero__ ( self ) : <EOL> return not not self . name <EOL> def __len__ ( self ) : <EOL> return self . size <EOL> def _get_name ( self ) : <EOL> return self . _name <EOL> name = property ( _get_name ) <EOL> def _get_mode ( self ) : <EOL> return self . _mode <EOL> mode = property ( _get_mode ) <EOL> def _get_closed ( self ) : <EOL> return self . _closed <EOL> closed = property ( _get_closed ) <EOL> def _get_size ( self ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> if hasattr ( self . file , '<STR_LIT:size>' ) : <EOL> self . _size = self . file . size <EOL> elif os . path . exists ( self . file . name ) : <EOL> self . _size = os . path . getsize ( self . file . name ) <EOL> else : <EOL> raise AttributeError ( "<STR_LIT>" ) <EOL> return self . _size <EOL> def _set_size ( self , size ) : <EOL> self . _size = size <EOL> size = property ( _get_size , _set_size ) <EOL> def chunks ( self , chunk_size = None ) : <EOL> """<STR_LIT>""" <EOL> if not chunk_size : <EOL> chunk_size = self . __class__ . DEFAULT_CHUNK_SIZE <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> self . seek ( <NUM_LIT:0> ) <EOL> counter = self . size <EOL> while counter > <NUM_LIT:0> : <EOL> yield self . read ( chunk_size ) <EOL> counter -= chunk_size <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 xreadlines ( self ) : <EOL> return iter ( self ) <EOL> def readlines ( self ) : <EOL> return list ( self . xreadlines ( ) ) <EOL> def __iter__ ( self ) : <EOL> buffer_ = None <EOL> for chunk in self . chunks ( ) : <EOL> chunk_buffer = StringIO ( chunk ) <EOL> for line in chunk_buffer : <EOL> if buffer_ : <EOL> line = buffer_ + line <EOL> buffer_ = None <EOL> if line [ - <NUM_LIT:1> ] in ( '<STR_LIT:\n>' , '<STR_LIT:\r>' ) : <EOL> yield line <EOL> else : <EOL> buffer_ = line <EOL> if buffer_ is not None : <EOL> yield buffer_ <EOL> def open ( self , mode = None ) : <EOL> if not self . closed : <EOL> self . seek ( <NUM_LIT:0> ) <EOL> elif os . path . exists ( self . file . name ) : <EOL> self . file = open ( self . file . name , mode or self . file . mode ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def seek ( self , position ) : <EOL> self . file . seek ( position ) <EOL> def tell ( self ) : <EOL> return self . file . tell ( ) <EOL> def read ( self , num_bytes = None ) : <EOL> if num_bytes is None : <EOL> return self . file . read ( ) <EOL> return self . file . read ( num_bytes ) <EOL> def write ( self , content ) : <EOL> if not self . mode . startswith ( '<STR_LIT:w>' ) : <EOL> raise IOError ( "<STR_LIT>" ) <EOL> self . file . write ( content ) <EOL> def flush ( self ) : <EOL> if not self . mode . startswith ( '<STR_LIT:w>' ) : <EOL> raise IOError ( "<STR_LIT>" ) <EOL> self . file . flush ( ) <EOL> def close ( self ) : <EOL> self . file . close ( ) <EOL> self . _closed = True <EOL> class ContentFile ( File ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , content ) : <EOL> self . file = StringIO ( content or '<STR_LIT>' ) <EOL> self . size = len ( content or '<STR_LIT>' ) <EOL> self . file . seek ( <NUM_LIT:0> ) <EOL> self . _closed = False <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' <EOL> def __nonzero__ ( self ) : <EOL> return True <EOL> def open ( self , mode = None ) : <EOL> if self . _closed : <EOL> self . _closed = False <EOL> self . seek ( <NUM_LIT:0> ) </s>
