1
# Copyright (C) 2006 Canonical Ltd
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
"""Inter-object utility class."""
21
class InterObject(object):
22
"""This class represents operations taking place between two objects.
24
Its instances have methods like join or copy_content or fetch, and contain
25
references to the source and target objects these operations can be
28
Often we will provide convenience methods on the objects which carry out
29
operations with another of similar type - they will always forward to
30
a subclass of InterObject - i.e.
31
InterVersionedFile.get(other).method_name(parameters).
35
# Each concrete InterObject type should have its own optimisers set.
37
def __init__(self, source, target):
38
"""Construct a default InterObject instance. Please use 'get'.
40
Only subclasses of InterObject should call
41
InterObject.__init__ - clients should call InterFOO.get where FOO
42
is the base type of the objects they are interacting between. I.e.
43
InterVersionedFile or InterRepository.
44
get() is a convenience class method which will create an optimised
51
def get(klass, source, target):
52
"""Retrieve a Inter worker object for these objects.
54
:param source: the object to be the 'source' member of
55
the InterObject instance.
56
:param target: the object to be the 'target' member of
57
the InterObject instance.
58
If an optimised worker exists it will be used otherwise
59
a default Inter worker instance will be created.
61
for provider in klass._optimisers:
62
if provider.is_compatible(source, target):
63
return provider(source, target)
64
return klass(source, target)
67
def register_optimiser(klass, optimiser):
68
"""Register an InterObject optimiser."""
69
klass._optimisers.add(optimiser)
72
def unregister_optimiser(klass, optimiser):
73
"""Unregister an InterObject optimiser."""
74
klass._optimisers.remove(optimiser)