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

  • Committer: Robert Collins
  • Date: 2007-04-19 02:27:44 UTC
  • mto: This revision was merged to the branch mainline in revision 2426.
  • Revision ID: robertc@robertcollins.net-20070419022744-pfdqz42kp1wizh43
``make docs`` now creates a man page at ``man1/bzr.1`` fixing bug 107388.
(Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
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
 
16
 
16
17
 
17
18
"""Inter-object utility class."""
18
19
 
19
 
from .errors import BzrError
20
 
from .lock import LogicalLockResult
21
 
from .pyutils import get_named_object
22
 
 
23
 
 
24
 
class NoCompatibleInter(BzrError):
25
 
 
26
 
    _fmt = ('No compatible object available for operations from %(source)r '
27
 
            'to %(target)r.')
28
 
 
29
 
    def __init__(self, source, target):
30
 
        self.source = source
31
 
        self.target = target
32
 
 
33
20
 
34
21
class InterObject(object):
35
22
    """This class represents operations taking place between two objects.
36
23
 
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.
40
27
 
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).
45
32
 
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.)
49
37
 
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
57
45
 
58
46
    def __init__(self, source, target):
59
47
        """Construct a default InterObject instance. Please use 'get'.
60
 
 
61
 
        Only subclasses of InterObject should call
 
48
        
 
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.
90
 
 
91
78
        If an optimised worker exists it will be used otherwise
92
79
        a default Inter worker instance will be created.
93
80
        """
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)
101
 
 
102
 
    @classmethod
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])
107
 
            else:
108
 
                yield provider
 
84
        return klass(source, target)
109
85
 
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.
115
91
        """
116
92
        self._double_lock(self.source.lock_read, self.target.lock_read)
117
 
        return LogicalLockResult(self.unlock)
118
93
 
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.
124
99
        """
125
100
        self._double_lock(self.source.lock_read, self.target.lock_write)
126
 
        return LogicalLockResult(self.unlock)
127
101
 
128
102
    @classmethod
129
103
    def register_optimiser(klass, optimiser):
130
104
        """Register an InterObject optimiser."""
131
105
        klass._optimisers.append(optimiser)
132
106
 
133
 
    @classmethod
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))
137
 
 
138
107
    def unlock(self):
139
108
        """Release the locks on source and target."""
140
109
        try: