mirror of
https://github.com/python/cpython.git
synced 2025-07-19 01:05:26 +00:00
urlopen: add basejoin() function.
addpack: new module to add packages to sys.path.
This commit is contained in:
parent
d66acb45f8
commit
d1df83ba6c
3 changed files with 156 additions and 0 deletions
|
@ -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'
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue