37
class DirectoryLookupFailure(errors.BzrError):
38
"""Base type for lookup errors."""
41
class InvalidLocationAlias(DirectoryLookupFailure):
43
_fmt = '"%(alias_name)s" is not a valid location alias.'
45
def __init__(self, alias_name):
46
DirectoryLookupFailure.__init__(self, alias_name=alias_name)
49
class UnsetLocationAlias(DirectoryLookupFailure):
51
_fmt = 'No %(alias_name)s location assigned.'
53
def __init__(self, alias_name):
54
DirectoryLookupFailure.__init__(self, alias_name=alias_name[1:])
57
39
class DirectoryServiceRegistry(registry.Registry):
58
40
"""This object maintains and uses a list of directory services.
65
47
name and URL, and return a URL.
68
def dereference(self, url, purpose=None):
50
def dereference(self, url):
69
51
"""Dereference a supplied URL if possible.
71
53
URLs that match a registered directory service prefix are looked up in
75
57
requires further dereferencing.
77
59
:param url: The URL to dereference
78
:param purpose: Purpose of the URL ('read', 'write' or None - if not declared)
79
60
:return: The dereferenced URL if applicable, the input URL otherwise.
81
62
match = self.get_prefix(url)
84
65
service, name = match
87
return directory.look_up(name, url, purpose=purpose)
89
# Compatibility for plugins written for Breezy < 3.0.0
90
return directory.look_up(name, url)
66
return service().look_up(name, url)
93
68
directories = DirectoryServiceRegistry()
96
class Directory(object):
97
"""Abstract directory lookup class."""
99
def look_up(self, name, url, purpose=None):
100
"""Look up an entry in a directory.
102
:param name: Directory name
103
:param url: The URL to dereference
104
:param purpose: Purpose of the URL ('read', 'write' or None - if not declared)
105
:return: The dereferenced URL if applicable, the input URL otherwise.
107
raise NotImplementedError(self.look_up)
110
class AliasDirectory(Directory):
70
class AliasDirectory(object):
111
71
"""Directory lookup for locations associated with a branch.
113
73
:parent, :submit, :public, :push, :this, and :bound are currently
117
77
branch_aliases = registry.Registry()
118
78
branch_aliases.register('parent', lambda b: b.get_parent(),
119
help="The parent of this branch.")
79
help="The parent of this branch.")
120
80
branch_aliases.register('submit', lambda b: b.get_submit_branch(),
121
help="The submit branch for this branch.")
81
help="The submit branch for this branch.")
122
82
branch_aliases.register('public', lambda b: b.get_public_branch(),
123
help="The public location of this branch.")
83
help="The public location of this branch.")
124
84
branch_aliases.register('bound', lambda b: b.get_bound_location(),
125
help="The branch this branch is bound to, for bound branches.")
85
help="The branch this branch is bound to, for bound branches.")
126
86
branch_aliases.register('push', lambda b: b.get_push_location(),
127
help="The saved location used for `brz push` with no arguments.")
87
help="The saved location used for `bzr push` with no arguments.")
128
88
branch_aliases.register('this', lambda b: b.base,
131
def look_up(self, name, url, purpose=None):
91
def look_up(self, name, url):
132
92
branch = _mod_branch.Branch.open_containing('.')[0]
133
93
parts = url.split('/', 1)
134
94
if len(parts) == 2:
140
100
method = self.branch_aliases.get(name[1:])
142
raise InvalidLocationAlias(url)
102
raise errors.InvalidLocationAlias(url)
144
104
result = method(branch)
145
105
if result is None:
146
raise UnsetLocationAlias(url)
106
raise errors.UnsetLocationAlias(url)
147
107
if extra is not None:
148
108
result = urlutils.join(result, extra)
161
121
Bazaar defines several aliases for locations associated with a branch. These
162
can be used with most commands that expect a location, such as `brz push`.
122
can be used with most commands that expect a location, such as `bzr push`.
164
124
The aliases are::
167
127
For example, to push to the parent location::
170
130
""" % "".join(alias_lines)
174
134
'Easy access to remembered branch locations')
177
class ColocatedDirectory(Directory):
137
class ColocatedDirectory(object):
178
138
"""Directory lookup for colocated branches.
180
140
co:somename will resolve to the colocated branch with "somename" in
181
141
the current directory.
184
def look_up(self, name, url, purpose=None):
144
def look_up(self, name, url):
185
145
dir = _mod_controldir.ControlDir.open_containing('.')[0]
186
return urlutils.join_segment_parameters(
187
dir.user_url, {"branch": urlutils.escape(name)})
146
return urlutils.join_segment_parameters(dir.user_url,
147
{"branch": urlutils.escape(name)})
190
150
directories.register('co:', ColocatedDirectory,
191
151
'Easy access to colocated branches')