/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: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2018-11-16 18:26:22 UTC
  • mfrom: (7167.1.4 run-flake8)
  • Revision ID: breezy.the.bot@gmail.com-20181116182622-qw3gan3hz78a2imw
Add a flake8 test.

Merged from https://code.launchpad.net/~jelmer/brz/run-flake8/+merge/358902

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2008, 2009, 2011 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Directory service registration and usage.
 
18
 
 
19
Directory services are utilities that provide a mapping from URL-like strings
 
20
to true URLs.  Examples include lp:urls and per-user location aliases.
 
21
"""
 
22
 
 
23
from __future__ import absolute_import
 
24
 
 
25
from . import (
 
26
    errors,
 
27
    registry,
 
28
    )
 
29
from .lazy_import import lazy_import
 
30
lazy_import(globals(), """
 
31
from breezy import (
 
32
    branch as _mod_branch,
 
33
    controldir as _mod_controldir,
 
34
    urlutils,
 
35
    )
 
36
""")
 
37
 
 
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
 
 
59
class DirectoryServiceRegistry(registry.Registry):
 
60
    """This object maintains and uses a list of directory services.
 
61
 
 
62
    Directory services may be registered via the standard Registry methods.
 
63
    They will be invoked if their key is a prefix of the supplied URL.
 
64
 
 
65
    Each item registered should be a factory of objects that provide a look_up
 
66
    method, as invoked by dereference.  Specifically, look_up should accept a
 
67
    name and URL, and return a URL.
 
68
    """
 
69
 
 
70
    def dereference(self, url):
 
71
        """Dereference a supplied URL if possible.
 
72
 
 
73
        URLs that match a registered directory service prefix are looked up in
 
74
        it.  Non-matching urls are returned verbatim.
 
75
 
 
76
        This is applied only once; the resulting URL must not be one that
 
77
        requires further dereferencing.
 
78
 
 
79
        :param url: The URL to dereference
 
80
        :return: The dereferenced URL if applicable, the input URL otherwise.
 
81
        """
 
82
        match = self.get_prefix(url)
 
83
        if match is None:
 
84
            return url
 
85
        service, name = match
 
86
        return service().look_up(name, url)
 
87
 
 
88
directories = DirectoryServiceRegistry()
 
89
 
 
90
class AliasDirectory(object):
 
91
    """Directory lookup for locations associated with a branch.
 
92
 
 
93
    :parent, :submit, :public, :push, :this, and :bound are currently
 
94
    supported.  On error, a subclass of DirectoryLookupFailure will be raised.
 
95
    """
 
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
 
 
111
    def look_up(self, name, url):
 
112
        branch = _mod_branch.Branch.open_containing('.')[0]
 
113
        parts = url.split('/', 1)
 
114
        if len(parts) == 2:
 
115
            name, extra = parts
 
116
        else:
 
117
            (name,) = parts
 
118
            extra = None
 
119
        try:
 
120
            method = self.branch_aliases.get(name[1:])
 
121
        except KeyError:
 
122
            raise InvalidLocationAlias(url)
 
123
        else:
 
124
            result = method(branch)
 
125
        if result is None:
 
126
            raise UnsetLocationAlias(url)
 
127
        if extra is not None:
 
128
            result = urlutils.join(result, extra)
 
129
        return result
 
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
 
 
153
directories.register(':', AliasDirectory,
 
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