/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-02-18 01:57:45 UTC
  • mto: This revision was merged to the branch mainline in revision 7493.
  • Revision ID: jelmer@jelmer.uk-20200218015745-q2ss9tsk74h4nh61
drop use of future.

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 .errors import BzrError
 
20
from .lock import LogicalLockResult
 
21
 
 
22
 
 
23
class NoCompatibleInter(BzrError):
 
24
 
 
25
    _fmt = ('No compatible object available for operations from %(source)r '
 
26
            'to %(target)r.')
 
27
 
 
28
    def __init__(self, source, target):
 
29
        self.source = source
 
30
        self.target = target
 
31
 
20
32
 
21
33
class InterObject(object):
22
34
    """This class represents operations taking place between two objects.
32
44
 
33
45
    If the source and target objects implement the locking protocol -
34
46
    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.)
 
47
    lock_write and unlock methods may be used.
37
48
 
38
49
    When looking for an inter, the most recently registered types are tested
39
50
    first.  So typically the most generic and slowest InterObjects should be
75
86
                       the InterObject instance.
76
87
        :param target: the object to be the 'target' member of
77
88
                       the InterObject instance.
 
89
 
78
90
        If an optimised worker exists it will be used otherwise
79
91
        a default Inter worker instance will be created.
80
92
        """
81
93
        for provider in reversed(klass._optimisers):
82
94
            if provider.is_compatible(source, target):
83
95
                return provider(source, target)
84
 
        return klass(source, target)
 
96
        raise NoCompatibleInter(source, target)
85
97
 
86
98
    def lock_read(self):
87
99
        """Take out a logical read lock.
90
102
        a read lock and the target a read lock.
91
103
        """
92
104
        self._double_lock(self.source.lock_read, self.target.lock_read)
 
105
        return LogicalLockResult(self.unlock)
93
106
 
94
107
    def lock_write(self):
95
108
        """Take out a logical write lock.
98
111
        a read lock and the target a write lock.
99
112
        """
100
113
        self._double_lock(self.source.lock_read, self.target.lock_write)
 
114
        return LogicalLockResult(self.unlock)
101
115
 
102
116
    @classmethod
103
117
    def register_optimiser(klass, optimiser):