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
1=== modified file 'breezy/location.py'
2--- breezy/location.py 2018-11-23 23:51:34 +0000
3+++ breezy/location.py 2019-02-09 10:34:29 +0000
4@@ -22,12 +22,27 @@
5 from . import (
6 urlutils,
7 )
8+from .hooks import Hooks
9 from .sixish import (
10 PY3,
11 string_types,
12 )
13
14
15+class LocationHooks(Hooks):
16+ """Dictionary mapping hook name to a list of callables for location hooks.
17+ """
18+
19+ def __init__(self):
20+ Hooks.__init__(self, "breezy.location", "hooks")
21+ self.add_hook(
22+ 'rewrite_url',
23+ "Called with a URL to rewrite.", (3, 0))
24+
25+
26+hooks = LocationHooks()
27+
28+
29 def location_to_url(location):
30 """Determine a fully qualified URL from a location string.
31
32@@ -61,4 +76,7 @@
33 if not urlutils.is_url(location):
34 return urlutils.local_path_to_url(location)
35
36+ for hook in hooks['rewrite_url']:
37+ location = hook(location)
38+
39 return location
40
41=== modified file 'breezy/tests/test_location.py'
42--- breezy/tests/test_location.py 2018-11-23 23:51:34 +0000
43+++ breezy/tests/test_location.py 2019-02-09 10:34:29 +0000
44@@ -23,6 +23,7 @@
45 )
46 from ..directory_service import directories
47 from ..location import (
48+ hooks as location_hooks,
49 location_to_url,
50 )
51
52@@ -73,3 +74,13 @@
53
54 def test_absolute_file_url(self):
55 self.assertEqual("file:///bar", location_to_url("file:/bar"))
56+
57+ def test_rewrite_hook(self):
58+ self.assertEqual(
59+ 'http://foo.example.com/blah', location_to_url('http://foo.example.com/blah'))
60+ def rewrite_url(url):
61+ return url.replace('foo', 'bar')
62+ self.addCleanup(location_hooks.uninstall_named_hook, 'rewrite_url', 'test')
63+ location_hooks.install_named_hook('rewrite_url', rewrite_url, 'test')
64+ self.assertEqual(
65+ 'http://bar.example.com/bar', location_to_url('http://foo.example.com/foo'))

Subscribers

People subscribed via source and target branches