<s> import codecs <EOL> import mimetypes <EOL> from uuid import uuid4 <EOL> from io import BytesIO <EOL> from . packages import six <EOL> from . packages . six import b <EOL> writer = codecs . lookup ( '<STR_LIT:utf-8>' ) [ <NUM_LIT:3> ] <EOL> def choose_boundary ( ) : <EOL> """<STR_LIT>""" <EOL> return uuid4 ( ) . hex <EOL> def get_content_type ( filename ) : <EOL> return mimetypes . guess_type ( filename ) [ <NUM_LIT:0> ] or '<STR_LIT>' <EOL> def iter_fields ( fields ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( fields , dict ) : <EOL> return ( ( k , v ) for k , v in six . iteritems ( fields ) ) <EOL> return ( ( k , v ) for k , v in fields ) <EOL> def encode_multipart_formdata ( fields , boundary = None ) : <EOL> """<STR_LIT>""" <EOL> body = BytesIO ( ) <EOL> if boundary is None : <EOL> boundary = choose_boundary ( ) <EOL> for fieldname , value in iter_fields ( fields ) : <EOL> body . write ( b ( '<STR_LIT>' % ( boundary ) ) ) <EOL> if isinstance ( value , tuple ) : <EOL> if len ( value ) == <NUM_LIT:3> : <EOL> filename , data , content_type = value <EOL> else : <EOL> filename , data = value <EOL> content_type = get_content_type ( filename ) <EOL> writer ( body ) . write ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( fieldname , filename ) ) <EOL> body . write ( b ( '<STR_LIT>' % <EOL> ( content_type , ) ) ) <EOL> else : <EOL> data = value <EOL> writer ( body ) . write ( '<STR_LIT>' <EOL> % ( fieldname ) ) <EOL> body . write ( b'<STR_LIT:\r\n>' ) <EOL> if isinstance ( data , int ) : <EOL> data = str ( data ) <EOL> if isinstance ( data , six . text_type ) : <EOL> writer ( body ) . write ( data ) <EOL> else : <EOL> body . write ( data ) <EOL> body . write ( b'<STR_LIT:\r\n>' ) <EOL> body . write ( b ( '<STR_LIT>' % ( boundary ) ) ) <EOL> content_type = str ( '<STR_LIT>' % boundary ) <EOL> return body . getvalue ( ) , content_type </s>
