/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4454.3.1 by John Arbash Meinel
Initial api for Annotator.
1
# Copyright (C) 2009 Canonical Ltd
2
#
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.
7
#
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.
12
#
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17
"""Functionality for doing annotations in the 'optimal' way"""
18
19
from bzrlib import (
20
    annotate,
21
    errors,
22
    graph as _mod_graph,
23
    osutils,
4454.3.3 by John Arbash Meinel
Start implementing the reannotation functionality directly.
24
    patiencediff,
4454.3.1 by John Arbash Meinel
Initial api for Annotator.
25
    )
26
27
28
class AnnotatorPolicy(object):
29
    """Variables that define annotations."""
30
31
32
class Annotator(object):
33
    """Class that drives performing annotations."""
34
35
    def __init__(self, vf):
36
        """Create a new Annotator from a VersionedFile."""
37
        self._vf = vf
4454.3.2 by John Arbash Meinel
Start moving bits into helper functions. Add tests for multiple revs.
38
        self._parent_map = {}
4454.3.3 by John Arbash Meinel
Start implementing the reannotation functionality directly.
39
        self._lines_cache = {}
40
        self._annotations_cache = {}
4454.3.2 by John Arbash Meinel
Start moving bits into helper functions. Add tests for multiple revs.
41
        self._heads_provider = None
4454.3.1 by John Arbash Meinel
Initial api for Annotator.
42
4454.3.2 by John Arbash Meinel
Start moving bits into helper functions. Add tests for multiple revs.
43
    def _get_needed_texts(self, key):
4454.3.1 by John Arbash Meinel
Initial api for Annotator.
44
        graph = _mod_graph.Graph(self._vf)
45
        parent_map = dict((k, v) for k, v in graph.iter_ancestry([key])
46
                          if v is not None)
4454.3.2 by John Arbash Meinel
Start moving bits into helper functions. Add tests for multiple revs.
47
        self._parent_map.update(parent_map)
4454.3.1 by John Arbash Meinel
Initial api for Annotator.
48
        keys = parent_map.keys()
4454.3.2 by John Arbash Meinel
Start moving bits into helper functions. Add tests for multiple revs.
49
        return keys
50
51
    def _get_heads_provider(self):
52
        if self._heads_provider is None:
53
            self._heads_provider = _mod_graph.KnownGraph(self._parent_map)
54
        return self._heads_provider
55
4454.3.3 by John Arbash Meinel
Start implementing the reannotation functionality directly.
56
    def _reannotate_one_parent(self, annotations, lines, key, parent_key):
57
        """Reannotate this text relative to its first parent."""
58
        parent_lines = self._lines_cache[parent_key]
59
        parent_annotations = self._annotations_cache[parent_key]
60
        # PatienceSequenceMatcher should probably be part of Policy
61
        matcher = patiencediff.PatienceSequenceMatcher(None,
62
            parent_lines, lines)
63
        matching_blocks = matcher.get_matching_blocks()
64
65
        for parent_idx, lines_idx, match_len in matching_blocks:
66
            # For all matching regions we copy across the parent annotations
67
            annotations[lines_idx:lines_idx + match_len] = \
68
                parent_annotations[parent_idx:parent_idx + match_len]
69
4454.3.2 by John Arbash Meinel
Start moving bits into helper functions. Add tests for multiple revs.
70
    def annotate(self, key):
71
        """Return annotated fulltext for the given key."""
72
        keys = self._get_needed_texts(key)
73
        heads_provider = self._get_heads_provider
4454.3.1 by John Arbash Meinel
Initial api for Annotator.
74
        for record in self._vf.get_record_stream(keys, 'topological', True):
4454.3.3 by John Arbash Meinel
Start implementing the reannotation functionality directly.
75
            this_key = record.key
4454.3.2 by John Arbash Meinel
Start moving bits into helper functions. Add tests for multiple revs.
76
            lines = osutils.chunks_to_lines(record.get_bytes_as('chunked'))
4454.3.3 by John Arbash Meinel
Start implementing the reannotation functionality directly.
77
            annotations = [(this_key,)]*len(lines)
78
            self._lines_cache[this_key] = lines
79
            self._annotations_cache[this_key] = annotations
80
81
            parents = self._parent_map[this_key]
82
            if not parents:
83
                continue
84
            self._reannotate_one_parent(annotations, lines, key, parents[0])
4454.3.1 by John Arbash Meinel
Initial api for Annotator.
85
        try:
4454.3.3 by John Arbash Meinel
Start implementing the reannotation functionality directly.
86
            annotations = self._annotations_cache[key]
87
        except KeyError:
4454.3.1 by John Arbash Meinel
Initial api for Annotator.
88
            raise errors.RevisionNotPresent(key, self._vf)
4454.3.3 by John Arbash Meinel
Start implementing the reannotation functionality directly.
89
        return annotations, self._lines_cache[key]