nturl2path.py 2.39 KB
Newer Older
1
"""Convert a NT pathname to a file URL and vice versa."""
Guido van Rossum's avatar
Guido van Rossum committed
2 3

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

def pathname2url(p):
41 42 43
    """OS-specific conversion from a file system path to a relative URL
    of the 'file' scheme; not recommended for general use."""
    # e.g.
44
    #   C:\foo\bar\spam.foo
45
    # becomes
46
    #   ///C:/foo/bar/spam.foo
47
    import urllib.parse
Tim Peters's avatar
Tim Peters committed
48 49 50 51 52 53 54
    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
55
        components = p.split('\\')
56
        return urllib.parse.quote('/'.join(components))
57
    comp = p.split(':')
Tim Peters's avatar
Tim Peters committed
58 59
    if len(comp) != 2 or len(comp[0]) > 1:
        error = 'Bad path: ' + p
60
        raise OSError(error)
Guido van Rossum's avatar
Guido van Rossum committed
61

62
    drive = urllib.parse.quote(comp[0].upper())
63
    components = comp[1].split('\\')
64
    path = '///' + drive + ':'
Tim Peters's avatar
Tim Peters committed
65 66
    for comp in components:
        if comp:
67
            path = path + '/' + urllib.parse.quote(comp)
Tim Peters's avatar
Tim Peters committed
68
    return path