<s> import os <EOL> import mimetypes <EOL> from libcloud . utils . py3 import PY3 <EOL> from libcloud . utils . py3 import httplib <EOL> from libcloud . utils . py3 import next <EOL> from libcloud . utils . py3 import b <EOL> if PY3 : <EOL> from io import FileIO as file <EOL> CHUNK_SIZE = <NUM_LIT> <EOL> __all__ = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> def read_in_chunks ( iterator , chunk_size = None , fill_size = False , <EOL> yield_empty = False ) : <EOL> """<STR_LIT>""" <EOL> chunk_size = chunk_size or CHUNK_SIZE <EOL> if isinstance ( iterator , ( file , httplib . HTTPResponse ) ) : <EOL> get_data = iterator . read <EOL> args = ( chunk_size , ) <EOL> else : <EOL> get_data = next <EOL> args = ( iterator , ) <EOL> data = b ( '<STR_LIT>' ) <EOL> empty = False <EOL> while not empty or len ( data ) > <NUM_LIT:0> : <EOL> if not empty : <EOL> try : <EOL> chunk = b ( get_data ( * args ) ) <EOL> if len ( chunk ) > <NUM_LIT:0> : <EOL> data += chunk <EOL> else : <EOL> empty = True <EOL> except StopIteration : <EOL> empty = True <EOL> if len ( data ) == <NUM_LIT:0> : <EOL> if empty and yield_empty : <EOL> yield b ( '<STR_LIT>' ) <EOL> raise StopIteration <EOL> if fill_size : <EOL> if empty or len ( data ) >= chunk_size : <EOL> yield data [ : chunk_size ] <EOL> data = data [ chunk_size : ] <EOL> else : <EOL> yield data <EOL> data = b ( '<STR_LIT>' ) <EOL> def exhaust_iterator ( iterator ) : <EOL> """<STR_LIT>""" <EOL> data = b ( '<STR_LIT>' ) <EOL> try : <EOL> chunk = b ( next ( iterator ) ) <EOL> except StopIteration : <EOL> chunk = b ( '<STR_LIT>' ) <EOL> while len ( chunk ) > <NUM_LIT:0> : <EOL> data += chunk <EOL> try : <EOL> chunk = b ( next ( iterator ) ) <EOL> except StopIteration : <EOL> chunk = b ( '<STR_LIT>' ) <EOL> return data <EOL> def guess_file_mime_type ( file_path ) : <EOL> filename = os . path . basename ( file_path ) <EOL> ( mimetype , encoding ) = mimetypes . guess_type ( filename ) <EOL> return mimetype , encoding </s>
