1
# Copyright (C) 2009 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Functionality for doing annotations in the 'optimal' way"""
27
class AnnotatorPolicy(object):
28
"""Variables that define annotations."""
31
class Annotator(object):
32
"""Class that drives performing annotations."""
34
def __init__(self, vf):
35
"""Create a new Annotator from a VersionedFile."""
38
def annotate(self, key):
39
"""Return annotated fulltext for the given key."""
40
graph = _mod_graph.Graph(self._vf)
41
parent_map = dict((k, v) for k, v in graph.iter_ancestry([key])
44
raise errors.RevisionNotPresent(key, self)
45
keys = parent_map.keys()
46
heads_provider = _mod_graph.KnownGraph(parent_map)
48
reannotate = annotate.reannotate
49
for record in self._vf.get_record_stream(keys, 'topological', True):
51
fulltext = osutils.chunks_to_lines(record.get_bytes_as('chunked'))
52
parents = parent_map[key]
53
if parents is not None:
54
parent_lines = [parent_cache[parent] for parent in parent_map[key]]
57
parent_cache[key] = list(
58
reannotate(parent_lines, fulltext, key, None, heads_provider))
60
annotated = parent_cache[key]
62
raise errors.RevisionNotPresent(key, self._vf)
63
annotations = [(a,) for a,l in annotated]
64
lines = [l for a,l in annotated]
65
return annotations, lines