/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
7204.2.1 by Jelmer Vernooij
Move location_to_url to its own module.
1
# Copyright (C) 2006-2010 Canonical Ltd
2
# Copyright (C) 2018 Breezy Developers
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
18
"""UI location string handling."""
19
20
from __future__ import absolute_import
21
22
from . import (
23
    urlutils,
24
    )
25
from .sixish import (
26
    PY3,
27
    string_types,
28
    )
29
30
31
def location_to_url(location):
32
    """Determine a fully qualified URL from a location string.
33
34
    This will try to interpret location as both a URL and a directory path. It
35
    will also lookup the location in directories.
36
37
    :param location: Unicode or byte string object with a location
38
    :raise InvalidURL: If the location is already a URL, but not valid.
39
    :return: Byte string with resulting URL
40
    """
41
    if not isinstance(location, string_types):
42
        raise AssertionError("location not a byte or unicode string")
43
    from .directory_service import directories
44
    location = directories.dereference(location)
45
46
    # Catch any URLs which are passing Unicode rather than ASCII
47
    try:
48
        location = location.encode('ascii')
49
    except UnicodeError:
50
        if urlutils.is_url(location):
51
            raise urlutils.InvalidURL(path=location,
52
                                      extra='URLs must be properly escaped')
53
        location = urlutils.local_path_to_url(location)
54
    else:
55
        if PY3:
56
            location = location.decode('ascii')
57
58
    if location.startswith("file:") and not location.startswith("file://"):
59
        return urlutils.join(urlutils.local_path_to_url("."), location[5:])
60
61
    if not urlutils.is_url(location):
62
        return urlutils.local_path_to_url(location)
63
64
    return location