1
# Copyright (C) 2006-2010 Canonical Ltd
2
# Copyright (C) 2018 Breezy Developers
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.
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.
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
18
"""UI location string handling."""
20
from __future__ import absolute_import
31
def location_to_url(location):
32
"""Determine a fully qualified URL from a location string.
34
This will try to interpret location as both a URL and a directory path. It
35
will also lookup the location in directories.
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
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)
46
# Catch any URLs which are passing Unicode rather than ASCII
48
location = location.encode('ascii')
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)
56
location = location.decode('ascii')
58
if location.startswith("file:") and not location.startswith("file://"):
59
return urlutils.join(urlutils.local_path_to_url("."), location[5:])
61
if not urlutils.is_url(location):
62
return urlutils.local_path_to_url(location)