nturl2path.py 2.52 KB
Newer Older
1 2 3 4 5 6
"""Convert a NT pathname to a file URL and vice versa.

This module only exists to provide OS-specific code
for urllib.requests, thus do not use directly.
"""
# Testing is done through test_urllib.
Guido van Rossum's avatar
Guido van Rossum committed
7 8

def url2pathname(url):
9 10 11
    """OS-specific conversion from a relative URL of the 'file' scheme
    to a file system path; not recommended for general use."""
    # e.g.
12 13 14 15 16
    #   ///C|/foo/bar/spam.foo
    # and
    #   ///C:/foo/bar/spam.foo
    # become
    #   C:\foo\bar\spam.foo
17
    import string, urllib.parse
18 19
    # Windows itself uses ":" even in URLs.
    url = url.replace(':', '|')
Tim Peters's avatar
Tim Peters committed
20 21 22 23 24 25 26
    if not '|' in url:
        # No drive specifier, just convert slashes
        if url[:4] == '////':
            # path is something like ////host/path/on/remote/host
            # convert this to \\host\path\on\remote\host
            # (notice halving of slashes at the start of the path)
            url = url[2:]
27
        components = url.split('/')
Tim Peters's avatar
Tim Peters committed
28
        # make sure not to convert quoted slashes :-)
29
        return urllib.parse.unquote('\\'.join(components))
30
    comp = url.split('|')
31
    if len(comp) != 2 or comp[0][-1] not in string.ascii_letters:
Tim Peters's avatar
Tim Peters committed
32
        error = 'Bad URL: ' + url
33
        raise OSError(error)
34 35
    drive = comp[0][-1].upper()
    components = comp[1].split('/')
Tim Peters's avatar
Tim Peters committed
36
    path = drive + ':'
37
    for comp in components:
Tim Peters's avatar
Tim Peters committed
38
        if comp:
39
            path = path + '\\' + urllib.parse.unquote(comp)
40 41 42
    # Issue #11474 - handing url such as |c/|
    if path.endswith(':') and url.endswith('/'):
        path += '\\'
Tim Peters's avatar
Tim Peters committed
43
    return path
Guido van Rossum's avatar
Guido van Rossum committed
44 45

def pathname2url(p):
46 47 48
    """OS-specific conversion from a file system path to a relative URL
    of the 'file' scheme; not recommended for general use."""
    # e.g.
49
    #   C:\foo\bar\spam.foo
50
    # becomes
51
    #   ///C:/foo/bar/spam.foo
52
    import urllib.parse
Tim Peters's avatar
Tim Peters committed
53 54 55 56 57 58 59
    if not ':' in p:
        # No drive specifier, just convert slashes and quote the name
        if p[:2] == '\\\\':
        # path is something like \\host\path\on\remote\host
        # convert this to ////host/path/on/remote/host
        # (notice doubling of slashes at the start of the path)
            p = '\\\\' + p
60
        components = p.split('\\')
61
        return urllib.parse.quote('/'.join(components))
62
    comp = p.split(':')
Tim Peters's avatar
Tim Peters committed
63 64
    if len(comp) != 2 or len(comp[0]) > 1:
        error = 'Bad path: ' + p
65
        raise OSError(error)
Guido van Rossum's avatar
Guido van Rossum committed
66

67
    drive = urllib.parse.quote(comp[0].upper())
68
    components = comp[1].split('\\')
69
    path = '///' + drive + ':'
Tim Peters's avatar
Tim Peters committed
70 71
    for comp in components:
        if comp:
72
            path = path + '/' + urllib.parse.quote(comp)
Tim Peters's avatar
Tim Peters committed
73
    return path