/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/directory_service.py

  • Committer: Jelmer Vernooij
  • Date: 2017-06-08 23:30:31 UTC
  • mto: This revision was merged to the branch mainline in revision 6690.
  • Revision ID: jelmer@jelmer.uk-20170608233031-3qavls2o7a1pqllj
Update imports.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
to true URLs.  Examples include lp:urls and per-user location aliases.
21
21
"""
22
22
 
 
23
from __future__ import absolute_import
 
24
 
23
25
from . import (
24
26
    errors,
25
27
    registry,
34
36
""")
35
37
 
36
38
 
37
 
class DirectoryLookupFailure(errors.BzrError):
38
 
    """Base type for lookup errors."""
39
 
 
40
 
 
41
 
class InvalidLocationAlias(DirectoryLookupFailure):
42
 
 
43
 
    _fmt = '"%(alias_name)s" is not a valid location alias.'
44
 
 
45
 
    def __init__(self, alias_name):
46
 
        DirectoryLookupFailure.__init__(self, alias_name=alias_name)
47
 
 
48
 
 
49
 
class UnsetLocationAlias(DirectoryLookupFailure):
50
 
 
51
 
    _fmt = 'No %(alias_name)s location assigned.'
52
 
 
53
 
    def __init__(self, alias_name):
54
 
        DirectoryLookupFailure.__init__(self, alias_name=alias_name[1:])
55
 
 
56
 
 
57
39
class DirectoryServiceRegistry(registry.Registry):
58
40
    """This object maintains and uses a list of directory services.
59
41
 
65
47
    name and URL, and return a URL.
66
48
    """
67
49
 
68
 
    def dereference(self, url, purpose=None):
 
50
    def dereference(self, url):
69
51
        """Dereference a supplied URL if possible.
70
52
 
71
53
        URLs that match a registered directory service prefix are looked up in
75
57
        requires further dereferencing.
76
58
 
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.
80
61
        """
81
62
        match = self.get_prefix(url)
82
63
        if match is None:
83
64
            return url
84
65
        service, name = match
85
 
        directory = service()
86
 
        try:
87
 
            return directory.look_up(name, url, purpose=purpose)
88
 
        except TypeError:
89
 
            # Compatibility for plugins written for Breezy < 3.0.0
90
 
            return directory.look_up(name, url)
91
 
 
 
66
        return service().look_up(name, url)
92
67
 
93
68
directories = DirectoryServiceRegistry()
94
69
 
95
 
 
96
 
class Directory(object):
97
 
    """Abstract directory lookup class."""
98
 
 
99
 
    def look_up(self, name, url, purpose=None):
100
 
        """Look up an entry in a directory.
101
 
 
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.
106
 
        """
107
 
        raise NotImplementedError(self.look_up)
108
 
 
109
 
 
110
 
class AliasDirectory(Directory):
 
70
class AliasDirectory(object):
111
71
    """Directory lookup for locations associated with a branch.
112
72
 
113
73
    :parent, :submit, :public, :push, :this, and :bound are currently
116
76
 
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,
129
 
                            help="This branch.")
 
89
        help="This branch.")
130
90
 
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:
139
99
        try:
140
100
            method = self.branch_aliases.get(name[1:])
141
101
        except KeyError:
142
 
            raise InvalidLocationAlias(url)
 
102
            raise errors.InvalidLocationAlias(url)
143
103
        else:
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)
149
109
        return result
159
119
================
160
120
 
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`.
163
123
 
164
124
The aliases are::
165
125
 
166
126
%s
167
127
For example, to push to the parent location::
168
128
 
169
 
    brz push :parent
 
129
    bzr push :parent
170
130
""" % "".join(alias_lines)
171
131
 
172
132
 
174
134
                     'Easy access to remembered branch locations')
175
135
 
176
136
 
177
 
class ColocatedDirectory(Directory):
 
137
class ColocatedDirectory(object):
178
138
    """Directory lookup for colocated branches.
179
139
 
180
140
    co:somename will resolve to the colocated branch with "somename" in
181
141
    the current directory.
182
142
    """
183
143
 
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)})
188
148
 
189
149
 
190
150
directories.register('co:', ColocatedDirectory,
191
151
                     'Easy access to colocated branches')
 
152