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.
24
from bzrlib.filters import (
28
filtered_output_bytes,
29
_get_filter_stack_for,
30
_get_registered_names,
31
internal_size_sha_file_byname,
32
register_filter_stack_map,
36
class ContentFilterTree(tree.Tree):
37
"""A virtual tree that applies content filters to an underlying tree.
39
Not every operation is supported yet.
42
def __init__(self, backing_tree, filter_stack_callback):
43
"""Construct a new filtered tree view.
45
:param filter_stack_callback: A callable taking a path that returns
46
the filter stack that should be used for that path.
47
:param backing_tree: An underlying tree to wrap.
49
self.backing_tree = backing_tree
50
self.filter_stack_callback = filter_stack_callback
52
def get_file_text(self, file_id, path=None):
53
chunks = self.backing_tree.get_file_lines(file_id, path)
54
filters = self.filter_stack_callback(path)
56
path = self.backing_tree.id2path(file_id)
57
context = ContentFilterContext(path, self, None)
58
contents = filtered_output_bytes(chunks, filters, context)
59
content = ''.join(contents)
62
def has_filename(self, filename):
63
return self.backing_tree.has_filename
65
def is_executable(self, file_id, path=None):
66
return self.backing_tree.is_executable(file_id, path)
68
def iter_entries_by_dir(self, specific_file_ids=None, yield_parents=None):
69
# NB: This simply returns the parent tree's entries; the length may be
70
# wrong but it can't easily be calculated without filtering the whole
71
# text. Currently all callers cope with this; perhaps they should be
72
# updated to a narrower interface that only provides things guaranteed
73
# cheaply available across all trees. -- mbp 20110705
74
return self.backing_tree.iter_entries_by_dir(
75
specific_file_ids=specific_file_ids,
76
yield_parents=yield_parents)
79
return self.backing_tree.lock_read()
82
return self.backing_tree.unlock()