1
# Copyright (C) 2011 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
"""Content-filtered view of any tree.
20
from __future__ import absolute_import
25
from bzrlib.filters import (
27
filtered_output_bytes,
31
class ContentFilterTree(tree.Tree):
32
"""A virtual tree that applies content filters to an underlying tree.
34
Not every operation is supported yet.
37
def __init__(self, backing_tree, filter_stack_callback):
38
"""Construct a new filtered tree view.
40
:param filter_stack_callback: A callable taking a path that returns
41
the filter stack that should be used for that path.
42
:param backing_tree: An underlying tree to wrap.
44
self.backing_tree = backing_tree
45
self.filter_stack_callback = filter_stack_callback
47
def get_file_text(self, file_id, path=None):
48
chunks = self.backing_tree.get_file_lines(file_id, path)
50
path = self.backing_tree.id2path(file_id)
51
filters = self.filter_stack_callback(path)
52
context = ContentFilterContext(path, self, None)
53
contents = filtered_output_bytes(chunks, filters, context)
54
content = ''.join(contents)
57
def has_filename(self, filename):
58
return self.backing_tree.has_filename
60
def is_executable(self, file_id, path=None):
61
return self.backing_tree.is_executable(file_id, path)
63
def iter_entries_by_dir(self, specific_file_ids=None, yield_parents=None):
64
# NB: This simply returns the parent tree's entries; the length may be
65
# wrong but it can't easily be calculated without filtering the whole
66
# text. Currently all callers cope with this; perhaps they should be
67
# updated to a narrower interface that only provides things guaranteed
68
# cheaply available across all trees. -- mbp 20110705
69
return self.backing_tree.iter_entries_by_dir(
70
specific_file_ids=specific_file_ids,
71
yield_parents=yield_parents)
74
return self.backing_tree.lock_read()
77
return self.backing_tree.unlock()