20
20
to true URLs. Examples include lp:urls and per-user location aliases.
23
from bzrlib import errors, registry
24
from bzrlib.lazy_import import lazy_import
27
from .lazy_import import lazy_import
25
28
lazy_import(globals(), """
26
from bzrlib.branch import Branch
27
from bzrlib import urlutils
30
branch as _mod_branch,
31
controldir as _mod_controldir,
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:])
31
57
class DirectoryServiceRegistry(registry.Registry):
32
58
"""This object maintains and uses a list of directory services.
49
75
requires further dereferencing.
51
77
:param url: The URL to dereference
78
:param purpose: Purpose of the URL ('read', 'write' or None - if not declared)
52
79
:return: The dereferenced URL if applicable, the input URL otherwise.
54
81
match = self.get_prefix(url)
57
84
service, name = match
58
return service().look_up(name, url)
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)
60
93
directories = DirectoryServiceRegistry()
63
class AliasDirectory(object):
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):
64
111
"""Directory lookup for locations associated with a branch.
66
113
:parent, :submit, :public, :push, :this, and :bound are currently
67
114
supported. On error, a subclass of DirectoryLookupFailure will be raised.
70
def look_up(self, name, url):
71
branch = Branch.open_containing('.')[0]
73
'parent': branch.get_parent,
74
'submit': branch.get_submit_branch,
75
'public': branch.get_public_branch,
76
'bound': branch.get_bound_location,
77
'push': branch.get_push_location,
78
'this': lambda: branch.base
117
branch_aliases = registry.Registry()
118
branch_aliases.register('parent', lambda b: b.get_parent(),
119
help="The parent of this branch.")
120
branch_aliases.register('submit', lambda b: b.get_submit_branch(),
121
help="The submit branch for this branch.")
122
branch_aliases.register('public', lambda b: b.get_public_branch(),
123
help="The public location of this branch.")
124
branch_aliases.register('bound', lambda b: b.get_bound_location(),
125
help="The branch this branch is bound to, for bound branches.")
126
branch_aliases.register('push', lambda b: b.get_push_location(),
127
help="The saved location used for `brz push` with no arguments.")
128
branch_aliases.register('this', lambda b: b.base,
131
def look_up(self, name, url, purpose=None):
132
branch = _mod_branch.Branch.open_containing('.')[0]
80
133
parts = url.split('/', 1)
81
134
if len(parts) == 2:
82
135
name, extra = parts
87
method = lookups[name[1:]]
140
method = self.branch_aliases.get(name[1:])
89
raise errors.InvalidLocationAlias(url)
142
raise InvalidLocationAlias(url)
144
result = method(branch)
92
145
if result is None:
93
raise errors.UnsetLocationAlias(url)
146
raise UnsetLocationAlias(url)
94
147
if extra is not None:
95
148
result = urlutils.join(result, extra)
152
def help_text(cls, topic):
154
for key in cls.branch_aliases.keys():
155
help = cls.branch_aliases.get_help(key)
156
alias_lines.append(" :%-10s%s\n" % (key, help))
161
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`.
167
For example, to push to the parent location::
170
""" % "".join(alias_lines)
98
173
directories.register(':', AliasDirectory,
99
174
'Easy access to remembered branch locations')
177
class ColocatedDirectory(Directory):
178
"""Directory lookup for colocated branches.
180
co:somename will resolve to the colocated branch with "somename" in
181
the current directory.
184
def look_up(self, name, url, purpose=None):
185
dir = _mod_controldir.ControlDir.open_containing('.')[0]
186
return urlutils.join_segment_parameters(
187
dir.user_url, {"branch": urlutils.escape(name)})
190
directories.register('co:', ColocatedDirectory,
191
'Easy access to colocated branches')