~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/urlutils.py

  • Committer: Ian Clatworthy
  • Date: 2010-05-26 04:26:59 UTC
  • mto: (5255.2.1 integration)
  • mto: This revision was merged to the branch mainline in revision 5256.
  • Revision ID: ian.clatworthy@canonical.com-20100526042659-2e3p4qdjr0sby0bt
Fix PDF generation of User Reference

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Bazaar -- distributed version control
2
 
#
3
 
# Copyright (C) 2006 Canonical Ltd
 
1
# Copyright (C) 2006-2010 Canonical Ltd
4
2
#
5
3
# This program is free software; you can redistribute it and/or modify
6
4
# it under the terms of the GNU General Public License as published by
77
75
    This assumes that both paths are already fully specified file:// URLs.
78
76
    """
79
77
    if len(base) < MIN_ABS_FILEURL_LENGTH:
80
 
        raise ValueError('Length of base must be equal or'
 
78
        raise ValueError('Length of base (%r) must equal or'
81
79
            ' exceed the platform minimum url length (which is %d)' %
82
 
            MIN_ABS_FILEURL_LENGTH)
 
80
            (base, MIN_ABS_FILEURL_LENGTH))
83
81
    base = local_path_from_url(base)
84
82
    path = local_path_from_url(path)
85
83
    return escape(osutils.relpath(base, path))
219
217
# jam 20060502 Sorted to 'l' because the final target is 'local_path_from_url'
220
218
def _posix_local_path_from_url(url):
221
219
    """Convert a url like file:///path/to/foo into /path/to/foo"""
222
 
    if not url.startswith('file:///'):
223
 
        raise errors.InvalidURL(url, 'local urls must start with file:///')
 
220
    file_localhost_prefix = 'file://localhost/'
 
221
    if url.startswith(file_localhost_prefix):
 
222
        path = url[len(file_localhost_prefix) - 1:]
 
223
    elif not url.startswith('file:///'):
 
224
        raise errors.InvalidURL(
 
225
            url, 'local urls must start with file:/// or file://localhost/')
 
226
    else:
 
227
        path = url[len('file://'):]
224
228
    # We only strip off 2 slashes
225
 
    return unescape(url[len('file://'):])
 
229
    return unescape(path)
226
230
 
227
231
 
228
232
def _posix_local_path_to_url(path):
687
691
    if len(segments) == 0:
688
692
        return '.'
689
693
    return osutils.pathjoin(*segments)
 
694
 
 
695
 
 
696
 
 
697
def parse_url(url):
 
698
    """Extract the server address, the credentials and the path from the url.
 
699
 
 
700
    user, password, host and path should be quoted if they contain reserved
 
701
    chars.
 
702
 
 
703
    :param url: an quoted url
 
704
 
 
705
    :return: (scheme, user, password, host, port, path) tuple, all fields
 
706
        are unquoted.
 
707
    """
 
708
    if isinstance(url, unicode):
 
709
        raise errors.InvalidURL('should be ascii:\n%r' % url)
 
710
    url = url.encode('utf-8')
 
711
    (scheme, netloc, path, params,
 
712
     query, fragment) = urlparse.urlparse(url, allow_fragments=False)
 
713
    user = password = host = port = None
 
714
    if '@' in netloc:
 
715
        user, host = netloc.rsplit('@', 1)
 
716
        if ':' in user:
 
717
            user, password = user.split(':', 1)
 
718
            password = urllib.unquote(password)
 
719
        user = urllib.unquote(user)
 
720
    else:
 
721
        host = netloc
 
722
 
 
723
    if ':' in host and not (host[0] == '[' and host[-1] == ']'): #there *is* port
 
724
        host, port = host.rsplit(':',1)
 
725
        try:
 
726
            port = int(port)
 
727
        except ValueError:
 
728
            raise errors.InvalidURL('invalid port number %s in url:\n%s' %
 
729
                                    (port, url))
 
730
    if host != "" and host[0] == '[' and host[-1] == ']': #IPv6
 
731
        host = host[1:-1]
 
732
 
 
733
    host = urllib.unquote(host)
 
734
    path = urllib.unquote(path)
 
735
 
 
736
    return (scheme, user, password, host, port, path)