/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: Martin
  • Date: 2018-11-16 16:38:22 UTC
  • mto: This revision was merged to the branch mainline in revision 7172.
  • Revision ID: gzlist@googlemail.com-20181116163822-yg1h1cdng6w7w9kn
Make --profile-imports work on Python 3

Also tweak heading to line up correctly.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008 Canonical Ltd
 
1
# Copyright (C) 2008, 2009, 2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
20
20
to true URLs.  Examples include lp:urls and per-user location aliases.
21
21
"""
22
22
 
23
 
from bzrlib import errors, registry
24
 
from bzrlib.lazy_import import lazy_import
 
23
from __future__ import absolute_import
 
24
 
 
25
from . import (
 
26
    errors,
 
27
    registry,
 
28
    )
 
29
from .lazy_import import lazy_import
25
30
lazy_import(globals(), """
26
 
from bzrlib.branch import Branch
27
 
from bzrlib import urlutils
 
31
from breezy import (
 
32
    branch as _mod_branch,
 
33
    controldir as _mod_controldir,
 
34
    urlutils,
 
35
    )
28
36
""")
29
37
 
30
38
 
 
39
class DirectoryLookupFailure(errors.BzrError):
 
40
    """Base type for lookup errors."""
 
41
 
 
42
 
 
43
class InvalidLocationAlias(DirectoryLookupFailure):
 
44
 
 
45
    _fmt = '"%(alias_name)s" is not a valid location alias.'
 
46
 
 
47
    def __init__(self, alias_name):
 
48
        DirectoryLookupFailure.__init__(self, alias_name=alias_name)
 
49
 
 
50
 
 
51
class UnsetLocationAlias(DirectoryLookupFailure):
 
52
 
 
53
    _fmt = 'No %(alias_name)s location assigned.'
 
54
 
 
55
    def __init__(self, alias_name):
 
56
        DirectoryLookupFailure.__init__(self, alias_name=alias_name[1:])
 
57
 
 
58
 
31
59
class DirectoryServiceRegistry(registry.Registry):
32
60
    """This object maintains and uses a list of directory services.
33
61
 
59
87
 
60
88
directories = DirectoryServiceRegistry()
61
89
 
62
 
 
63
90
class AliasDirectory(object):
64
91
    """Directory lookup for locations associated with a branch.
65
92
 
67
94
    supported.  On error, a subclass of DirectoryLookupFailure will be raised.
68
95
    """
69
96
 
 
97
    branch_aliases = registry.Registry()
 
98
    branch_aliases.register('parent', lambda b: b.get_parent(),
 
99
        help="The parent of this branch.")
 
100
    branch_aliases.register('submit', lambda b: b.get_submit_branch(),
 
101
        help="The submit branch for this branch.")
 
102
    branch_aliases.register('public', lambda b: b.get_public_branch(),
 
103
        help="The public location of this branch.")
 
104
    branch_aliases.register('bound', lambda b: b.get_bound_location(),
 
105
        help="The branch this branch is bound to, for bound branches.")
 
106
    branch_aliases.register('push', lambda b: b.get_push_location(),
 
107
        help="The saved location used for `brz push` with no arguments.")
 
108
    branch_aliases.register('this', lambda b: b.base,
 
109
        help="This branch.")
 
110
 
70
111
    def look_up(self, name, url):
71
 
        branch = Branch.open_containing('.')[0]
72
 
        lookups = {
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
79
 
        }
 
112
        branch = _mod_branch.Branch.open_containing('.')[0]
80
113
        parts = url.split('/', 1)
81
114
        if len(parts) == 2:
82
115
            name, extra = parts
84
117
            (name,) = parts
85
118
            extra = None
86
119
        try:
87
 
            method = lookups[name[1:]]
 
120
            method = self.branch_aliases.get(name[1:])
88
121
        except KeyError:
89
 
            raise errors.InvalidLocationAlias(url)
 
122
            raise InvalidLocationAlias(url)
90
123
        else:
91
 
            result = method()
 
124
            result = method(branch)
92
125
        if result is None:
93
 
            raise errors.UnsetLocationAlias(url)
 
126
            raise UnsetLocationAlias(url)
94
127
        if extra is not None:
95
128
            result = urlutils.join(result, extra)
96
129
        return result
97
130
 
 
131
    @classmethod
 
132
    def help_text(cls, topic):
 
133
        alias_lines = []
 
134
        for key in cls.branch_aliases.keys():
 
135
            help = cls.branch_aliases.get_help(key)
 
136
            alias_lines.append("  :%-10s%s\n" % (key, help))
 
137
        return """\
 
138
Location aliases
 
139
================
 
140
 
 
141
Bazaar defines several aliases for locations associated with a branch.  These
 
142
can be used with most commands that expect a location, such as `brz push`.
 
143
 
 
144
The aliases are::
 
145
 
 
146
%s
 
147
For example, to push to the parent location::
 
148
 
 
149
    brz push :parent
 
150
""" % "".join(alias_lines)
 
151
 
 
152
 
98
153
directories.register(':', AliasDirectory,
99
154
                     'Easy access to remembered branch locations')
 
155
 
 
156
 
 
157
class ColocatedDirectory(object):
 
158
    """Directory lookup for colocated branches.
 
159
 
 
160
    co:somename will resolve to the colocated branch with "somename" in
 
161
    the current directory.
 
162
    """
 
163
 
 
164
    def look_up(self, name, url):
 
165
        dir = _mod_controldir.ControlDir.open_containing('.')[0]
 
166
        return urlutils.join_segment_parameters(dir.user_url,
 
167
            {"branch": urlutils.escape(name)})
 
168
 
 
169
 
 
170
directories.register('co:', ColocatedDirectory,
 
171
                     'Easy access to colocated branches')
 
172