Merge lp:~jelmer/brz/rewrite-url into lp:brz

Proposed by Jelmer Vernooij
Status: Merged
Approved by: Jelmer Vernooij
Approved revision: no longer in the source branch.
Merge reported by: The Breezy Bot
Merged at revision: not available
Proposed branch: lp:~jelmer/brz/rewrite-url
Merge into: lp:brz
Diff against target: 65 lines (+29/-0)
2 files modified
breezy/location.py (+18/-0)
breezy/tests/test_location.py (+11/-0)
To merge this branch: bzr merge lp:~jelmer/brz/rewrite-url
Reviewer Review Type Date Requested Status
Martin Packman Approve
Review via email: mp+362941@code.launchpad.net

Commit message

Add a hook for URL dereferencing.

Description of the change

Add a hook for URL dereferencing.

To post a comment you must log in.
Revision history for this message
Martin Packman (gz) wrote :

I'm a bit scared this is too low-level a hook to sensibly support as we want to hack location stuff around a fair bit still, but I guess we're not offering *that* much on external compatibility guarantees at present.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'breezy/location.py'
--- breezy/location.py 2018-11-23 23:51:34 +0000
+++ breezy/location.py 2019-02-09 10:34:29 +0000
@@ -22,12 +22,27 @@
22from . import (22from . import (
23 urlutils,23 urlutils,
24 )24 )
25from .hooks import Hooks
25from .sixish import (26from .sixish import (
26 PY3,27 PY3,
27 string_types,28 string_types,
28 )29 )
2930
3031
32class LocationHooks(Hooks):
33 """Dictionary mapping hook name to a list of callables for location hooks.
34 """
35
36 def __init__(self):
37 Hooks.__init__(self, "breezy.location", "hooks")
38 self.add_hook(
39 'rewrite_url',
40 "Called with a URL to rewrite.", (3, 0))
41
42
43hooks = LocationHooks()
44
45
31def location_to_url(location):46def location_to_url(location):
32 """Determine a fully qualified URL from a location string.47 """Determine a fully qualified URL from a location string.
3348
@@ -61,4 +76,7 @@
61 if not urlutils.is_url(location):76 if not urlutils.is_url(location):
62 return urlutils.local_path_to_url(location)77 return urlutils.local_path_to_url(location)
6378
79 for hook in hooks['rewrite_url']:
80 location = hook(location)
81
64 return location82 return location
6583
=== modified file 'breezy/tests/test_location.py'
--- breezy/tests/test_location.py 2018-11-23 23:51:34 +0000
+++ breezy/tests/test_location.py 2019-02-09 10:34:29 +0000
@@ -23,6 +23,7 @@
23 )23 )
24from ..directory_service import directories24from ..directory_service import directories
25from ..location import (25from ..location import (
26 hooks as location_hooks,
26 location_to_url,27 location_to_url,
27 )28 )
2829
@@ -73,3 +74,13 @@
7374
74 def test_absolute_file_url(self):75 def test_absolute_file_url(self):
75 self.assertEqual("file:///bar", location_to_url("file:/bar"))76 self.assertEqual("file:///bar", location_to_url("file:/bar"))
77
78 def test_rewrite_hook(self):
79 self.assertEqual(
80 'http://foo.example.com/blah', location_to_url('http://foo.example.com/blah'))
81 def rewrite_url(url):
82 return url.replace('foo', 'bar')
83 self.addCleanup(location_hooks.uninstall_named_hook, 'rewrite_url', 'test')
84 location_hooks.install_named_hook('rewrite_url', rewrite_url, 'test')
85 self.assertEqual(
86 'http://bar.example.com/bar', location_to_url('http://foo.example.com/foo'))

Subscribers

People subscribed via source and target branches