<s> import email . utils <EOL> import mimetypes <EOL> from . packages import six <EOL> def guess_content_type ( filename , default = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if filename : <EOL> return mimetypes . guess_type ( filename ) [ <NUM_LIT:0> ] or default <EOL> return default <EOL> def format_header_param ( name , value ) : <EOL> """<STR_LIT>""" <EOL> if not any ( ch in value for ch in '<STR_LIT>' ) : <EOL> result = '<STR_LIT>' % ( name , value ) <EOL> try : <EOL> result . encode ( '<STR_LIT:ascii>' ) <EOL> except UnicodeEncodeError : <EOL> pass <EOL> else : <EOL> return result <EOL> if not six . PY3 : <EOL> value = value . encode ( '<STR_LIT:utf-8>' ) <EOL> value = email . utils . encode_rfc2231 ( value , '<STR_LIT:utf-8>' ) <EOL> value = '<STR_LIT>' % ( name , value ) <EOL> return value <EOL> class RequestField ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , data , filename = None , headers = None ) : <EOL> self . _name = name <EOL> self . _filename = filename <EOL> self . data = data <EOL> self . headers = { } <EOL> if headers : <EOL> self . headers = dict ( headers ) <EOL> @ classmethod <EOL> def from_tuples ( cls , fieldname , value ) : <EOL> """<STR_LIT>""" <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 = guess_content_type ( filename ) <EOL> else : <EOL> filename = None <EOL> content_type = None <EOL> data = value <EOL> request_param = cls ( fieldname , data , filename = filename ) <EOL> request_param . make_multipart ( content_type = content_type ) <EOL> return request_param <EOL> def _render_part ( self , name , value ) : <EOL> """<STR_LIT>""" <EOL> return format_header_param ( name , value ) <EOL> def _render_parts ( self , header_parts ) : <EOL> """<STR_LIT>""" <EOL> parts = [ ] <EOL> iterable = header_parts <EOL> if isinstance ( header_parts , dict ) : <EOL> iterable = header_parts . items ( ) <EOL> for name , value in iterable : <EOL> if value : <EOL> parts . append ( self . _render_part ( name , value ) ) <EOL> return '<STR_LIT>' . join ( parts ) <EOL> def render_headers ( self ) : <EOL> """<STR_LIT>""" <EOL> lines = [ ] <EOL> sort_keys = [ '<STR_LIT>' , '<STR_LIT:Content-Type>' , '<STR_LIT>' ] <EOL> for sort_key in sort_keys : <EOL> if self . headers . get ( sort_key , False ) : <EOL> lines . append ( '<STR_LIT>' % ( sort_key , self . headers [ sort_key ] ) ) <EOL> for header_name , header_value in self . headers . items ( ) : <EOL> if header_name not in sort_keys : <EOL> if header_value : <EOL> lines . append ( '<STR_LIT>' % ( header_name , header_value ) ) <EOL> lines . append ( '<STR_LIT:\r\n>' ) <EOL> return '<STR_LIT:\r\n>' . join ( lines ) <EOL> def make_multipart ( self , content_disposition = None , content_type = None , <EOL> content_location = None ) : <EOL> """<STR_LIT>""" <EOL> self . headers [ '<STR_LIT>' ] = content_disposition or '<STR_LIT>' <EOL> self . headers [ '<STR_LIT>' ] += '<STR_LIT>' . join ( [ <EOL> '<STR_LIT>' , self . _render_parts ( <EOL> ( ( '<STR_LIT:name>' , self . _name ) , ( '<STR_LIT:filename>' , self . _filename ) ) <EOL> ) <EOL> ] ) <EOL> self . headers [ '<STR_LIT:Content-Type>' ] = content_type <EOL> self . headers [ '<STR_LIT>' ] = content_location </s>
