urlopen: add basejoin() function.

addpack: new module to add packages to sys.path.
This commit is contained in:
Guido van Rossum 1994-03-07 11:45:36 +00:00
parent d66acb45f8
commit d1df83ba6c
3 changed files with 156 additions and 0 deletions

View file

@ -303,6 +303,28 @@ class addinfo(addbase):
return self.headers
# Utility to combine a URL with a base URL to form a new URL
def basejoin(base, url):
type, path = splittype(url)
if type: return url
host, path = splithost(path)
basetype, basepath = splittype(base)
basehost, basepath = splithost(basepath)
basepath, basetag = splittag(basepath)
basepath, basequery = splitquery(basepath)
type = basetype or 'file'
if path[:1] != '/':
import string
i = string.rfind(basepath, '/')
if i < 0: basepath = '/'
else: basepath = basepath[:i+1]
path = basepath + path
if not host: host = basehost
if host: return type + '://' + host + path
else: return type + ':' + path
# Utilities to parse URLs:
# unwrap('<URL:type//host/path>') --> 'type//host/path'
# splittype('type:opaquestring') --> 'type', 'opaquestring'