Rev 6034: (jelmer) Split location to URL conversion out into a separate function from in file:///home/pqm/archives/thelove/bzr/%2Btrunk/
Canonical.com Patch Queue Manager
pqm at pqm.ubuntu.com
Tue Jul 19 13:06:51 UTC 2011
At file:///home/pqm/archives/thelove/bzr/%2Btrunk/
------------------------------------------------------------
revno: 6034 [merge]
revision-id: pqm at pqm.ubuntu.com-20110719130644-efx0i6dq30myjhmk
parent: pqm at pqm.ubuntu.com-20110718183335-rall83p4yxjdd900
parent: jelmer at samba.org-20110719120012-q8aw0aqyy67pn3aj
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Tue 2011-07-19 13:06:44 +0000
message:
(jelmer) Split location to URL conversion out into a separate function from
get_transport. (Jelmer Vernooij)
modified:
bzrlib/errors.py errors.py-20050309040759-20512168c4e14fbd
bzrlib/tests/test_transport.py testtransport.py-20050718175618-e5cdb99f4555ddce
bzrlib/transport/__init__.py transport.py-20050711165921-4978aa7ce1285ad5
doc/en/release-notes/bzr-2.5.txt bzr2.5.txt-20110708125756-587p0hpw7oke4h05-1
=== modified file 'bzrlib/errors.py'
--- a/bzrlib/errors.py 2011-06-21 11:34:52 +0000
+++ b/bzrlib/errors.py 2011-07-16 20:06:11 +0000
@@ -621,7 +621,7 @@
_fmt = 'Unsupported protocol for url "%(path)s"%(extra)s'
- def __init__(self, url, extra):
+ def __init__(self, url, extra=""):
PathError.__init__(self, url, extra=extra)
=== modified file 'bzrlib/tests/test_transport.py'
--- a/bzrlib/tests/test_transport.py 2011-07-05 02:52:00 +0000
+++ b/bzrlib/tests/test_transport.py 2011-07-16 20:43:40 +0000
@@ -27,11 +27,13 @@
transport,
urlutils,
)
+from bzrlib.directory_service import directories
from bzrlib.transport import (
chroot,
fakenfs,
http,
local,
+ location_to_url,
memory,
pathfilter,
readonly,
@@ -993,3 +995,31 @@
result = http.unhtml_roughly(fake_html)
self.assertEquals(len(result), 1000)
self.assertStartsWith(result, " something!")
+
+
+class SomeDirectory(object):
+
+ def look_up(self, name, url):
+ return "http://bar"
+
+
+class TestLocationToUrl(tests.TestCase):
+
+ def test_regular_url(self):
+ self.assertEquals("file://foo", location_to_url("file://foo"))
+
+ def test_directory(self):
+ directories.register("bar:", SomeDirectory, "Dummy directory")
+ self.addCleanup(directories.remove, "bar:")
+ self.assertEquals("http://bar", location_to_url("bar:"))
+
+ def test_unicode_url(self):
+ self.assertRaises(errors.InvalidURL, location_to_url,
+ "http://fo/\xc3\xaf".decode("utf-8"))
+
+ def test_unicode_path(self):
+ self.assertEquals("file:///foo/bar%C3%AF",
+ location_to_url("/foo/bar\xc3\xaf".decode("utf-8")))
+
+ def test_path(self):
+ self.assertEquals("file:///foo/bar", location_to_url("/foo/bar"))
=== modified file 'bzrlib/transport/__init__.py'
--- a/bzrlib/transport/__init__.py 2011-05-18 19:22:46 +0000
+++ b/bzrlib/transport/__init__.py 2011-07-19 12:00:12 +0000
@@ -118,10 +118,6 @@
def register_transport(self, key, help=None):
self.register(key, [], help)
- def set_default_transport(self, key=None):
- """Return either 'key' or the default key if key is None"""
- self._default_key = key
-
transport_list_registry = TransportListRegistry()
@@ -1557,6 +1553,36 @@
raise NotImplementedError(self.disconnect)
+def location_to_url(location):
+ """Determine a fully qualified URL from a location string.
+
+ This will try to interpret location as both a URL and a directory path. It
+ will also lookup the location in directories.
+
+ :param location: Unicode or byte string object with a location
+ :raise InvalidURL: If the location is already a URL, but not valid.
+ :return: Byte string with resulting URL
+ """
+ if not isinstance(location, basestring):
+ raise AssertionError("location not a byte or unicode string")
+ from bzrlib.directory_service import directories
+ location = directories.dereference(location)
+
+ # Catch any URLs which are passing Unicode rather than ASCII
+ try:
+ location = location.encode('ascii')
+ except UnicodeError:
+ if urlutils.is_url(location):
+ raise errors.InvalidURL(path=location,
+ extra='URLs must be properly escaped')
+ location = urlutils.local_path_to_url(location)
+
+ if urlutils.is_url(location):
+ return location
+
+ return urlutils.local_path_to_url(location)
+
+
def get_transport(base, possible_transports=None):
"""Open a transport to access a URL or directory.
@@ -1570,28 +1596,7 @@
"""
if base is None:
base = '.'
- last_err = None
- from bzrlib.directory_service import directories
- base = directories.dereference(base)
-
- def convert_path_to_url(base, error_str):
- if urlutils.is_url(base):
- # This looks like a URL, but we weren't able to
- # instantiate it as such raise an appropriate error
- # FIXME: we have a 'error_str' unused and we use last_err below
- raise errors.UnsupportedProtocol(base, last_err)
- # This doesn't look like a protocol, consider it a local path
- new_base = urlutils.local_path_to_url(base)
- # mutter('converting os path %r => url %s', base, new_base)
- return new_base
-
- # Catch any URLs which are passing Unicode rather than ASCII
- try:
- base = base.encode('ascii')
- except UnicodeError:
- # Only local paths can be Unicode
- base = convert_path_to_url(base,
- 'URLs must be properly escaped (protocol: %s)')
+ base = location_to_url(base)
transport = None
if possible_transports is not None:
@@ -1603,6 +1608,7 @@
possible_transports.append(t_same_connection)
return t_same_connection
+ last_err = None
for proto, factory_list in transport_list_registry.items():
if proto is not None and base.startswith(proto):
transport, last_err = _try_transport_factories(base, factory_list)
@@ -1612,16 +1618,7 @@
raise AssertionError()
possible_transports.append(transport)
return transport
-
- # We tried all the different protocols, now try one last time
- # as a local protocol
- base = convert_path_to_url(base, 'Unsupported protocol: %s')
-
- # The default handler is the filesystem handler, stored as protocol None
- factory_list = transport_list_registry.get(None)
- transport, last_err = _try_transport_factories(base, factory_list)
-
- return transport
+ raise errors.UnsupportedProtocol(base, last_err)
def _try_transport_factories(base, factory_list):
@@ -1696,7 +1693,6 @@
register_transport_proto('file://',
help="Access using the standard filesystem (default)")
register_lazy_transport('file://', 'bzrlib.transport.local', 'LocalTransport')
-transport_list_registry.set_default_transport("file://")
register_transport_proto('sftp://',
help="Access using SFTP (most SSH servers provide SFTP).",
=== modified file 'doc/en/release-notes/bzr-2.5.txt'
--- a/doc/en/release-notes/bzr-2.5.txt 2011-07-18 18:33:35 +0000
+++ b/doc/en/release-notes/bzr-2.5.txt 2011-07-19 13:06:44 +0000
@@ -86,6 +86,9 @@
* Remove ``trace.info``, ``trace.error`` and ``trace.show_log_error``
deprecated in 2.1.0. (Vincent Ladeuil)
+* Remove ``TransportListRegistry.set_default_transport``, as the concept of
+ a default transport is currently unused. (Jelmer Vernooij)
+
Internals
*********
More information about the bazaar-commits
mailing list