macurl2path.py 3.04 KB
Newer Older
1 2 3
"""Macintosh-specific module for conversion between pathnames and URLs.

Do not import directly; use urllib instead."""
4 5 6 7

import urllib
import os

8 9
__all__ = ["url2pathname","pathname2url"]

10 11 12 13 14 15
def url2pathname(pathname):
    "Convert /-delimited pathname to mac pathname"
    #
    # XXXX The .. handling should be fixed...
    #
    tp = urllib.splittype(pathname)[0]
16
    if tp and tp != 'file':
17
        raise RuntimeError, 'Cannot convert non-local URL to pathname'
18 19
    # Turn starting /// into /, an empty hostname means current host
    if pathname[:3] == '///':
Tim Peters's avatar
Tim Peters committed
20
        pathname = pathname[2:]
21 22
    elif pathname[:2] == '//':
        raise RuntimeError, 'Cannot convert non-local URL to pathname'
23
    components = pathname.split('/')
24 25 26
    # Remove . and embedded ..
    i = 0
    while i < len(components):
27 28 29 30 31 32
        if components[i] == '.':
            del components[i]
        elif components[i] == '..' and i > 0 and \
                                  components[i-1] not in ('', '..'):
            del components[i-1:i+1]
            i = i-1
33
        elif components[i] == '' and i > 0 and components[i-1] != '':
34 35 36
            del components[i]
        else:
            i = i+1
37
    if not components[0]:
38
        # Absolute unix path, don't start with colon
39
        rv = ':'.join(components[1:])
40
    else:
41 42 43 44 45 46
        # relative unix path, start with colon. First replace
        # leading .. by empty strings (giving ::file)
        i = 0
        while i < len(components) and components[i] == '..':
            components[i] = ''
            i = i + 1
47
        rv = ':' + ':'.join(components)
48 49
    # and finally unquote slashes and other funny characters
    return urllib.unquote(rv)
50 51 52 53

def pathname2url(pathname):
    "convert mac pathname to /-delimited pathname"
    if '/' in pathname:
54
        raise RuntimeError, "Cannot convert pathname containing slashes"
55
    components = pathname.split(':')
56 57
    # Remove empty first and/or last component
    if components[0] == '':
58
        del components[0]
59
    if components[-1] == '':
60
        del components[-1]
61
    # Replace empty string ('::') by .. (will result in '/../' later)
62
    for i in range(len(components)):
63 64
        if components[i] == '':
            components[i] = '..'
65
    # Truncate names longer than 31 bytes
66
    components = map(_pncomp2url, components)
67 68

    if os.path.isabs(pathname):
69
        return '/' + '/'.join(components)
70
    else:
71
        return '/'.join(components)
Tim Peters's avatar
Tim Peters committed
72

73
def _pncomp2url(component):
Tim Peters's avatar
Tim Peters committed
74 75 76
    component = urllib.quote(component[:31], safe='')  # We want to quote slashes
    return component

77 78
def test():
    for url in ["index.html",
79 80 81 82
                "bar/index.html",
                "/foo/bar/index.html",
                "/foo/bar/",
                "/"]:
83
        print '%r -> %r' % (url, url2pathname(url))
84
    for path in ["drive:",
85 86 87 88 89 90 91
                 "drive:dir:",
                 "drive:dir:file",
                 "drive:file",
                 "file",
                 ":file",
                 ":dir:",
                 ":dir:file"]:
92
        print '%r -> %r' % (path, pathname2url(path))
93 94 95

if __name__ == '__main__':
    test()