13
13
# You should have received a copy of the GNU General Public License
14
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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18
"""Inter-object utility class."""
19
from .errors import BzrError
20
from .lock import LogicalLockResult
21
from .pyutils import get_named_object
24
class NoCompatibleInter(BzrError):
26
_fmt = ('No compatible object available for operations from %(source)r '
29
def __init__(self, source, target):
34
21
class InterObject(object):
35
22
"""This class represents operations taking place between two objects.
37
24
Its instances have methods like join or copy_content or fetch, and contain
38
references to the source and target objects these operations can be
25
references to the source and target objects these operations can be
39
26
carried out between.
41
28
Often we will provide convenience methods on the objects which carry out
42
29
operations with another of similar type - they will always forward to
43
a subclass of InterObject - i.e.
30
a subclass of InterObject - i.e.
44
31
InterVersionedFile.get(other).method_name(parameters).
46
If the source and target objects implement the locking protocol -
33
If the source and target objects implement the locking protocol -
47
34
lock_read, lock_write, unlock, then the InterObject's lock_read,
48
lock_write and unlock methods may be used.
35
lock_write and unlock methods may be used (optionally in conjunction with
36
the needs_read_lock and needs_write_lock decorators.)
50
38
When looking for an inter, the most recently registered types are tested
51
39
first. So typically the most generic and slowest InterObjects should be
58
46
def __init__(self, source, target):
59
47
"""Construct a default InterObject instance. Please use 'get'.
61
Only subclasses of InterObject should call
49
Only subclasses of InterObject should call
62
50
InterObject.__init__ - clients should call InterFOO.get where FOO
63
51
is the base type of the objects they are interacting between. I.e.
64
52
InterVersionedFile or InterRepository.
87
75
the InterObject instance.
88
76
:param target: the object to be the 'target' member of
89
77
the InterObject instance.
91
78
If an optimised worker exists it will be used otherwise
92
79
a default Inter worker instance will be created.
94
for i, provider in enumerate(reversed(klass._optimisers)):
95
if isinstance(provider, tuple):
96
provider = get_named_object(provider[0], provider[1])
97
klass._optimisers[-i] = provider
81
for provider in reversed(klass._optimisers):
98
82
if provider.is_compatible(source, target):
99
83
return provider(source, target)
100
raise NoCompatibleInter(source, target)
103
def iter_optimisers(klass):
104
for provider in klass._optimisers:
105
if isinstance(provider, tuple):
106
yield get_named_object(provider[0], provider[1])
84
return klass(source, target)
110
86
def lock_read(self):
111
87
"""Take out a logical read lock.
114
90
a read lock and the target a read lock.
116
92
self._double_lock(self.source.lock_read, self.target.lock_read)
117
return LogicalLockResult(self.unlock)
119
94
def lock_write(self):
120
95
"""Take out a logical write lock.
123
98
a read lock and the target a write lock.
125
100
self._double_lock(self.source.lock_read, self.target.lock_write)
126
return LogicalLockResult(self.unlock)
129
103
def register_optimiser(klass, optimiser):
130
104
"""Register an InterObject optimiser."""
131
105
klass._optimisers.append(optimiser)
134
def register_lazy_optimiser(klass, module_name, member_name):
135
# TODO(jelmer): Allow passing in a custom .is_compatible
136
klass._optimisers.append((module_name, member_name))
138
107
def unlock(self):
139
108
"""Release the locks on source and target."""