/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/inter.py

  • Committer: Jelmer Vernooij
  • Date: 2020-09-02 16:35:18 UTC
  • mto: (7490.40.109 work)
  • mto: This revision was merged to the branch mainline in revision 7526.
  • Revision ID: jelmer@jelmer.uk-20200902163518-sy9f4unbboljphgu
Handle duplicate directories entries for git.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
 
18
17
"""Inter-object utility class."""
19
18
 
 
19
from __future__ import absolute_import
 
20
 
 
21
from .errors import BzrError
 
22
from .lock import LogicalLockResult
 
23
from .pyutils import get_named_object
 
24
 
 
25
 
 
26
class NoCompatibleInter(BzrError):
 
27
 
 
28
    _fmt = ('No compatible object available for operations from %(source)r '
 
29
            'to %(target)r.')
 
30
 
 
31
    def __init__(self, source, target):
 
32
        self.source = source
 
33
        self.target = target
 
34
 
20
35
 
21
36
class InterObject(object):
22
37
    """This class represents operations taking place between two objects.
32
47
 
33
48
    If the source and target objects implement the locking protocol -
34
49
    lock_read, lock_write, unlock, then the InterObject's lock_read,
35
 
    lock_write and unlock methods may be used (optionally in conjunction with
36
 
    the needs_read_lock and needs_write_lock decorators.)
 
50
    lock_write and unlock methods may be used.
37
51
 
38
52
    When looking for an inter, the most recently registered types are tested
39
53
    first.  So typically the most generic and slowest InterObjects should be
75
89
                       the InterObject instance.
76
90
        :param target: the object to be the 'target' member of
77
91
                       the InterObject instance.
 
92
 
78
93
        If an optimised worker exists it will be used otherwise
79
94
        a default Inter worker instance will be created.
80
95
        """
81
 
        for provider in reversed(klass._optimisers):
 
96
        for i, provider in enumerate(reversed(klass._optimisers)):
 
97
            if isinstance(provider, tuple):
 
98
                provider = get_named_object(provider[0], provider[1])
 
99
                klass._optimisers[-i] = provider
82
100
            if provider.is_compatible(source, target):
83
101
                return provider(source, target)
84
 
        return klass(source, target)
 
102
        raise NoCompatibleInter(source, target)
 
103
 
 
104
    @classmethod
 
105
    def iter_optimisers(klass):
 
106
        for provider in klass._optimisers:
 
107
            if isinstance(provider, tuple):
 
108
                yield get_named_object(provider[0], provider[1])
 
109
            else:
 
110
                yield provider
85
111
 
86
112
    def lock_read(self):
87
113
        """Take out a logical read lock.
90
116
        a read lock and the target a read lock.
91
117
        """
92
118
        self._double_lock(self.source.lock_read, self.target.lock_read)
 
119
        return LogicalLockResult(self.unlock)
93
120
 
94
121
    def lock_write(self):
95
122
        """Take out a logical write lock.
98
125
        a read lock and the target a write lock.
99
126
        """
100
127
        self._double_lock(self.source.lock_read, self.target.lock_write)
 
128
        return LogicalLockResult(self.unlock)
101
129
 
102
130
    @classmethod
103
131
    def register_optimiser(klass, optimiser):
104
132
        """Register an InterObject optimiser."""
105
133
        klass._optimisers.append(optimiser)
106
134
 
 
135
    @classmethod
 
136
    def register_lazy_optimiser(klass, module_name, member_name):
 
137
        # TODO(jelmer): Allow passing in a custom .is_compatible
 
138
        klass._optimisers.append((module_name, member_name))
 
139
 
107
140
    def unlock(self):
108
141
        """Release the locks on source and target."""
109
142
        try: