<s> import six <EOL> from . . util import quote , quote_plus <EOL> def url_quote ( s , charset = '<STR_LIT:utf-8>' , safe = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( s , six . text_type ) : <EOL> s = s . encode ( charset ) <EOL> elif not isinstance ( s , str ) : <EOL> s = str ( s ) <EOL> return quote ( s , safe = safe ) <EOL> def url_encode ( obj , charset = "<STR_LIT:utf8>" , encode_keys = False ) : <EOL> items = [ ] <EOL> if isinstance ( obj , dict ) : <EOL> for k , v in list ( obj . items ( ) ) : <EOL> items . append ( ( k , v ) ) <EOL> else : <EOL> items = list ( items ) <EOL> tmp = [ ] <EOL> for k , v in items : <EOL> if encode_keys : <EOL> k = encode ( k , charset ) <EOL> if not isinstance ( v , ( tuple , list ) ) : <EOL> v = [ v ] <EOL> for v1 in v : <EOL> if v1 is None : <EOL> v1 = '<STR_LIT>' <EOL> elif six . callable ( v1 ) : <EOL> v1 = encode ( v1 ( ) , charset ) <EOL> else : <EOL> v1 = encode ( v1 , charset ) <EOL> tmp . append ( '<STR_LIT>' % ( quote ( k ) , quote_plus ( v1 ) ) ) <EOL> return '<STR_LIT:&>' . join ( tmp ) <EOL> def encode ( v , charset = "<STR_LIT:utf8>" ) : <EOL> if isinstance ( v , six . text_type ) : <EOL> v = v . encode ( charset ) <EOL> else : <EOL> v = str ( v ) <EOL> return v <EOL> def make_uri ( base , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> charset = kwargs . pop ( "<STR_LIT>" , "<STR_LIT:utf-8>" ) <EOL> safe = kwargs . pop ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> encode_keys = kwargs . pop ( "<STR_LIT>" , True ) <EOL> base_trailing_slash = False <EOL> if base and base . endswith ( "<STR_LIT:/>" ) : <EOL> base_trailing_slash = True <EOL> base = base [ : - <NUM_LIT:1> ] <EOL> retval = [ base ] <EOL> _path = [ ] <EOL> trailing_slash = False <EOL> for s in args : <EOL> if s is not None and isinstance ( s , six . string_types ) : <EOL> if len ( s ) > <NUM_LIT:1> and s . endswith ( '<STR_LIT:/>' ) : <EOL> trailing_slash = True <EOL> else : <EOL> trailing_slash = False <EOL> _path . append ( url_quote ( s . strip ( '<STR_LIT:/>' ) , charset , safe ) ) <EOL> path_str = "<STR_LIT>" <EOL> if _path : <EOL> path_str = "<STR_LIT:/>" . join ( [ '<STR_LIT>' ] + _path ) <EOL> if trailing_slash : <EOL> path_str = path_str + "<STR_LIT:/>" <EOL> elif base_trailing_slash : <EOL> path_str = path_str + "<STR_LIT:/>" <EOL> if path_str : <EOL> retval . append ( path_str ) <EOL> params_str = url_encode ( kwargs , charset , encode_keys ) <EOL> if params_str : <EOL> retval . extend ( [ '<STR_LIT:?>' , params_str ] ) <EOL> return '<STR_LIT>' . join ( retval ) </s>
