~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/urlutils.py

  • Committer: John Arbash Meinel
  • Date: 2006-05-02 21:17:38 UTC
  • mto: This revision was merged to the branch mainline in revision 1752.
  • Revision ID: john@arbash-meinel.com-20060502211738-be512e6ec1e6e6cf
Updated strip_trailing_slash to support lots more url stuff, added tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
    
40
40
    This assumes that both paths are already fully specified file:// URLs.
41
41
    """
42
 
    assert len(base) >= MIN_ABS_URLPATHLENGTH, ('Length of base must be equal or'
 
42
    assert len(base) >= MIN_ABS_FILEURL_LENGTH, ('Length of base must be equal or'
43
43
        ' exceed the platform minimum url length (which is %d)' % 
44
 
        MIN_ABS_URLPATHLENGTH)
 
44
        MIN_ABS_FILEURL_LENGTH)
45
45
 
46
46
    base = local_path_from_url(base)
47
47
    path = local_path_from_url(path)
98
98
 
99
99
local_path_to_url = _posix_local_path_to_url
100
100
local_path_from_url = _posix_local_path_from_url
101
 
MIN_ABS_URLPATHLENGTH = len('file:///')
 
101
MIN_ABS_FILEURL_LENGTH = len('file:///')
102
102
 
103
103
if sys.platform == 'win32':
104
104
    local_path_to_url = _win32_local_path_to_url
105
105
    local_path_from_url = _win32_local_path_from_url
106
106
 
107
 
    MIN_ABS_URLPATHLENGTH = len('file:///C|/')
108
 
 
 
107
    MIN_ABS_FILEURL_LENGTH = len('file:///C|/')
 
108
 
 
109
 
 
110
def basename(url, exclude_trailing_slash=True):
 
111
    """Return the last component of a URL.
 
112
 
 
113
    :param url: The URL in question
 
114
    :param exclude_trailing_slash: If the url looks like "path/to/foo/"
 
115
        ignore the final slash and return 'foo' rather than ''
 
116
    """
 
117
    if exclude_trailing_slash:
 
118
        url = strip_trailing_slash(url)
109
119
 
110
120
def strip_trailing_slash(url):
111
121
    """Strip trailing slash, except for root paths.
112
122
 
113
123
    The definition of 'root path' is platform-dependent.
114
 
    But the passed in URL must be a file:/// url.
 
124
    This assumes that all URLs are valid netloc urls, such that they
 
125
    form:
 
126
    scheme://host/path
 
127
    It searches for ://, and then refuses to remove the next '/'.
 
128
    It can also handle relative paths
 
129
    Examples:
 
130
        path/to/foo       => path/to/foo
 
131
        path/to/foo/      => path/to/foo
 
132
        http://host/path/ => http://host/path
 
133
        http://host/path  => http://host/path
 
134
        http://host/      => http://host/
 
135
        file:///          => file:///
 
136
        file:///foo/      => file:///foo
 
137
        # This is unique on win32 platforms, and is the only URL
 
138
        # format which does it differently.
 
139
        file:///C|/       => file:///C|/
115
140
    """
116
 
    assert url.startswith('file:///'), \
117
 
        'strip_trailing_slash expects file:// urls (%s)' % url
118
 
    if len(url) != MIN_ABS_URLPATHLENGTH and url[-1] == '/':
 
141
    if not url.endswith('/'):
 
142
        # Nothing to do
 
143
        return url
 
144
    if sys.platform == 'win32' and url.startswith('file:///'):
 
145
        # This gets handled specially, because the 'top-level'
 
146
        # of a win32 path is actually the drive letter
 
147
        if len(url) > MIN_ABS_FILEURL_LENGTH:
 
148
            return url[:-1]
 
149
    scheme_loc = url.find('://')
 
150
    if scheme_loc == -1:
 
151
        # This is a relative path, as it has no scheme
 
152
        # so just chop off the last character
119
153
        return url[:-1]
120
 
    else:
 
154
 
 
155
    # Find the path separating slash
 
156
    # (first slash after the ://)
 
157
    first_path_slash = url.find('/', scheme_loc+3)
 
158
    if first_path_slash == -1 or first_path_slash == len(url)-1:
 
159
        # Don't chop off anything if the only slash is the path
 
160
        # separating slash
121
161
        return url
122
162
 
 
163
    return url[:-1]
 
164
 
123
165
 
124
166
def unescape(url):
125
167
    """Unescape relpath from url format.