~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/directory_service.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-06-30 05:55:35 UTC
  • mfrom: (3512.2.4 alias.lookup)
  • Revision ID: pqm@pqm.ubuntu.com-20080630055535-42tx43kb228k4p94
Implement aliases for remembered locations (abentley)

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 bzrlib import registry
 
23
from bzrlib import errors, registry
 
24
from bzrlib.branch import Branch
24
25
 
25
26
class DirectoryServiceRegistry(registry.Registry):
26
27
    """This object maintains and uses a list of directory services.
52
53
        return service().look_up(name, url)
53
54
 
54
55
directories = DirectoryServiceRegistry()
 
56
 
 
57
 
 
58
class AliasDirectory(object):
 
59
    """Directory lookup for locations associated with a branch.
 
60
 
 
61
    :parent, :submit, :public, :push, :this, and :bound are currently
 
62
    supported.  On error, a subclass of DirectoryLookupFailure will be raised.
 
63
    """
 
64
 
 
65
    def look_up(self, name, url):
 
66
        branch = Branch.open_containing('.')[0]
 
67
        lookups = {
 
68
            'parent': branch.get_parent,
 
69
            'submit': branch.get_submit_branch,
 
70
            'public': branch.get_public_branch,
 
71
            'bound': branch.get_bound_location,
 
72
            'push': branch.get_push_location,
 
73
            'this': lambda: branch.base
 
74
        }
 
75
        try:
 
76
            method = lookups[url[1:]]
 
77
        except KeyError:
 
78
            raise errors.InvalidLocationAlias(url)
 
79
        else:
 
80
            result = method()
 
81
        if result is None:
 
82
            raise errors.UnsetLocationAlias(url)
 
83
        return result
 
84
 
 
85
directories.register(':', AliasDirectory,
 
86
                     'Easy access to remembered branch locations')