def url_join(base, url, allow_fragments=True):
 if isinstance(base, tuple):
  base = url_unparse(base)
 if isinstance(url, tuple):
  url = url_unparse(url)
 (base, url) = normalize_string_tuple((base, url))
 s = make_literal_wrapper(base)
 if (not base):
  return url
 if (not url):
  return base
 (bscheme, bnetloc, bpath, bquery, bfragment) = url_parse(base, allow_fragments=allow_fragments)
 (scheme, netloc, path, query, fragment) = url_parse(url, bscheme, allow_fragments)
 if (scheme != bscheme):
  return url
 if netloc:
  return url_unparse((scheme, netloc, path, query, fragment))
 netloc = bnetloc
 if (path[:1] == s('/')):
  segments = path.split(s('/'))
 elif (not path):
  segments = bpath.split(s('/'))
  if (not query):
   query = bquery
 else:
  segments = (bpath.split(s('/'))[:(-1)] + path.split(s('/')))
 if (segments[(-1)] == s('.')):
  segments[(-1)] = s('')
 segments = [segment for segment in segments if (segment != s('.'))]
 while 1:
  i = 1
  n = (len(segments) - 1)
  while (i < n):
   if ((segments[i] == s('..')) and (segments[(i - 1)] not in (s(''), s('..')))):
    del segments[(i - 1):(i + 1)]
    break
   i += 1
  else:
   break
 unwanted_marker = [s(''), s('..')]
 while (segments[:2] == unwanted_marker):
  del segments[1]
 path = s('/').join(segments)
 return url_unparse((scheme, netloc, path, query, fragment))

def url_join ( base , url , allow_fragments = True ) : if isinstance ( base , tuple ) : base = url_unparse ( base ) if isinstance ( url , tuple ) : url = url_unparse ( url ) ( base , url ) = normalize_string_tuple ( ( base , url ) ) s = make_literal_wrapper ( base ) if ( not base ) : return url if ( not url ) : return base ( bscheme , bnetloc , bpath , bquery , bfragment ) = url_parse ( base , allow_fragments = allow_fragments ) ( scheme , netloc , path , query , fragment ) = url_parse ( url , bscheme , allow_fragments ) if ( scheme != bscheme ) : return url if netloc : return url_unparse ( ( scheme , netloc , path , query , fragment ) ) netloc = bnetloc if ( path [ : 1 ] == s ( '/' ) ) : segments = path . split ( s ( '/' ) ) elif ( not path ) : segments = bpath . split ( s ( '/' ) ) if ( not query ) : query = bquery else : segments = ( bpath . split ( s ( '/' ) ) [ : ( - 1 ) ] + path . split ( s ( '/' ) ) ) if ( segments [ ( - 1 ) ] == s ( '.' ) ) : segments [ ( - 1 ) ] = s ( '' ) segments = [ segment for segment in segments if ( segment != s ( '.' ) ) ] while 1 : i = 1 n = ( len ( segments ) - 1 ) while ( i < n ) : if ( ( segments [ i ] == s ( '..' ) ) and ( segments [ ( i - 1 ) ] not in ( s ( '' ) , s ( '..' ) ) ) ) : del segments [ ( i - 1 ) : ( i + 1 ) ] break i += 1 else : break unwanted_marker = [ s ( '' ) , s ( '..' ) ] while ( segments [ : 2 ] == unwanted_marker ) : del segments [ 1 ] path = s ( '/' ) . join ( segments ) return url_unparse ( ( scheme , netloc , path , query , fragment ) )
convenience method for joining parts of a url any leading and trailing / characters are removed .